How to check condition that 'only one jRadiobutton should be selected' - java

I am working on details entry form on NetBeans with Java Swings. There are three certain jRadiobutton, of them only one must be selected.
Any methods that could give such power, please suggest. Or might I need to custom my own method to perform check?

You have to create a object of ButtonGroup and add radio buttons to that object.
JRadioButton birdButton = new JRadioButton(birdString);
birdButton.setSelected(true);
JRadioButton catButton = new JRadioButton(catString);
//Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(birdButton);
group.add(catButton);

Related

Clear Selection of RadioButtons

My application: user wants the object to move left with a particular speed - he/she chooses one radiobutton from moveToLeft ButtonGroup. But he/she changes his/her mind and wants the object to move right - he/she chooses one radiobutton from moveToRight ButtonGroup.
My question is - how to clear buttongroup, that was chosen as first. I tried ClearSelection and setSelected(false), but it does not work. Here is my code:
JRadioButton vel1 = new JRadioButton("10 km/h");
JRadioButton vel2 = new JRadioButton("20 km/h");
JRadioButton vel3 = new JRadioButton("30 km/h");
JRadioButton vel4 = new JRadioButton("40 km/h");
ButtonGroup moveToRight = new ButtonGroup();
moveToRight.add(vel1);
moveToRight.add(vel2);
ButtonGroup moveToLeft = new ButtonGroup();
moveToLeft.add(vel3);
moveToLeft.add(vel4);
if(vel1.isSelected() || vel2.isSelected() )
{
moveToLeft.clearSelection();
//vel3.setSelected(false);
//vel4.setSelected(false);
}
if(vel3.isSelected() || vel4.isSelected() )
{
moveToRight.clearSelection();
//vel1.setSelected(false);
//vel2.setSelected(false);
}
The ButtonGroups are in two different panels.
What doesn't work? If the clearSelection(...) statement is executed, then it will work. If it doesn't work, then the statement is not executed.
The code you posted won't do anything. That code will be executed when the buttons are created and the frame isn't even visible yet, so obviously they will not yet be selected by the user.
...he/she chooses one radiobutton...
So then you need to add an ActionListener to the radio button to execute the relevant code.
Read the section from the Swing tutorial on How to Use Radio Buttons for more information and working examples.

display combox java in different JPanel

How I can display the same JCombox in different JPanel.
In my code it displays just in the last JPanel.
for (int i=1; i<=nb_client; i++) {
JPanel panel=new JPanel();
String titre="client"+i;
tabbedPane.add(titre, panel);
combox.setPreferredSize(new Dimension(100, 20));
panel.add(combox);
tabbedPane.validate();
}
how i can display the same Jcombox in diffrent Jpanel in my code it displays just in the last Jpanel
You can't.
A component can only have a single parent, so it will only ever display in the last panel you add the combo box to.
However you can share the model of the combo box:
JComboBox comboBox1 = new JComboBox(...);
JComboBox comboBox2 = new JComboBox( comboBox1.getModel() );
Now when you select an item in one it will also be selected in the other.
If you don't want this behaviour then you need to copy the data from one combo box to the other. So in this case you will need to write a loop. I'll let you write the loop to copy the data.

get the name of the selected radio button from button group

I want to return selected radio button name from button group in java :
ButtonGroup bg = new ButtonGroup();
bg.add(radiobutton1);
bg.add(radiobutton2);
bg.add(radiobutton3);
I think you can loop through the radio buttons, for each button if isSelected() is true then call button.getText() to get the name.
for (AbstractButton button : bg.getElements())
if (button.isSelected())
return button.getText();
Why do you want the button name? It isn't of much use. You can, however, get the all of the button references using bg.getElements(), and you can get the text of each of those references with button.getText(), both of which are of much more use than the name you gave the button. If you REALLY want the name, you can create the buttons and then call radiobutton1.setName("radiobutton1") and then use getName() on the reference.

JCheckBox List error?

I'm coding an enigma machine in java, and when the program launches, I have a JOptionPane appear with 5 JCheckBoxes for the user to select which rotors to use, and in which order.
My problem is, they get added to the popup, but they aren't actually displayed.
Instead I get a massive readout of all 5 checkboxes as if I called their toString method.
I have a few JLabels on the popup that display correctly, along with the OK button at the bottom.
My list is initialized like so:
private final List<JCheckBox> rotorCheckBox = Arrays.asList(new JCheckBox(
"Rotor 1"), new JCheckBox("Rotor 2"), new JCheckBox("Rotor 3"),
new JCheckBox("Rotor 4"), new JCheckBox("Rotor 5"));
I'm not sure why it does this, it worked as an array before, and I've been trying to convert it so I don't have to constantly call Arrays.asList() on it.
I've checked every use of it in my code, nothing is being called toString or creating errors relating to it being in a list.
How can I make it display correctly?
You're adding the List to the JOptionPane, you should add the JCheckBox's to a JPanel and use that instead
So, instead of something like...
JOptionPane.showMessageDialog(null, rotorCheckBox);
You should use something more like...
JPanel panel = new JPanel();
for (JCheckBox cb : rotorCheckBox) {
panel.add(cb);
}
JOptionPane.showMessageDialog(null, panel);
as an example

Change the way JRadioButtons displayed on JPanel [duplicate]

This question already has an answer here:
How can I use BoxLayout to do this?
(1 answer)
Closed 9 years ago.
I have a JPanel, and I want to add JRadioButtons to it, this is the code I tried :
private void populateQuestionnaire(Question question){
buttonGroup = new ButtonGroup();
for(Choix c : question.getListChoix()) {
radioButton = new JRadioButton(c.getChoixLibelle());
buttonGroup.add(radioButton);
jPanel1.add(radioButton);
}
jPanel1.revalidate();
jPanel1.repaint();
}
And I have the layout of JPanel is FlowLayout.
This is how the JRadioButtons displayed :
I want JRadioButtons to be added one below the other and to be centered in the JPanel.
Instead of using a FlowLayout, which lays out items left to right and wraps appropriately, you can use a BoxLayout, which allows you to specify laying out items either horizontally or vertically.
You can set the LayoutManager for your JPanel at construction:
JPanel jpanel1 = new JPanel(new BoxLayout(parentComponent, BoxLayout.Y_AXIS));
BoxLayout is great for stacking elements on top of each other. Consider this code:
public class MyFrame extends JFrame {
public MyFrame() {
ButtonGroup bg = new ButtonGroup();
JRadioButton b1 = new JRadioButton("My Button 1");
JRadioButton b2 = new JRadioButton("My Button 2");
bg.add(b1);
bg.add(b2);
BoxLayout bl = new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS);
this.setLayout(bl);
b1.setAlignmentX(CENTER_ALIGNMENT);
b2.setAlignmentX(CENTER_ALIGNMENT);
this.add(b1);
this.add(b2);
}
}
Which makes, when instantiated and shown, the following window:
Now let's look at how this code works:
Consider this code:
ButtonGroup bg = new ButtonGroup();
JRadioButton b1 = new JRadioButton("My Button 1");
JRadioButton b2 = new JRadioButton("My Button 2");
bg.add(b1);
bg.add(b2);
This code does the same thing you were doing before, only a little simpler for example's sake. It creates a button group and two JRadioButtons, then adds the buttons to the button group. Now here is when it gets interesting.
Next, consider this code:
BoxLayout bl = new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS);
this.setLayout(bl);
The first line creates a new BoxLayout with the following parameters:
1 The container which it is laying out. (It needs this because it can't be shared.)
2 The axis which it should be laying out components. (You want Y-AXIS for your case.)
The second line set's the JFrame's contentPane's layout to the BoxLayout you just created.
Finally, consider this code:
b1.setAlignmentX(CENTER_ALIGNMENT);
b2.setAlignmentX(CENTER_ALIGNMENT);
this.add(b1);
this.add(b2);
This sets the alignment of the two radio buttons so that their centers will be aligned to each other and to the center of the frame. Then it adds them to the frame's content pane.
Hope this helped!
Note: The reason I used this.getContentPane() while constructing the BoxLayout instead of just using this is because when working with JFrames commands like add() and setLayout() get redirected to the frame's content pane. So if we were to use this in the constructor, when we called this.setLayout(bl) we really would be calling this.getContentPane().setLayout(bl). But, we just told the BoxLayout it'll be laying out the frame, not it's content pane, so you'll get an exception saying that the BoxLayout can't be shared. To correct the error, we just need to realise that we are actually working with the content pane through the frame's methods, and update the BoxLayout's constructor accordingly to let it know what it really is laying out.

Categories