Results 1 to 16 of 16
Like Tree1Likes
  • 1 Post By Tolls

Thread: JoptionPane Cancel and 'X' button confusion

  1. #1
    IanKW is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default JoptionPane Cancel and 'X' button confusion

    Okay i have done some research and have found that when using the JOptionPane, when the cancel or x button is clicked, the value is returned as null for the particular JOptionPane.

    It does not seem to work when I try to code it though, I have tried the following:

    Java Code:
    	public static int getBoardType()		
    		{
    			int boardType = 0;				
    			String boardInput;
    			
    			 boardInput = JOptionPane.showInputDialog(null,										 
    					   "Enter 1 (LongBoard), 2 (Shortboard), or 3 (BoogieBoard)." ,
    					   "Dwelling Type",
    					   JOptionPane.QUESTION_MESSAGE);
    			 
    			 boardType = Integer.parseInt(dwellingInput);								
    			
    if ((boardType < 1) || (boardType > 3))									{
    				JOptionPane.showMessageDialog(null,
    					"The value for dwelling type must be 1 (Longboard) 2 (Shortboard), or 3 (Boogieboard)",
    					"Dwelling Type Error", 
    					JOptionPane.ERROR_MESSAGE);
    				getBoardType();
    			}
    			
    			if (boardInput == null)
    			{
    				exit();
    			}
    				
    			return boardType;														}

    Where exit() is coded to this:

    Java Code:
    public static void exit()														//exit application method when finished
    {
    	System.exit(-1);
    }

    If the "X" button or the Cancel button does in fact return a null value, why is the way I have this set up not working?

    Thanks!

    Ian

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: JoptionPane Cancel and 'X' button confusion

    Stick some debug printlns() in there, showing what boardInput is.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    IanKW is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: JoptionPane Cancel and 'X' button confusion

    Im not sure I understand what you mean, how would println help to elicit a reaction from clicking x or cancel? (btw this is just a snippet of my code...the program on a whole works, the only two functions I am still confused with are the cancel and x button actions)....

    I have boardType returned to the main method and the input is tested using a switch statement. So it will print "you should get a" + boardType. That part of this works...but still...cancel and x button.

    As far as I know, the println just prints what you tell it to, so how does that help? confused! (ps and yes I am a newbie still so maybe there is more to this println than I am aware of)

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: JoptionPane Cancel and 'X' button confusion

    how would println help
    It will help you see what the code is doing so you can change the code to handle it.

    It's called "debugging the code". debugging code is a major part of programming. The sooner you learn it, the better it will be.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: JoptionPane Cancel and 'X' button confusion

    Debugging is all about seeing what values you are getting back in reality.
    You have a theory that the value you are getting from the JOptionPane is null, so prove it.
    Use Sys.out.println() to show what the boardInput is.
    In fact, you probably should use that to print out every variable as they are set/changed/used.
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    IanKW is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: JoptionPane Cancel and 'X' button confusion

    proof= :

    Java Code:
    Exception in thread "main" java.lang.NumberFormatException: null

  7. #7
    IanKW is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: JoptionPane Cancel and 'X' button confusion

    hat is how I knew it without using a println. So why is println still needed (yes I know what debugging is and have used it, but the things I am finding during my debugging do not add up)

  8. #8
    IanKW is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: JoptionPane Cancel and 'X' button confusion

    ps I have also tried input for string as "" but i still get exceptions on cancel or x

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: JoptionPane Cancel and 'X' button confusion

    Quote Originally Posted by IanKW View Post
    proof= :

    Java Code:
    Exception in thread "main" java.lang.NumberFormatException: null
    Well, you didn't say that, did you?
    And the answer is, you cannot parse a null String object into an int.

    You have a check for null, the problem is it is several lines after you attempt to parse the object.

    ETA:
    ACtually, you're parsing a completely different variable:
    Java Code:
    boardType = Integer.parseInt(dwellingInput);
    Where's 'dwellingInput' coming from?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    IanKW is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: JoptionPane Cancel and 'X' button confusion

    that should be boardInput.

    ...so I should put my test between these two statements?

    Java Code:
               
                 boardInput = JOptionPane.showInputDialog(null,                                      
                           "Enter 1 (LongBoard), 2 (Shortboard), or 3 (BoogieBoard)." ,
                           "Dwelling Type",
                           JOptionPane.QUESTION_MESSAGE);
    
                 boardType = Integer.parseInt(boardInput);

    I try and check the string for null with an if statement before but it wont let me do that as it throws error cannot convert string to boolean

    Java Code:
    if {boardInput ="" || boardInput = null)
    			 {
    				 exit();
    			 }

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: JoptionPane Cancel and 'X' button confusion

    You compare with ==, not =, for the null check.
    And you should use .equals() for the check against an empty string...or just the isEmpty() method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    IanKW is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: JoptionPane Cancel and 'X' button confusion

    Okay I figured it out, had to use the statement I already had, just before the value was parsed. and the isEmpty() worked as well... I didnt know that existed before 5 mins ago. thank you

  13. #13
    IanKW is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: JoptionPane Cancel and 'X' button confusion

    now here is another question, lets say I have an error message pop up if user picks an int outside of the 1 to 3 range, so say i pick 20. I have it coded to pop up an error message, i then click OK, after I click ok it walks me back through the method but when I get to recommendation it recommends I get a null... why does that happen?

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: JoptionPane Cancel and 'X' button confusion

    I get a null... why does that happen?
    Because there is a bug in the logic.
    What variable has the null value? Why doesn't it have a valid value?
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: JoptionPane Cancel and 'X' button confusion

    I don't understand all the trouble here: when you press 'ok', the dialog returns the string entered; when you press 'cancel' or the 'X' button, the dialog returns null. You have to test that and act accordingly; of course trying to convert null to an int fails and throws an exception.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  16. #16
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    11

    Default Re: JoptionPane Cancel and 'X' button confusion

    you should post your complete code with code tag.
    null condition must be checked everywhere to prevent issues.
    Ramya:cool:

Similar Threads

  1. Replies: 1
    Last Post: 12-14-2011, 10:23 PM
  2. Cancel button ? In java
    By smasm in forum New To Java
    Replies: 21
    Last Post: 10-01-2011, 06:24 AM
  3. Replies: 5
    Last Post: 08-15-2011, 06:10 PM
  4. Cancel button for exiting app
    By phearless in forum AWT / Swing
    Replies: 1
    Last Post: 01-13-2011, 07:20 PM
  5. Struts 2 cancel button
    By kirtichopra2003 in forum Advanced Java
    Replies: 0
    Last Post: 09-29-2009, 03:40 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
  •