我正在尝试使用场景构建器自学JavaFx 2!
我用3个场景和每个场景的单独控制器构建了我的应用程序。
第一个场景是登录屏幕,团队成员输入其徽章编号。
当按下Login按钮时(在动作事件中)我正在根据Navision中的表来验证员工。
我要做的是将进度指示器的可见值设置为true我开始查找,但是直到线程完成才显示,然后我要么在下一个场景上,要么将可见性设置为假。
我已经尝试将它放在一个任务和一个线程中,但没有任何作用!
这是我在LoginController中的代码。
java package com.dp.inventorycheck;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.ResourceBundle;
import java.util.regex.Pattern;`
import org.apache.log4j.Logger;
import com.dp.inventorycheck.modal.Employee;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.AnchorPane;
public class LoginController extends AnchorPane implements Initializable {
@FXML
PasswordField badgeNumPF;
@FXML
Button loginBtn;
@FXML
Button keypadBtn;
@FXML
Label errorLbl;
@FXML
AnchorPane keyPad;
@FXML
ProgressIndicator progressIndicator;
@FXML
AnchorPane inputPane;
private MainApp application;
Employee employee = null;
Logger logger = Logger.getLogger(LoginController.class);
public void setApp(MainApp application) {
this.application = application;
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
public void processLogin(ActionEvent event) {
if (application == null) {
errorLbl.setText("Unknown Error!");
} else {
errorLbl.setText("");
// Check for valid badge (7 numeric characters)
if (Pattern.matches("[0-9]+", badgeNumPF.getText()) && badgeNumPF.getText().length() > 5 && badgeNumPF.getText().length() < 8) {
// Valid badge
// Check navision for valid employee and permisions
logger.info("Badge is numeric and < 8");
MyTask task = new MyTask();
updateIndicator();
Thread th = new Thread(task);
th.start();
logger.info("continuing On");
try {
th.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
if(employee != null){
application.setEmployee(employee);
application.gotoLocationSelection();
}else{
// Badge not valid
updateIndicator();
badgeNumPF.requestFocus();
errorLbl.setText("Invalid Badge Number!");
}
} else {
// Badge not valid
badgeNumPF.requestFocus();
errorLbl.setText("Invalid Badge Number!");
}
}
}
public class MyTask extends Task<Void>{
protected Void call() throws Exception {
try {
logger.info("Task Started");
employee = application.getValidator().validEmployee(badgeNumPF.getText());
logger.info("Lookup/Thread Done");
} catch (RemoteException e) {
updateIndicator();
badgeNumPF.requestFocus();
errorLbl.setText(e.getMessage());
}
return null;
}
}
private void updateIndicator() {
if (!progressIndicator.isVisible()) {
logger.info("Indicator On");
progressIndicator.setVisible(true);
} else {
logger.info("Indicator Off");
progressIndicator.setVisible(false);
}
}
}