80
81 Stage stage = (Stage) loginField.getScene().getWindow();
82 stage.setScene(scene);
83 stage.show(); 84 }
85
86 private String generateStudentNumber(){
51
52 Stage stage = new Stage();
53 stage.setScene(scene);
54 stage.show(); 55 }
56
57 public void deleteCourse(ActionEvent actionEvent) {
107
108 Stage stage = new Stage();
109 stage.setScene(scene);
110 stage.show();111 }
112
113 public void fillTree() {
86
87 Stage stage = new Stage();
88 stage.setScene(scene);
89 stage.show(); 90 }
91
92 public void showFolderInfo(ActionEvent actionEvent) throws IOException {
95
96 Stage stage = new Stage();
97 stage.setScene(scene);
98 stage.show(); 99 }
100}
Methods show()
, setVisible()
, and pack()
must not be invoked on the main thread.
With each invocation, these methods create the peer for the associated JFrame
. Creation of the peer involves creation of the event dispatch thread.
At this point, the event dispatch thread could be in the middle of notifying listeners while show()
, setVisible()
, or pack()
is still executing.
As a result, we could have two threads going through the Swing component-based GUI at the same time, which is a serious issue that might lead to deadlocks or other multithreading bugs.
public void method() {
// These are problematic.
frame.show();
frame.setVisible(true);
frame.pack();
}
Consider calling these methods on the event dispatch thread.
public void method() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.show();
frame.setVisible(true);
frame.pack();
}
}
}