Results 1 to 4 of 4
  1. #1
    nish.singha20 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default 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?

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

    Default 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.

  3. #3
    nish.singha20 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default 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.

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

    Default Re: Calling another JFrame from a JFrame through a JButton...(Help Reqired)

    Quote Originally Posted by nish.singha20 View Post
    Thanks Fubarable for the reply! Actually, using Timer will create new thread that will obviously execute in parallel with the main thread.
    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).

    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.
    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.

    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 06:21 AM.

Similar Threads

  1. Calling one Jframe from another
    By sandeep43 in forum New To Java
    Replies: 21
    Last Post: 05-05-2012, 02:48 PM
  2. Adding JButton to JFrame with background
    By bzknight in forum AWT / Swing
    Replies: 1
    Last Post: 01-19-2011, 07:55 PM
  3. How To Open a JFrame Form from a jButton
    By Lyricid in forum AWT / Swing
    Replies: 12
    Last Post: 03-28-2010, 07:53 AM
  4. Link between two JFrame using JButton
    By Ravi Ranjan in forum NetBeans
    Replies: 2
    Last Post: 06-09-2009, 11:30 PM

Posting Permissions

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