I would like to ask if how can I achieve this thing. I wanted to add a multiple values in one column in database from checking the check boxes. e.g., if i check three check boxes all the checkbox selected will add in column in database. I have attached the image below for visualization. thank you!
CodePudding user response:
I finally got it by exploring and getting some ideas from David. this is the so much lesser code for it.
String subject = "";
if (cb1.isSelected()) {
subject = subject "Subject 1";
}
if (cb2.isSelected()) {
subject = subject ", Subject 2";
}
if (cb3.isSelected()) {
subject = subject ", Subject 3";
}
System.out.println(subject);
CodePudding user response:
I take it back, there's an even easier way that doesn't require ButtonGroup. Simply use an ActionListener.
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class SOQ_20220425
{
public static void main(String[] args)
{
final JFrame frame = new JFrame();
frame.setTitle("Button Group Example");
frame.setSize(500, 500);
frame.setLocation(500, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
final AbstractButton button1 = new JCheckBox("Button 1");
final AbstractButton button2 = new JCheckBox("Button 2");
final AbstractButton button3 = new JCheckBox("Button 3");
final AbstractButton check = new JButton("CHECK");
check.addActionListener(event -> System.out.println("button 1 is selected? = " button1.isSelected()));
check.addActionListener(event -> System.out.println("button 2 is selected? = " button2.isSelected()));
check.addActionListener(event -> System.out.println("button 3 is selected? = " button3.isSelected()));
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(check);
frame.add(panel);
frame.setVisible(true);
}
}
As you can see, check the boxes, then click CHECK. If the checkboxes are checked, the terminal will reflect that.
If you want different behaviour, just change what you do in the ActionListener. In this case, you could have just 1 ActionListener (instead of 3) which figures out if the checkboxes are set, then sends that String to your database. That should solve your problem.
EDIT -- added another example.
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class SOQ_20220425
{
public static void main(String[] args)
{
final JFrame frame = new JFrame();
frame.setTitle("Button Group Example");
frame.setSize(500, 500);
frame.setLocation(500, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
final AbstractButton button1 = new JCheckBox("Button 1");
final AbstractButton button2 = new JCheckBox("Button 2");
final AbstractButton button3 = new JCheckBox("Button 3");
final AbstractButton check = new JButton("CHECK");
check.addActionListener(event -> printButtonString(button1, button2, button3));
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(check);
frame.add(panel);
frame.setVisible(true);
}
private static void printButtonString(AbstractButton... buttons)
{
String output = "";
for (AbstractButton button : buttons)
{
if (button.isSelected())
{
output = button.getText() ", ";
}
}
output = output.length() == 0 ? "" : output.substring(0, output.length() - 2);
System.out.println(output);
}
}