aspose file tools*
The moose likes Beginning Java and the fly likes Array question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array question" Watch "Array question" New topic
Author

Array question

Brandt Charles
Ranch Hand

Joined: Apr 17, 2006
Posts: 57

I have an array of 5 JRadioButtons. I want a text field to display the integer value of the subscript. For example, if the Zeroth element radio button is selected, the text field will display 0. I'm not sure of the terminology, but is there a field associated with arrays that will yield this value? For example, the array is called radio, is there some sort of statement such as radio.VALUE_OF_ELEMENT? Or, is there a field corresponding to the string each radio button is initialized with? Each appears in the container numbered 0 through 4.

THIS IS NOT FOR HOMEWORK! I actually did the corresponding assignment, and wanted to spice it up a bit after the fact.


Green, green, green!
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Add the appropriate listener to listen for selection changes. When a selection change occurs iterate over the array until you find the index of the radio that has selection. Display that number.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Brandt Charles:
...is there a field associated with arrays that will yield this value? For example, the array is called radio, is there some sort of statement such as radio.VALUE_OF_ELEMENT? ...

There is no way to get an index number directly from an array element. This is why you would need to iterate through the array, as Ken suggests.
[ June 08, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Brandt Charles
Ranch Hand

Joined: Apr 17, 2006
Posts: 57

So here's what I came up with, resulting in an array out of bounds exception. There are 5 radio buttons in the radio button array. The variable j had been declared outside this class and initialized in the loop as 0. Be gentle:


What I was going for was if the counter j matched the element of the array, the appropriate value of j would be displayed in the text field focusBox. I don't know if it can be discerned why I'm getting the error from this snippet of code.

I had tried to use the inherited methods from AbstractButton including isSelected() and getText(), but kept getting compilation errors (symbol cannot be found). Doesn't JRadioButton have access to the methods it inherits from AbstractButton? Perhaps my syntax was wrong. With this approach I had tried in an if statement
for (int i = 0; i < radio.length; i++)
{
if (radio[i].isSelected())
{ ... }
}

And kept getting the compile error. The above is what I could best come up with from the gentle hint given before. Any insight into what needs to be change and/or blatant syntax problems?

I appreciate the help, and again, this assignment has been completed already without the JRadioButtons

Brandt
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
when you create the radioButtons, set the actionCommand as well

eg
for loop
rb[x] = new JRadioButton(...);
rb[x].setActionCommand(""+x);
[Button]group.add(rb[x])

then in your listener it's just

group.getSelection().getActionCommand()
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Michael Dunn:
when you create the radioButtons, set the actionCommand as well

eg
for loop
rb[x] = new JRadioButton(...);
rb[x].setActionCommand(""+x);
[Button]group.add(rb[x])

then in your listener it's just

group.getSelection().getActionCommand()


1. That should be Integer.toString(x) not ""+x.
2. It satifies the requirements iff the array is never altered. A seemingly innocuous change like altering the order at runtime would suddenly break it. In a naive example like this that's probably not a problem, but I prefer to avoid brittle code.
Brandt Charles
Ranch Hand

Joined: Apr 17, 2006
Posts: 57

An update:

I didn't realize I could use other listener classes with radio buttons other than ItemListener (please read my signature). When I used ActionListener the program worked in conjunction with a for loop and the radio button array. So now I have a question to build on the original post: for what purpose would I want to use the ItemListener class for event handling?

Incidentally, thanks for the suggestions thus far.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Array question