Results 1 to 4 of 4
- 06-28-2009, 08:32 AM #1
Member
- Join Date
- Jun 2009
- Posts
- 8
- Rep Power
- 0
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
-
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:
Best of luck.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(); } }); } }Last edited by Fubarable; 06-28-2009 at 02:42 PM.
- 06-28-2009, 06:08 PM #3
Member
- Join Date
- Jun 2009
- Posts
- 8
- Rep Power
- 0
Thank you! That was really helpful
-
Similar Threads
-
Difference between Thread.yield() and Thread.sleep() methods
By Nageswara Rao Mothukuri in forum New To JavaReplies: 12Last Post: 07-30-2010, 05:37 PM -
Strange behavior with Thread.sleep()
By Steve11235 in forum Advanced JavaReplies: 16Last Post: 05-04-2009, 05:24 AM -
Sleep in thread
By jithan in forum New To JavaReplies: 1Last Post: 08-27-2008, 02:27 PM -
Can't get my thread to sleep!
By jamesfrize in forum New To JavaReplies: 2Last Post: 03-25-2008, 05:14 AM -
How to use the sleep and thread?
By jiuhu in forum Java AppletsReplies: 4Last Post: 08-07-2007, 02:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks