这听起来很奇怪但是当我使用lookup()方法通过ID查找我想要的节点时,如果节点在TabPane中,它根本就不会伪造它。我已经google了很多,有人说lookup()不适用于里面有内容的节点,这是TabPane案例。有一个简单的解决方法吗?我不想从我的UI中删除窗格。感谢。
代码:
public class WindowDataFacade {
private Button btn_connect = null;
private Button btn_disconnect = null;
private Button btn_serv_opt = null;
private TextField fld_username = null;
private TextField sv_address = null;
private PasswordField passwd_field = null;
private TextField sv_port = null;
private ProgressBar progress = null;
private ProgressIndicator indicator = null;
public WindowDataFacade(Parent root) {
//Tried to use the lookup() method on the TabPane, but, no success at all.
TabPane tpane = (TabPane) root.getScene().lookup("#tab_pane");
tpane.lookup("#fld_userame");
btn_connect = (Button) root.getScene().lookup("#btn_connect");
btn_disconnect = (Button) root.getScene().lookup("#btn_disconnect");
fld_username = (TextField) tpane.getScene().lookup("#fld_userame");
passwd_field = (PasswordField) root.getScene().lookup("#passwd_field");
sv_port = (TextField) root.getScene().lookup("#sv_port");
sv_address = (TextField) root.getScene().lookup("#sv_address");
progress = (ProgressBar) root.getScene().lookup("#progress");
indicator = (ProgressIndicator) root.getScene().lookup("#indicator");
}
主要类别:
public void start(Stage arg0) throws Exception {
Platform.runLater(new Runnable() {
private Stage mainStage;
private MainScene scene;
private FXController fxControl = null;
@Override
public void run() {
System.out.println("run: " + Thread.currentThread());
mainStage = new Stage();
try {
// root = FXMLLoader.load(getClass().getResource("/fxgui.fxml"));
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxgui.fxml"));
Parent root = (Parent) loader.load();
fxControl = loader.getController();
scene = new MainScene(root);
scene.initialize(null, null);
mainStage.setScene(scene);
fxControl.setFacade(scene.getFacade());
Button node = (Button) mainStage.getScene().lookup("#btn_disconnect");
node.setDisable(true);
mainStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
});
System.out.println("public void start: " + Thread.currentThread());
}
场景类:
public class MainScene extends Scene implements Initializable {
Parent root = null;
@FXML
private Button btn_connect = new Button();
@FXML
private Button btn_disconnect = new Button();
@FXML
private Button btn_serv_opt = new Button();
@FXML
private TextField fld_username = new TextField();
@FXML
private TextField sv_address = new TextField();
@FXML
private PasswordField passwd_field = new PasswordField();
@FXML
private TextField sv_port = new TextField();
@FXML
private ProgressBar progress = new ProgressBar();
@FXML
private ProgressIndicator indicator = new ProgressIndicator();
public MainScene(Parent root) {
super(root);
this.root = root;
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
//Setting and ID for each node.
btn_connect.setId("btn_connect");
btn_disconnect.setId("btn_disconnect");
fld_username.setId("fld_username");
passwd_field.setId("passwd_field");
sv_address.setId("sv_address");
sv_port.setId("sv_port");
progress.setId("progress");
indicator.setId("indicator");
}
public WindowDataFacade getFacade() {
return new WindowDataFacade(root);
}
}
(由于某种原因,FXML在这个页面上没有正确格式化,所以我无法将其粘贴到此处,但是确保TabPane在那里,我无法访问那些节点)