Reputation: 439
I created a GUI class and a demo class. the demo class is calling to the GUI. I would like to run the GUI in a different thread.
GUI Class
public class UserGui extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UserGui frame = new UserGui();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Demo Class:
public class NNDemo {
public static void main(String[] args) {
UserGui gui = new UserGui();
gui.setVisible(true);
}
}
Upvotes: 2
Views: 5786
Reputation: 33534
1. Event Dispatcher Thread
(EDT) is responsible for the Gui.
2. main()
method in GUI applications are not long lived, and after scheduling the construction of GUI in the Event Dispatcher Thread it exits.. So now its the Responsibility of the EDT to handle the GUI.
This is how i like to do the above example:
public class UserGui extends JFrame {
public UserGui() {
// You can set the size here, initialize the state and handlers.
}
}
public class Demo {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new UserGui().setVisible(true);
}
});
}
}
Upvotes: 1
Reputation: 20603
The following is the code automatically generated by Net beans for running the frame in a separated thread
public static void main(String[] args){
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new BoardPlay().setVisible(true);
}
});
}
Upvotes: 1
Reputation: 2137
public class UserGui extends JFrame {
public UserGui() {}
public void showGui() {
setVisible(true);
}
}
public class Demo {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final UserGui GUI = new UserGui();
GUI.showGui();
}
});
}
}
Upvotes: 2