a question on JRadioButton
Hi!
I'm quite new to java programming and i was wondering if some kind soul could help me out in this.
I'm doing a simple member registration for a library. I'm using jdk1.2.2, Swing to create the interface and MS Access as the database.
Here's the problem:
I have two JRadioButtons (which is grouped using ButtonGroup) to choose the member's gender, ie Male or Female.
I've tried a few methods based on the API docs (getSelection and setSelected), but i can't seem to add/write it to the database when i click the SAVE button.
Can someone please tell me how i can successfully write it into the db and how i can display the correct radio button when i retrive that record.
Thank you for your time and help.
M.
Do this,
Panel panel = new JPanel();
JRadioButton button1 = new JRadioButton("Button 1");
JRadioButton button2 = new JRadioButton("Button 2");
button1.setActionCommand("button1");
button2.setActionCommand("button2");
ButtonGroup group = new ButtonGroup();
group.add(button1);
group.add(button2);
panel.add(button1);
panel.add(button2);
this.add(panel, BorderLayout.CENTER);
JButton button = new JButton("SAVE");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
performAction(ae);
}
});
this.add(button, BorderLayout.SOUTH);
private void performAction(ActionEvent ae) {
ButtonModel model = group.getSelection();
String actionCommand = model.getActionCommand();
if(actionCommand.equals("button1")) {
System.out.println("Button 1 selected."); // here we know which is selected.
group.setSelected(button2.getModel(), true); // here I am selecting Button 2.
}
else {
System.out.println("Button 2 selected."); // here we know which is selected.
group.setSelected(button1.getModel(), true); // here I am selecting Button 1.
}
panel.validate();
panel.repaint();
}