Results 1 to 13 of 13
  1. #1
    musasabi is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default dispose()'ing a frame from itself.

    EDIT: im trying to close one JFrame while having my window controller draw another to replace it. i've revised my code from the original implementation, and it "works", but throws a null pointer exception. here's the revised bit:

    Java Code:
    //from the frame to be closed
    //get the difficulty from the user
    difficulty = (String) JOptionPane.showInputDialog(null, "What difficulty would you like?", "Game",
    			JOptionPane.QUESTION_MESSAGE, null, dificultyChoices, dificultyChoices[0]);
    		
    //if the user presses the cancel button, present them with the main menu
    if(difficulty == null)
    	wc.canceled(this);
    Java Code:
    //from the window controller
    public void canceled(JFrame callingWindow)
    {
    	callingWindow.dispose();
    	//createWindow() draws the window i want to replace callingWindow
    	createWindow();
    }
    when i run that and click that particular cancel button, eclipse gives me

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

    with the first location being the very next line of code after wc.canceled(this);. it also cites two other places in my code and a couple dozen in the swing and awt libraries.

    the odd thing, though, is that it works until i go to actually start the game.

    whats actually happening?

    -----------------original post-----------------

    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.

    Java 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);
    }
    Java Code:
    //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?
    Last edited by musasabi; 05-13-2010 at 08:27 AM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Why are you catching a HeadlessException instead of reading the return value of showInputDialog which tells you if the user clicked cancel or not?

  3. #3
    musasabi is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default

    i thought that was how you handled the cancel button, obviously. =P

    i'll mess around with the string but ive got a feeling it'll still try to execute code after ive destroyed the window.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by musasabi View Post
    i thought that was how you handled the cancel button, obviously. =P
    The returned value won't help either, my bad.

    Better make a CustomDialog and setDefaultCloseOperation on it.
    See here.

  5. #5
    musasabi is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default

    alright, ive been trying to figure out how to make a custom dialog to handle the cancel buttons, but it hasnt been going well.

    any help, or should i just go to my prof with this? haha.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Post what you have for your custom dialog and let's see where it's going wrong.
    You can simply add a button yourself labeled cancel and put an ActionListener (or better set an Action) on it to do your cancel logic.

  7. #7
    musasabi is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default

    i just finished updating my original post.

    if you think im better off trying to create a custom dialog still, i'll have at that. =)

    btw, thanks for the help already. every step of this is educational.

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Yes, you are better off creating a custom dialog for this. It's not that hard. See the example code in the link I posted ealier.

  9. #9
    musasabi is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default

    ok, so far, this is what ive got.

    Java Code:
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    public class CustomDialog extends JDialog
    {
    	private WindowController wc = null;
    	private JFrame parentWindow = null;
    	
    	public CustomDialog(WindowController wc, JFrame parentWindow)
    	{
    		this.wc = wc;
    		this.parentWindow = parentWindow;
    		
    		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    		addWindowListener(new WindowAdapter()
    		{
    			public void windowClosing(WindowEvent we)
    			{
    				wc.canceled(parentWindow);
    			}
    		});
    	}
    }
    two issues, aside from potentially doing it wrong: one, i havent the slightest clue how to implement this as i had been implementing the input dialogs (with list boxes/etc). two, eclipse tells me that referring to wc and parentWindow inside the constructor for CustomDialog requires i declare the parameters as final. im certain that this actually means im doing something wrong, so i've left the auto-corrections out of this posting.

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by musasabi View Post
    ..
    two issues, aside from potentially doing it wrong: one, i havent the slightest clue how to implement this as i had been implementing the input dialogs (with list boxes/etc). ..
    You can pass the data through the constructor of the dialog and use it to create the JOptionPane that you would then set at the Dialog's content pane (see the example I posted).


    Quote Originally Posted by musasabi View Post
    .. two, eclipse tells me that referring to wc and parentWindow inside the constructor for CustomDialog requires i declare the parameters as final. im certain that this actually means im doing something wrong, so i've left the auto-corrections out of this posting.
    It's actually refering to wc in the windowClosing event of the Anonymous inner class that would complain. You can simply make it final.

  11. #11
    musasabi is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default

    ok, i believe i implemented what you suggested.

    Java Code:
    package hangman;
    
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    public class CustomDialog extends JDialog
    {
    	private WindowController wc = null;
    	private JFrame parentWindow = null;
    	
    	public CustomDialog(final WindowController wc, final JFrame parentWindow)
    	{
    		this.wc = wc;
    		this.parentWindow = parentWindow;
    		
    		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    		addWindowListener(new WindowAdapter()
    		{
    			public void windowClosing(WindowEvent we)
    			{
    				wc.canceled(parentWindow);
    			}
    		});
    	}
    	
    	public String getInput(String message, String title, int style)
    	{
    		return JOptionPane.showInputDialog(parentWindow, message, title, style);
    	}
    }
    i tried to implement this like String something = customDialog.getInput("something",...);.

    when i click cancel, though, it crashes with a null pointer exception and no redrawn main menu.

    sorry if im just not getting your intentions. im certainly trying, haha.

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    From the example I posted, you will find
    Java Code:
           //Create the JOptionPane.
            optionPane = new JOptionPane(array,
                                        JOptionPane.QUESTION_MESSAGE,
                                        JOptionPane.YES_NO_OPTION,
                                        null,
                                        options,
                                        options[0]);
    
            //Make this dialog display it.
            setContentPane(optionPane);
    I don't see that in your code. About the N.P.E, you are passing valid values when actually creating an instance of the CustomDialog, right?

  13. #13
    musasabi is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default

    i re-read over the example code you posted (and the corresponding java tutorial) after getting some sleep (it was 3:30am for me when i we were having that last exchange). in the DialogDemo example, they're testing against a null string return, even with the custom dialog. at least thats what i got from it.

    what im getting at that i only have a vague notion of what im actually trying to do here. i think i know the objective, but enough pieces are missing from the route that im not sure im even headed in the right direction.

    here's my source code/etc for the time being.

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

    its comically lengthy for a hangman game, but thats how it goes when you try to have real "features".

Similar Threads

  1. dispose() does not work here
    By arunkumarinfo in forum NetBeans
    Replies: 5
    Last Post: 12-28-2013, 05:42 PM
  2. dispose
    By simontkk2005 in forum AWT / Swing
    Replies: 3
    Last Post: 11-18-2009, 11:42 PM
  3. Replies: 7
    Last Post: 11-02-2009, 06:31 PM
  4. dispose all using Vector ?
    By sysout in forum New To Java
    Replies: 10
    Last Post: 08-31-2009, 04:51 PM
  5. How to dispose tooltip.
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 07-22-2008, 07:06 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
  •