Is it important to use EventQueue.invokeLater in my main method?

1

Create a class inherited from JFrame and send it in another class with this code

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                JFrameEjemplo window = new JFrameEjemplo();
                window.setVisible(true);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }
        }
    });
}

My question is related to whether it is necessary to incorporate all that code or only with this code it is necessary:

public static void main(String[] args) {
            JFrameEjemplo window = new JFrameEjemplo();
            window.setVisible(true);
}
    
asked by CesarG 25.10.2017 в 00:00
source

1 answer

1

The complete processing of Swing is done in a thread called EDT (Event Dispatching Thread). So in the traditional way you could block this thread, since your program is implicitly using it (with Swing).

For this reason, the way to ensure that the GUI is not blocked is to use the form you mentioned at the beginning:

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
        new NewJFrame().setVisible(true);
    }
});

}

With this you ensure that your application is executed in another thread (thread for friends).

Greetings!

    
answered by 25.10.2017 в 01:00