|
|
Re: What's the name of that group box thingamajig
|
Posted: Dec 3, 2004 6:00 AM
|
|
Java works differently.
1. Create a JPanel. 2. Select a TitledBorder for that panel. 3. Put your options (Radiobuttons, JToggleButtons) in this panel.
So much for the optical part.
Now to the logic: I guess you want that just one button should be selected at one time. 1. Create a JButtonGroup. 2. Assign this JButtonGroup to all your RadioButtons.
Here is an example created with NetBeans IDE:
public class MyFrame extends javax.swing.JFrame {
public MyFrame() {
initComponents();
}
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
buttonGroup1 = new javax.swing.ButtonGroup();
jPanel1 = new javax.swing.JPanel();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
getContentPane().setLayout(null);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jPanel1.setLayout(new java.awt.GridBagLayout());
jPanel1.setBorder(new javax.swing.border.TitledBorder("Some title"));
jRadioButton1.setSelected(true);
jRadioButton1.setText("jRadioButton1");
buttonGroup1.add(jRadioButton1);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
jPanel1.add(jRadioButton1, gridBagConstraints);
jRadioButton2.setText("jRadioButton2");
buttonGroup1.add(jRadioButton2);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
jPanel1.add(jRadioButton2, gridBagConstraints);
jRadioButton3.setText("jRadioButton3");
buttonGroup1.add(jRadioButton3);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
jPanel1.add(jRadioButton3, gridBagConstraints);
getContentPane().add(jPanel1);
jPanel1.setBounds(20, 20, 130, 150);
pack();
setSize (640,480);
}
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
new MyFrame().show();
}
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JPanel jPanel1;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
}
You don't need to create the panel containing the buttons, the ButtonGroup works without it, too.
|
|