Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JOptionPane.showOptionDialog

 
Jacob Coddaire
Ranch Hand
Posts: 36
Chrome Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys. I'm having trouble understanding the concept of this command. I'm creating a small game. Here's what I need to do.
1) Have a user specify how many players (2-4)
2) Have the user create names, or custom strings, for each player
3) Sort the names in an array
4) Create a dialog box that throws up the names and allows the user to select which will go first. The selection process should be either a JButton or radio button style, and should include the custom name set by the user.

For the record, steps one through three are done. My instructor said that this method would be the best for displaying the information I needed.

Here is what I figure:
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you having trouble understanding? The syntax?
 
Jacob Coddaire
Ranch Hand
Posts: 36
Chrome Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For some reason, it keeps getting this error message.
Exception in thread "main" java.lang.NullPointerException
at javax.swing.plaf.basic.BasicOptionPaneUI.addButtonComponents(Unknown Source)
at javax.swing.plaf.basic.BasicOptionPaneUI.createButtonArea(Unknown Source)
at javax.swing.plaf.basic.BasicOptionPaneUI.installComponents(Unknown Source)
at javax.swing.plaf.basic.BasicOptionPaneUI.installUI(Unknown Source)
at javax.swing.JComponent.setUI(Unknown Source)
at javax.swing.JOptionPane.setUI(Unknown Source)
at javax.swing.JOptionPane.updateUI(Unknown Source)
at javax.swing.JOptionPane.<init>(Unknown Source)
at javax.swing.JOptionPane.showOptionDialog(Unknown Source)
at Input.main(Input.java:60)

Here's my full code.

 
Jacob Coddaire
Ranch Hand
Posts: 36
Chrome Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't figure out how to make it show a list of names, or anything , for that matter.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be using a showInputDialog instead :


(there are some examples in the java tutorial)
 
Jacob Coddaire
Ranch Hand
Posts: 36
Chrome Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works, but it has a small defect. I can select a blank space when there is nothing there. Is there a way to work around this?
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob,

Did you see the tutorial? If so,



Your best bet would be to initialize the array once you find out how many players there will be. Then you can just use that array in the input dialog. Otherwise, you'll need to create another array once you figure out there's only 2 players or whatever.
 
Jacob Coddaire
Ranch Hand
Posts: 36
Chrome Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I got it to work. Sweet and thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic