我正在尝试使用场景生成器自学 JavaFx 2!我用 3 个场景和每个场景的单独控制器构建了我的应用程序。第一个场景是一个登录屏幕,团队成员在其中输入他们的徽章编号。当按下登录按钮时(在操作事件中),我正在根据 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);
}
}
}