aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes using getSelection() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "using getSelection()" Watch "using getSelection()" New topic
Author

using getSelection()

Al Swearengen
Greenhorn

Joined: May 04, 2009
Posts: 5
what i'm trying to do is simple. i have one question presented in a JLabel and four possible answers indicated by radio buttons. those of you who saw my thread about getting the name of a container may remember. i call getSelection() on the buttonGroup but i'm not sure how to handle that the method returns. why does the method not return the button selected rather than a generic button model? how do i make my app understand that it's dealing with the actual button that's selected rather than a generic button model? i've read about programming a loop that puts all selected buttons in an array but i'm still a newb and i'm not sure how to go about that. shouldn't i be able to use the actual method included with buttonGroup rather than program a loop to do the same thing?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
What you are supposed to do is to add a Listener to each of the buttons.
A trip to the Java™ Tutorials will probably prove profitable. You should find radio buttons etc on the contents there.

Good luck with it.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you would normally use getSelection with getActionCommand

group.getSelection().getActionCommand();

but you have to set the action command, which, in your case,
would be the radioButton text, as your answer is text.

if you were to change your answer to an index number, the
action command would be "0", "1" etc, then the selection (...getActionCommand()),
converted to an int, would be compared to the answer (index #)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Else check out http://www.exampledepot.com/egs/javax.swing/radio_GetGroupCur.html


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Al Swearengen
Greenhorn

Joined: May 04, 2009
Posts: 5
Michael Dunn wrote:you would normally use getSelection with getActionCommand

group.getSelection().getActionCommand();

but you have to set the action command, which, in your case,
would be the radioButton text, as your answer is text.

if you were to change your answer to an index number, the
action command would be "0", "1" etc, then the selection (...getActionCommand()),
converted to an int, would be compared to the answer (index #)


that's what i'd thought i should do, as well. problem is, in the listener, i have the code:


and i get a null pointer exeption. here is the entire program:

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
You appear to be declaring your answers ButtonGroup object twice, first in the SharkQuiz class, and it is never constructed, and a second time in your Question constructor. Note that the two variables are not related, and while the one declared in the Question constructor has been fully initialized, the one in the SharkQuiz class hasn't. It's the latter one that you're testing, and due to the above, it will be null.

I recommend a more OOP approach here so that you can avoid hard-coding your questions. I'd have a non-GUI Question class that holds a Question String, a List<String> of possible answers, a true answer String, has a method for returning a random shuffling of answers (true and the bad answers), and has a boolean method to test that an answer is correct. I'd then use this class as a model for the JPanel that displays it. etc...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using getSelection()