从javafx中的另一个类访问窗格 [英] accessing a Pane from another class in javafx
问题描述
我无法从另一个类访问,我在一个FXML文件中创建了两个不同控制器的窗格,第二个窗格必须在第一个窗格中移动,在Click Action上,我在第二个窗格中加载了一个FXML文件,也成功地移动了窗格一次,但第二次我想从第二个控制器移动它,但它给了我一个NullPointerException。
I am unable to access from another class, I have created two pane with different controllers in one FXML File, second pane has to move around the first pane, on Click Action, and I have loaded an FXML file in second pane, also successfully moved the pane one time, but the second time I want to move it from second controller, but it is giving me an NullPointerException.
这是我的主控制器在哪里我移动了窗格:
this is my main controller where I have moved the pane:
public class MainController implements Initializable {
@FXML
private Pane searchPane;
@FXML
private Pane secondPane;
private TranslateTransition nextTransition;
public Pane getSecondPane() {
return secondPane; // Accessing Pane with this getter method
}
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
}
@FXML
private void nextBtnAction(ActionEvent event) {
try {
Parent pessFxml = FXMLLoader.load(getClass().getResource("firstPane.fxml"));
secondPane.getChildren().add(pessFxml);
nextTransition = new TranslateTransition(Duration.millis(300), secondPane);
nextTransition.setToX(searchPane.getLayoutX()-secondPane.getLayoutX());
nextTransition.play();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Form does not found" ,ex.toString(),0);
}
}
}
这是 SecondController
我在哪里访问窗格以顺利地移回它,但它抛出 NullPointerException
:
请告诉我如何解决这个问题
this is SecondController
where I am accessing the pane to move it back smoothly, but its throwing NullPointerException
:
please tell me how to solve this
public class SecondController implements Initializable {
MainController mainControll;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
}
@FXML
private void backBtnPessAction(ActionEvent event) {
//here i am putting the second pane to move back
TranslateTransition back = new TranslateTransition(Duration.millis(300), mainControll.getSecondPane());
back.setToX(mainControll.getSecondPane().getLayoutX()-750);
back.play();
}
}
推荐答案
MainController
永远不会在 SecondController
中初始化。在 nextBtnAction
中创建第二个控制器时应该传递它。
MainController
is never initialized in SecondController
. You should pass it when creating second controller in nextBtnAction
.
MainController代码:
MainController code:
private void nextBtnAction(ActionEvent event) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("firstPane.fxml"));
Parent pessFxml = loader.load();
SecondController controller = (SecondController)loader.getController();
controller.setMainController(this);
secondPane.getChildren().add(pessFxml);
nextTransition = new TranslateTransition(Duration.millis(300), secondPane);
nextTransition.setToX(searchPane.getLayoutX()-secondPane.getLayoutX());
nextTransition.play();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Form does not found" ,ex.toString(),0);
}
}
SecondController:
SecondController:
public void setMainController(MainController controller) {
this.mainControll = controller;
}
这篇关于从javafx中的另一个类访问窗格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!