show should be called from the event dispatch thread
80
81 Stage stage = (Stage) loginField.getScene().getWindow();
82 stage.setScene(scene);
83 stage.show(); 84 }
85
86 private String generateStudentNumber(){
show should be called from the event dispatch thread
51
52 Stage stage = new Stage();
53 stage.setScene(scene);
54 stage.show(); 55 }
56
57 public void deleteCourse(ActionEvent actionEvent) {
show should be called from the event dispatch thread
107
108 Stage stage = new Stage();
109 stage.setScene(scene);
110 stage.show();111 }
112
113 public void fillTree() {
show should be called from the event dispatch thread
86
87 Stage stage = new Stage();
88 stage.setScene(scene);
89 stage.show(); 90 }
91
92 public void showFolderInfo(ActionEvent actionEvent) throws IOException {
show should be called from the event dispatch thread
95
96 Stage stage = new Stage();
97 stage.setScene(scene);
98 stage.show(); 99 }
100}
show should be called from the event dispatch thread
58
59 Stage stage = new Stage();
60 stage.setScene(scene);
61 stage.show(); 62 }
63
64 public void setUserInfo(Event event) {
show should be called from the event dispatch thread
42 stage.setTitle("Course Management System");
43 stage.setScene(scene);
44 stage.initModality(Modality.APPLICATION_MODAL);
45 stage.show();46 }
47 else {
48 FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("student-courses-window.fxml"));
show should be called from the event dispatch thread
80
81 Stage stage = (Stage) loginField.getScene().getWindow();
82 stage.setScene(scene);
83 stage.show();84 }
85}
show should be called from the event dispatch thread
15 Scene scene = new Scene(fxmlLoader.load());
16 stage.setTitle("Course Management System");
17 stage.setScene(scene);
18 stage.show();19 }
20 public static void main(String[] args) {
21 launch();
Description
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.
Bad Practice
public void method() {
// These are problematic.
frame.show();
frame.setVisible(true);
frame.pack();
}
Recommended
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();
}
}
}