Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Jpane custom options + if statements

 
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys, got it working now

For future reference, I did use the following afterwards:



 
Knute Snortum
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's more straightforward to write:

 
Marshal
Posts: 76468
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 732
    10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 76468
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Get out of my mind! Look! A tiny ad!
    Garden Master Course kickstarter
    https://coderanch.com/t/754577/Garden-Master-kickstarter
    reply
      Bookmark Topic Watch Topic
    • New Topic