I have a jframe that has a lable and two radiobuttons.
I use spring layout, But my second radioButton seen in top left of page!
public class tester extends JFrame {
public tester() {
add(create());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 500);
setVisible(true);
}
public JPanel create() {
JPanel panel = new JPanel();
ButtonGroup group = new ButtonGroup();
JRadioButton r1 = new JRadioButton("Yes");
JRadioButton r2 = new JRadioButton("No");
group.add(r1);
group.add(r2);
JLabel lable = new JLabel("Today is sunday?");
panel.add(lable);
// panel.add(group); // How add this?
panel.add(r1);
panel.add(r2);
JButton savebt= new JButton("Save");
JButton cancelbt=new JButton("Cancell");
panel.add(savebt);
panel.add(cancelbt);
panel.setLayout(new SpringLayout());
SpringUtilities.makeCompactGrid(panel, 1, 3, 50, 100, 25, 50);
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new tester();
}
});
}
}
Now this exception occur:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: No such child: 5
I want to display my two buttons on below of radio button's line!
There are three items in your panel, so the number of columns should be 3:
SpringUtilities.makeCompactGrid(panel, 1, 3, 50, 100, 25, 50);
// panel.add(group); // How add this?
You don't need this. ButtonGroups don't get added to a panel. They are for button selection management and are not displayed.