Java AWT Threads

后端 未结 3 1606
猫巷女王i
猫巷女王i 2020-12-07 03:13

I\'m having an issue with Threads using netbeans Swing GUI. This is my first time really trying to develop a GUI for a backup program using Java\'s File System Notifier. I h

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-07 04:02

    1. Swing is Not Thread safe, but some methods like repaint(), setText() are Tread Safe.

    2. The main() method in Swing is Not Long Lived. It schedules the construction of GUI in the Event Dispactcher Thread and then quits. Now its the responsibility of the EDT to handle the GUI.

    3. You must keep your Non-UI work on your Non-UI thread, out off the GUI thread, ie EDT.

    4. Your main() method should only do the work of making the JFrame visible, using EventQueue.invokeLater.

    Eg:

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

    5 SwingWorker is provided by Java to Synchronize the Work Output of the Non-UI thread on the GUI thread.

提交回复
热议问题