1 2 Previous Next 16 Replies Latest reply: Jun 20, 2008 5:36 AM by Darryl Burke RSS

    Thread.sleep(int millis) makes it transparent?

    807591
      Hi. I'm new to Java, and I have a problem. While some people are wondering how to make windows transparent, I'm having trouble making it not transparent, e.g. opaque. My actual source code is too big to put here, so I made a test to demonstrate it. Please compile it to see what I mean.
      import java.awt.FlowLayout;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      
      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JOptionPane;
      
      public class sleeptest extends JFrame
      {
           public static void main(String[] args)
           {
                sleeptest test = new sleeptest();
                test.setSize(100, 100);
                test.setVisible(true);
           }
      
           JButton launch;
      
           public sleeptest()
           {
                launch = new JButton("Launch");
                add(launch);
      
                launch.addActionListener(new ActionListener()
                {
                     public void actionPerformed(ActionEvent ae)
                     {
                          JFrame newframe = new JFrame();
                          newframe.setLayout(new FlowLayout());
                          newframe.add(new JLabel("This doesn't show"));
                          newframe.setSize(300, 300);
                          newframe.setVisible(true);
      
                          try
                          {
                               Thread.sleep(10000);
                          } catch (InterruptedException e)
                          {
                          }
                          JOptionPane.showMessageDialog(null, "Time's Up!");
                     }
                });
           }
      }
      If you've run it, you'll see that the test window is transparent. Obviously I didn't want it to be. It's supposed to wait for 10 seconds, then popup "Time's Up". However, if I use Thread.sleep(int) without using an ActionListener somewhere like this:
      import java.awt.FlowLayout;
      
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JOptionPane;
      
      public class sleeptest extends JFrame
      {
           public static void main(String[] args)
           {
                JFrame newframe = new JFrame();
                newframe.setLayout(new FlowLayout());
                newframe.add(new JLabel("This isn't transparent."));
                newframe.setSize(300, 300);
                newframe.setVisible(true);
      
                try
                {
                     Thread.sleep(10000);
                } catch (InterruptedException e)
                {
                }
                JOptionPane.showMessageDialog(null, "Time's Up!");
           }
      }
      Then it's perfectly fine. Can someone tell me why this happens? It's for my game.

      Thanks.
        • 1. Re: Thread.sleep(int millis) makes it transparent?
          807591
          Never put the EDT to sleep. Instead, use a background thread, or in this case, a Swing Timer:
          import java.awt.FlowLayout;
          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;
          
          import javax.swing.JButton;
          import javax.swing.JFrame;
          import javax.swing.JLabel;
          import javax.swing.JOptionPane;
          import javax.swing.Timer;
          
          public class sleeptest extends JFrame
          {
            public static void main(String[] args)
            {
              sleeptest test = new sleeptest();
              test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              test.setSize(100, 100);
              test.setLocationRelativeTo(null);
              test.setVisible(true);
            }
          
            JButton launch;
          
            public sleeptest()
            {
              launch = new JButton("Launch");
              add(launch);
          
              launch.addActionListener(new ActionListener()
              {
                public void actionPerformed(ActionEvent ae)
                {
                  JFrame newframe = new JFrame();
                  newframe.setLayout(new FlowLayout());
                  newframe.add(new JLabel("This does show now"));
                  newframe.setSize(300, 300);
                  newframe.setLocationRelativeTo(null);
                  newframe.setVisible(true);
          
                  Timer myTimer = new Timer(4000, new ActionListener()
                  {
                    public void actionPerformed(ActionEvent arg0)
                    {
                      JOptionPane.showMessageDialog(null, "Time's Up!");
                    }
                  });
                  myTimer.setRepeats(false);
                  myTimer.start();
                }
              });
            }
          }
          Please read up on [concurrency and Swing|http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html] and on the [Swing Timer|http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html]

          Edited by: Encephalopathic on Jun 19, 2008 3:38 PM
          • 2. Re: Thread.sleep(int millis) makes it transparent?
            807591
            Sorry, but what exactly is EDT? And can someone explain what that code does? And why it makes the window transparent?

            Thanks.
            • 3. Re: Thread.sleep(int millis) makes it transparent?
              807591
              Okay, thanks. I think i've got it.
              • 4. Re: Thread.sleep(int millis) makes it transparent?
                807591
                JavaGamer wrote:
                Sorry, but what exactly is EDT?
                Please read my links, especially the first one as it explains EDT and why it's important.
                And can someone explain what that code does? And why it makes the window transparent?
                When you put the EDT (event dispatch thread) to sleep, swing cannot draw its components, or is only able to partially draw them. Again, please read the links. They will explain this and more.
                • 5. Re: Thread.sleep(int millis) makes it transparent?
                  807591
                  One more thing. How do you make the whole thing sleep for a while without suddenly making the frame transparent? I don't have anything specific to do every X seconds, just to make it pause for X seconds. Any way to do that?

                  And how come Thread.sleep() works just fine if there's no ActionListener?

                  Edited by: JavaGamer on Jun 19, 2008 5:54 PM
                  • 6. Re: Thread.sleep(int millis) makes it transparent?
                    807591
                    Umm, anyone?

                    All I want is for it to pause the 'main' thread, or what you call it, without stopping the processes that repaint the JFrame.
                    • 7. Re: Thread.sleep(int millis) makes it transparent?
                      camickr
                      How do you make the whole thing sleep for a while without suddenly making the frame transparent?
                      Your question doesn't make any sense. You don't just "pause" a GUI. An application needs to be responsive to that it can respond to mouse clicks and users typing data etc.

                      If you have a specific question about why you think you need to "pause" something then post it in the Swing forum where Swing related questions should be posted. Better yet try searching the Swing forum first.
                      • 8. Re: Thread.sleep(int millis) makes it transparent?
                        Darryl Burke
                        And how come Thread.sleep() works just fine if there's no ActionListener?
                        Why don't you go through the tutorials linked by E'hic? Listener event methods run on the EDT, so a sleep(...) call there holds up any normal EDT activity.

                        In the other case, you put the main thread to sleep and the EDT is unaffected as you have neglected to launch your GUI on the EDT.

                        Neither of which will make any sense to you until you go through that tutorial, so get cracking!

                        db
                        • 9. Re: Thread.sleep(int millis) makes it transparent?
                          807591
                          JavaGamer wrote:
                          Umm, anyone?

                          All I want is for it to pause the 'main' thread, or what you call it, without stopping the processes that repaint the JFrame.
                          GUI programs don't have a main thread. They respond to events, and they have "worker" threads which do long running activities.
                          • 10. Re: Thread.sleep(int millis) makes it transparent?
                            Darryl Burke
                            malcolmmc wrote:
                            GUI programs don't have a main thread.
                            Err, Malcolm...
                            import javax.swing.JFrame;
                            import javax.swing.SwingUtilities;
                            
                            public class ThreadTest {
                              
                              void makeUI() {
                                JFrame frame = new JFrame("Thread Test");
                                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                frame.setSize(200, 200);
                                frame.setLocationRelativeTo(null);
                                frame.setVisible(true);
                              }
                              
                              public static void main(String[] args) {
                               
                               System.out.println(Thread.currentThread().getName());
                               
                               SwingUtilities.invokeLater(new Runnable() {
                                 public void run() {
                                   System.out.println(Thread.currentThread().getName());
                                   
                                   new ThreadTest().makeUI();
                                 }
                               });
                              }
                            }
                            Prints
                            {color:black}main
                            AWT-EventQueue-0{color}
                            db
                            • 11. Re: Thread.sleep(int millis) makes it transparent?
                              807591
                              OK, to be more pedantic, they don't have a main thread once they've opened their first window. There's still the thread the JVM started to call main() but it's just another worker thread and, typically, it ends as soon as it's openned the main window. (Sometimes I make it wait until the main window is closed and have it do the clean up).
                              • 12. Re: Thread.sleep(int millis) makes it transparent?
                                Darryl Burke
                                That's only true if the GUI is (correctly) shown on the EDT. It's entirely possible to show the GUI on the main thread. This is the case in OP's second code sample.
                                import java.awt.event.WindowAdapter;
                                import java.awt.event.WindowEvent;
                                import javax.swing.JFrame;
                                import javax.swing.SwingUtilities;
                                
                                public class ThreadTest2 {
                                  
                                  void makeUI() {
                                    JFrame frame = new JFrame("Thread Test");
                                    frame.addWindowListener(new WindowAdapter() {
                                      public void windowClosing(WindowEvent e) {
                                        System.out.println(Thread.currentThread().getName());
                                      }
                                    });
                                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                    frame.setSize(200, 200);
                                    frame.setLocationRelativeTo(null);
                                    frame.setVisible(true);
                                    System.out.println(Thread.currentThread().getName());
                                  }
                                  
                                  public static void main(String[] args) {
                                   new ThreadTest2().makeUI();
                                  }
                                }
                                Note that the first sysout is after the frame is made visible.
                                (Sometimes I make it wait until the main window is closed and have it do the clean up)
                                I'd like to learn how that's done (if not too lengthy to post here), is it with Thread#join()?

                                db
                                • 13. Re: Thread.sleep(int millis) makes it transparent?
                                  807591
                                  1) Actually I don't think openning the main window directly from the initial thread makes any significant difference. The reason GUI updates are supposed to be done from the dispatcher thread is that, otherwise, there could be conflicts between updates from different threads. Until the main window is openned there's not usually any multi-threading happening. It's different in an applet environment where the JVM may be shared.

                                  2) Whether the initial thread opens the window, or schedules code to the dispatcher thread to open the window, it's usually ends at that point, once you have a functional GUI there no longer a "main thread" worthy of the name even if the thread doesn't terminate it just becomes another worker thread.
                                  • 14. Re: Thread.sleep(int millis) makes it transparent?
                                    Darryl Burke
                                    Thank you Malcolm.
                                    once you have a functional GUI there no longer a "main thread" worthy of the name
                                    That's why I was curious how you ...
                                    make it wait until the main window is closed and have it do the clean up
                                    thanks, Darryl
                                    1 2 Previous Next