Results 1 to 4 of 4
  1. #1
    mityay is offline Member
    Join Date
    Jun 2009
    Posts
    8
    Rep Power
    0

    Default Unexpected behavior Thread.sleep() is used within CardLayout

    I am trying to display two panels after each other using CardLayout. Here is a method:

    public void initComponents()
    {
    CardLayout cl = new CardLayout();
    setLayout(cl);
    add(welcomePanel, "Welcome");
    cl.show(this, "Welcome");
    try
    {
    Thread.sleep(3000);
    } catch (InterruptedException ie)
    {
    System.out.println("Caught " + ie.getClass());
    }
    System.exit(0);
    }

    From my perspective, the program should show welcomePanel for 3 seconds and then exit. On the contrary, no gui is ever displayed. Program runs for 3 seconds and exits. Is there any good explanation to this?

    Thanks,

    P.S. In case I comment out try-catch and System.exit(0) statements, welcomePanel is displayed as expected

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're calling Thread.sleep in the Event Dispatch Thread, or EDT. This is the single thread that allows Swing to draw the GUI and interact with the user, so when you put it to sleep you in essence put your whole app to sleep, which is obviously not your goal here. You should look into using a Swing Timer here instead. It would also be instructive for you to read up on Concurrency in Swing.

    Here's a simple demo of using a Timer with a CardLayout:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    
    import javax.swing.*;
    
    public class FuSwing1 {
    
      private static final String[] WEEK_DAYS = {
        "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
      private static final int TIMER_DELAY = 1000;
      private CardLayout cardlayout = new CardLayout();
      // create main JPanel and give it the CardLayout above
      private JPanel mainPanel = new JPanel(cardlayout);
      private Random random = new Random();
    
      public FuSwing1() {
        mainPanel.setPreferredSize(new Dimension(300, 200));
        for (String day : WEEK_DAYS) {
          
          // create a bunch of labels with days of the week and
          // random colors
          JLabel label = new JLabel(day, SwingConstants.CENTER);
          label.setOpaque(true);
          Color color = new Color(
              127 + random.nextInt(128), 
              127 + random.nextInt(128), 
              127 + random.nextInt(128));
          label.setBackground(color);
          // and add them to the main panel
          mainPanel.add(label, day);
        }
        
        // create a timer that cycles through the labels/cards
        new javax.swing.Timer(TIMER_DELAY, new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            cardlayout.next(mainPanel);
          }
        }).start();
        
      }
    
      public JPanel getMainPanel() {
        return mainPanel;
      }
      
      private static void createAndShowUI() {
        JFrame frame = new JFrame("CardLayout Demo");
        frame.getContentPane().add(new FuSwing1().getMainPanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
          }
        });
      }
    }
    Best of luck.
    Last edited by Fubarable; 06-28-2009 at 02:42 PM.

  3. #3
    mityay is offline Member
    Join Date
    Jun 2009
    Posts
    8
    Rep Power
    0

    Default

    Thank you! That was really helpful

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  2. Strange behavior with Thread.sleep()
    By Steve11235 in forum Advanced Java
    Replies: 16
    Last Post: 05-04-2009, 05:24 AM
  3. Sleep in thread
    By jithan in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 02:27 PM
  4. Can't get my thread to sleep!
    By jamesfrize in forum New To Java
    Replies: 2
    Last Post: 03-25-2008, 05:14 AM
  5. How to use the sleep and thread?
    By jiuhu in forum Java Applets
    Replies: 4
    Last Post: 08-07-2007, 02:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •