Skip to main content

Posts

Showing posts from October 21, 2017

Launch Swing GUI always in a dedicated Thread

1. Since Swing is not thread safe, it is not at all a good practice to launch a UI Container like JFrame, JDialog etc in main Thread. 2. Doing so leads to incorrect behavior of java GUI. 3. To avoid this, Swing toolkit has predefined API as part of JFC. 4.    a. SwingUtilities.invokeAndWait(Runnable r);        b. SwingUtilities.invokeLater(Runnable r); 5. Which part of the code must be in UI Thread?         a. setVisible(true);         b. show(); [deprecated]  and c. pack();       6. Call to these methods mentioned in 5(a), 5(b), and 5(c) must happen in one of the methods mentioned in point 4(a) and point 4(b). That's enough. :)          Example :                        public static void main(String[] args){                         ...