Results 1 to 4 of 4
- 11-26-2011, 08:15 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
Calling another JFrame from a JFrame through a JButton...(Help Reqired)
I have a JFrame(say, JFrame A) that consists of a JButton. This action calls another JFrame(say, JFrame B). B consists of a method that has Thread.sleep(2000) statement. Now what happens is that, when i click the button, only the window for B appears and no contents are displayed. The Thread.sleep() statement is executed first and then rest of the components in the JFrame appear. It happens inspite of the fact that all the components of B are setVisible in the constructor of B itself.
Can anyone help me please?
-
Re: Calling another JFrame from a JFrame through a JButton...(Help Reqired)
Never call Thread.sleep(...) on the main Swing thread called the Event Dispatch Thread or EDT since doing this will put all Swing drawing (including the drawing of the Swing components) and user interactions to sleep. You should use a Swing Timer for this instead.
- 11-27-2011, 05:11 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
Re: Calling another JFrame from a JFrame through a JButton...(Help Reqired)
Thanks Fubarable for the reply! Actually, using Timer will create new thread that will obviously execute in parallel with the main thread. But that's exactly what I didn't wait. I want the main thread to hold execution for some time, change a Swing component, and then perform other functions.
-
Re: Calling another JFrame from a JFrame through a JButton...(Help Reqired)
That's sort of true. The Timer will call Thread.sleep() inside of a background thread, but the code in the Timer's ActionListener will all be called on the main Swing event thread, the Event Dispatch Thread (or EDT).
Regardless, you never want to call Thread.sleep(...) on the EDT, ever. In all likelihood, there are other ways of doing what you want to do such as disabling your GUI but not putting it to sleep. For a more detailed answer, you may wish to provide more details on your problem.But that's exactly what I didn't wait. I want the main thread to hold execution for some time, change a Swing component, and then perform other functions.
Edit: perhaps all you need is a modal dialog such as provided by a JOptionPane or a JDialog set to modal rather than using a second JFrame. Again more information will tell us if this is so.Last edited by Fubarable; 11-27-2011 at 05:21 AM.
Similar Threads
-
Calling one Jframe from another
By sandeep43 in forum New To JavaReplies: 21Last Post: 05-05-2012, 01:48 PM -
Adding JButton to JFrame with background
By bzknight in forum AWT / SwingReplies: 1Last Post: 01-19-2011, 06:55 PM -
How To Open a JFrame Form from a jButton
By Lyricid in forum AWT / SwingReplies: 12Last Post: 03-28-2010, 06:53 AM -
Link between two JFrame using JButton
By Ravi Ranjan in forum NetBeansReplies: 2Last Post: 06-09-2009, 10:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks