• Post Reply Bookmark Topic Watch Topic
  • New Topic

Jpane custom options + if statements  RSS feed

 
christopher temple
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK been reading Jpane stuff all morning, and reading through forums, and I can find if statements regarding the standard YES_NO_OPTION Jpanes, but what about custom option Jpanes? Here is what mine looks like at the moment.



I've tried reading various JPane threads, but they all deal with the standard Yes/No choices. I tried using statements like if(soundSelection.equals("oo"){do this..}, but that doesn't work, and I am assuming it is because soundSelection is an object and not a string, but it also won't let me change soundSelection to a string without getting all kinds of errors. I also tried soundSelection == "oo"{do this...) and some version of soundSelection.equals(possibleSelections("oo")) or something like that.

Does anyone have a link to tutorials that deal with JPanes that have custom selection, or should I be going about this in a totally different way? I guess I could just do an input dialog box and ask for the user to type in "th" "oo" or "at", that would be easier on my end, since I could assign that to String, but I wanted to have the buttons to make it easier on the user, where all they have to do is click.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I can see, showOptionDialog returns an int, not an Object. I believe the int is the position of the option selected.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JOptionPane.showOptionDialog(...) returns an int whose value is the index of the choice selected by the user.
 
christopher temple
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, so the part where it says possibleSelections[0] is the return? With int soundSelection th=0 oo=1 and at=2?
 
christopher temple
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, got it working now

For future reference, I did use the following afterwards:



 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's more straightforward to write:

 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be more straightforward, but it is not a good idea. The reason ints are returned is because that method was written before proper enumerated types were invented. That means the only sensible return types were numbers or Strings, rather as are used as arguments for add methods with BorderLayout. You can see the values used for Border Layout here. Now, you can always look up the values returned from the method of option pane: here, but it would be a bad idea to write the literal values.
  • 1: It may be confusing. You may not remember what 2 means, but CANCEL_OPTION makes it so obvious.
  • 2: Using numbers is error‑prone. If you get the wrong number, it may take some time at runtime before you realise you have a logic error, but you are going to get CANCELOPTION right when you write the code. It will either be right and obviously right, or you will suffer a compile‑time error.
  • I shall let you work out which of the two spellings is correct.

    So you write
    if (JOptionPane.showOptionDialog(...) == JOPtionPane.APPROVE_OPTION) ...
    … and its meaning will be obvious.
     
    Fred Kleinschmidt
    Bartender
    Posts: 571
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    But if you use showOptionDialog with your own options ( say, {"Joe", "Bill", "Mary", "Steve", "Pete"} ) then you don't have anything like JOPtionPane.APPROVE_OPTION to compare the answer to.
     
    Campbell Ritchie
    Marshal
    Posts: 56529
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Now, I don't think people would design that class the same way now that enumerated types are available. If you look at the documentation for JOptionPane#showOptionDialog, you find it returns int. You can show one of these options:
    DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION, or OK_CANCEL_OPTION
    and you can get a return value chosen from those, or
    CLOSED_OPTION if the user closed the dialog
    So you would have something like APPROVE_OPTION or YES_OPTION to compare with the result.

    Think how much better the class would have been with enumerated types for its return values.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!