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;
}
Re: Determining RadioButton Selected Tabbed Panes?
Doesn't your code basically do the following?
Code:
public static JRadioButton getSelection(ButtonGroup group) {
return group.getSelection();
}
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:
Quote:
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.
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
Re: Determining RadioButton Selected Tabbed Panes?
Quote:
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:
Quote:
if(group.firstRadioButton.isSelected())
{
JOptionPane.showMessageDialog(null,"This first Radio Button was selected");
}
However, I keep getting the following error message:
Quote:
cannot find symbol: variable firstRadioButton
Re: Determining RadioButton Selected Tabbed Panes?
Have you ever been to an object oriented course before?
group.isSelected(firstRadioButton);