|
-
Oct 28th, 2008, 10:05 AM
#1
Thread Starter
Member
Determining RadioButton Selected Tabbed Panes?
Hello,
I need some guidance as to how I can determine which RadioButton or RadioButtons was/were selected if some are grouped together on several tabbed panes?
I have already created my tabbed pane GUI, in which my class implements ActionListener, and my (empty) actionPerformed function is provided.
However, what do I put in this function to check each RadioButton? Do I use a for loop as in the example below? This is not easy for me to understand. Any assistence will be appreciated. Thanks in advance.
Code:
// This method returns the selected radio button in a button group
public static JRadioButton getSelection(ButtonGroup group) {
for (Enumeration e=group.getElements(); e.hasMoreElements(); ) {
JRadioButton b = (JRadioButton)e.nextElement();
if (b.getModel() == group.getSelection()) {
return b;
}
}
return null;
}
-
Oct 28th, 2008, 04:52 PM
#2
Re: Determining RadioButton Selected Tabbed Panes?
Doesn't your code basically do the following?
Code:
public static JRadioButton getSelection(ButtonGroup group) {
return group.getSelection();
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 28th, 2008, 06:26 PM
#3
Thread Starter
Member
Re: Determining RadioButton Selected Tabbed Panes?
Thanks for your reply ComputerJy. The radioButtons in my program allows the user to select items from a restaurant menu. Once selected, I need to calculate the total cost for the items selected. So I am stuck at the very beginning trying to determine whether any particular button was selected.
Here is a code snippet from the program: ( 'group' refers to a ButtonGroup of 5 Radio Buttons)
Code:
// create the submit and cancel buttons
// and add them to tabbed pane 1
JPanel submitPanel = new JPanel();
JButton submitButton = new JButton("Submit Order");
submitPanel.add(submitButton);
JButton cancelButton = new JButton("Cancel Order");
submitPanel.add(cancelButton);
tp1.add(submitPanel);
// add action listeners to the buttons
submitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
group.getSelection().getActionCommand();
if (group.isSelected())
System.out.println("Radio Button was selected");
}
});
However, this snippet keeps giving me the following error message:
isSelected(javax.swing.ButtonModel) in javax.swing.ButtonGroup cannot be applied to ()
Also I was thinking about using the enhanced for loop but I am not sure how to use it in this example.
-
Oct 28th, 2008, 07:33 PM
#4
Re: Determining RadioButton Selected Tabbed Panes?
since you're not using it correctly, it'll keep throwing the compile error.
The isSelected method must only be used to check if a certain ButtonModel in this group is selected
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 28th, 2008, 07:45 PM
#5
Thread Starter
Member
Re: Determining RadioButton Selected Tabbed Panes?
 Originally Posted by ComputerJy
since you're not using it correctly, it'll keep throwing the compile error.
The isSelected method must only be used to check if a certain ButtonModel in this group is selected
Ok. I have tried to select one Radio Button from the group, like this:
if(group.firstRadioButton.isSelected())
{
JOptionPane.showMessageDialog(null,"This first Radio Button was selected");
}
However, I keep getting the following error message:
cannot find symbol: variable firstRadioButton
-
Oct 29th, 2008, 12:43 AM
#6
Re: Determining RadioButton Selected Tabbed Panes?
Have you ever been to an object oriented course before?
group.isSelected(firstRadioButton);
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|