Java AWT Threads

后端 未结 3 1599
猫巷女王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

    In support of davidbuzatto:

    No, I think you miss under stood what InvokeLater does. InvokeLater ensures that the runnable is executed ON the ETD. So basically, from what I can read, you've gone an put your long running, Event Blocking code right back in the ETD. Only use InvokeLater when you want to update the UI, use Threads or SwingWorker when you want to actually do processing

    void processEvents() throws IOException, InterruptedException
        {
            System.out.println("Entered processEvents");
    
            // PLEASE ETD, PUT THIS AT THE END OF THE QUEUE AND EXECUTE
            // SO I RUN WITHIN YOUR CONTEXT
            SwingUtilities.invokeLater(new Runnable() {
                public void run()
                {
    
                    // NOW RUNNING BACK ON THE ETD
                    System.out.println("Entered run");
                    list.add("test2");
                    list.repaint();
    
                    // NOW BLOCK THE ETD, SO NO MORE REPAINTS OR UPDATES WILL EVER
                    // OCCUR
                    while(true)
                    { 
                        WatchKey key;           
                        try
                        {
                            key = ws.take();
                        }
                        catch (InterruptedException x)
                        {
                            return;
                        }
    

    Sorry for the caps, but I wanted the comments to standout.

提交回复
热议问题