a question on JRadio Button-please help

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.

Mary.

[832 byte] By [angelswings] at [2007-9-26 1:23:00]
# 1

Hello Mary

OK, I'm going to try and help you out. I'm afraid I can't help you much with the actual workings of saving and reading records to/from your database but I might be able to help you with the radio buttons.

When a user selects a radio button, you (rightly) suggested using the isSelected() method.

Let's say your radio buttons are called maleRB and femaleRB. Then, when you call isSelected() on both buttons, one will return true and the other will return false.

Once you get a "true" response perhaps you can store, in a String object, the gender of that person.

An example might be clearer:

public boolean radioButtonCheck()

{ String gender = ""; // Actually, you might have to

// make this a global variable.

if (maleRB.isSelected() == true)

{ gender = "male";

}

if (femaleRB.isSelected() == true)

{ gender = "female";

}

}

Now you can save the "gender" String object into a database record along with the other information.

OK, now for reading records. Again, I don't know exactly how your database works but here's what I would do. Assuming the record has been read:

public void genderCheck()

{ if (gender.equalsIgnoreCase("male"))

{ maleRB.setSelected();

}

if (gender.equalsIgnoreCase("female"))

{ femaleRB.setSelected();

}

}

This will select the appropriate radio button according to the value (either "male" or "female") of the "gender" String object.

Remember, for every record read, you must set the "gender" String object to whatever is contained in the record.

I hope all this is of some use to you. If you need any more help, just post a message.

Regards

Daniel Lam

Daniel_Lam at 2007-6-29 1:00:51 > top of Java-index,Archived Forums,Swing...