I'm new to JFrame, I was trying to populate dynamic checkboxes with a scroll pane. I tried with some code as seen below. My Focus is:
- Get the selected dynamic checkbox's name.
- While I click no (Radio button) the check box panel is set to disabled/frozen/uncheckable.
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollBar;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import net.miginfocom.swing.MigLayout;
public class JFrameMain extends JFrame {
private JPanel contentPane;
private JButton btnOkay;
private JTextField textField;
private JRadioButton yes;
private JRadioButton no;
// Launch the application.
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrameMain frame = new JFrameMain();
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
// Create the frame.
public JFrameMain() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 556, 383);
contentPane = new JPanel();
contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(null, "Enetr the class", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel.setBounds(44, 23, 443, 53);
contentPane.add(panel);
panel.setLayout(null);
yes = new JRadioButton("yes");
yes.setSelected(true);
yes.setBounds(144, 20, 64, 25);
panel.add(yes);
yes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(yes.isSelected())
{
no.setSelected(false);
textField.setEnabled(true);
}
}
});
no = new JRadioButton("No");
no.setBounds(212, 20, 64, 25);
panel.add(no);
no.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(no.isSelected())
{
yes.setSelected(false);
textField.setEnabled(false);
}
}
});
btnOkay = new JButton("Okay");
btnOkay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Text field
System.out.println(textField.getText());
}
});
btnOkay.setBounds(120, 281, 97, 25);
contentPane.add(btnOkay);
JPanel panelCheckBox = new JPanel();
panelCheckBox.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
panelCheckBox.setBounds(44, 89, 443, 90);
contentPane.add(panelCheckBox);
panelCheckBox.setLayout(new MigLayout("", "[]", "[]"));
int numberCheckBox = 10;
JCheckBox[] checkBoxList = new JCheckBox[numberCheckBox];
for(int i = 0; i < numberCheckBox; i++) {
checkBoxList[i] = new JCheckBox("CheckBox" + i);
panelCheckBox.add(checkBoxList[i]);
}
JScrollBar scrollBar = new JScrollBar();
scrollBar.setBounds(492, 89, 21, 90);
contentPane.add(scrollBar);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new TitledBorder(null, "Enter Class", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_1.setBounds(44, 192, 443, 66);
contentPane.add(panel_1);
panel_1.setLayout(null);
textField = new JTextField();
//textField.setEnabled(false);
textField.setBounds(55, 23, 336, 22);
panel_1.add(textField);
textField.setColumns(10);
JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
btnCancel.setBounds(263, 281, 97, 25);
contentPane.add(btnCancel);
}
}
I was stuck with this for the past 3 days. Can't get the selected checkbox name in dynamic checkboxes. Can anyone help me with this?
Start by having a look at How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listener
One thing you want to focus on is the
setActionCommandfor buttons andgetActionCommandfor theActionEventFor example...
You're also going to want to take the time to read through How to Use Scroll Panes and Laying Out Components Within a Container
Now, because it annoys me, the following is basically your design using proper layout managers
Yours...
Compared to mine...
And now you also have resisability for free
WrapLayoutYou have a number of possible options. One might be to simply add or remove the "action command" of the checkbox from some kind of
ListFor example...
Runnable example