Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: dispose()'ing a JFrame and replacing it

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default dispose()'ing a JFrame and replacing it

    so, im trying to catch a cancel button click from a JOptionPane, then send the exception up to the window controller, have it destroy the JFrame the exception came from, then start over with the main JFrame of the program.

    as that's probably convoluted, i'll post some code.

    //from inside the JFrame
    try
    {
    	difficulty = (String) JOptionPane.showInputDialog(null, "What difficulty would you like?", "Game",
    			JOptionPane.QUESTION_MESSAGE, null, dificultyChoices, dificultyChoices[0]);
    }
    catch(HeadlessException ex)
    {
    	//wc is a window controller
    	wc.canceled(this);
    }
    //from the window controller
    public void canceled(JFrame callingWindow)
    {
    	callingWindow.dispose();
    	//createWindow() draws the window i want to replace callingWindow
    	createWindow();
    }

    the problem with this is that the original JFrame goes right ahead executing its next line of code. but, of course, since it's just been dispose()'d, it throws a NullPointerException and crashes.

    how should i actually be implementing this?


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: dispose()'ing a JFrame and replacing it

    Usually control is returned from the JOptionPane with a result that the calling frame uses to decide what to do next - processA, processB, or close. IOW the action is taken by the calling frame.

    Without seeing more of the code it's hard to make a suggestion, but you could just hide the frame and dispose of it later - or re-use it for something else.

    Ultimately, I guess there shouldn't be another line of code after the disposal...
    void method() {
       try {
       option = getOptionPaneResult
       if (option==1) 
         do process1
       else if (option==2)
         do process2
       else
         dispose
       } 
       catch (Exception e) {
         dispose
       }
    }
    Last edited by dlorde; May 14th, 2010 at 10:45 AM. Reason: .

  3. #3
    Junior Member
    Join Date
    May 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: dispose()'ing a JFrame and replacing it

    thanks for the reply.

    as it stands, ive revised it a little, and heres the code ive got:

    http://musasabi.h4xful.net/misc/home...manproject.zip

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: dispose()'ing a JFrame and replacing it

    I added a little to my last post - didn't think you'd be back so quick

  5. #5
    Junior Member
    Join Date
    May 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: dispose()'ing a JFrame and replacing it

    haha. yeah, i was talking to my professor yesterday, and since my current revision has an "if the user hits cancel, dispose of the window", line, he said i could just wrap everything else in an else block, ugly though that may be. i figure if thats the solution, the flow of the code needs to be reworked, though.

  6. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: dispose()'ing a JFrame and replacing it

    Sadly, I don't have time to search through your project for the relevant code. If you feel that an 'if' or 'else' block is too big, replace it with a method call, so all that code is in another method. That way things stay readable:
    ...
    choice = getOptionPaneChoice()
    if (choice == quit) {
       dispose()
    }
    else {
      doChoice(..)    // call code
    }
    ...
     
    // method to do the stuff
    void doChoice(..) {
       ... // all the messy code
    }
    Last edited by dlorde; May 14th, 2010 at 04:05 PM.

Similar Threads

  1. Print out a JFrame
    By ellias2007 in forum AWT / Java Swing
    Replies: 8
    Last Post: June 17th, 2010, 06:15 AM
  2. JPanel in JFrame
    By maele in forum AWT / Java Swing
    Replies: 2
    Last Post: March 8th, 2010, 04:12 AM
  3. lookandfeel() cannot be applied to JFrame?
    By emigrant in forum AWT / Java Swing
    Replies: 1
    Last Post: February 15th, 2010, 12:05 PM
  4. JFrame help
    By Uden in forum AWT / Java Swing
    Replies: 0
    Last Post: August 14th, 2009, 01:37 PM
  5. Replies: 3
    Last Post: February 26th, 2009, 05:21 PM