I need to retrieve data from radio buttons.
Basically like this:
String Gender = rdbtnM.getText();
My problem is, the user can select from one of 2 radio buttons:
rdbtnM and rdbtnF. So the String Gender should have the value whatever has been chosen; F or M.
How do I write that?
I tried this, but doesn't work:
String Gender - rdbtnM.getText(); && rdbtnF.getText();
Don't give the radio buttons an ActionListener. One way to get the result is to query the ButtonGroup that controls the RadioButton. If you've given each JRadioButton an appropriate actionCommand then the ButtonModel returned by the ButtonGroup will hold that String. For example, please have a look at the sample code here.
If the variable is global, implement an action listener to both radio buttons that triggers when a user toggles either one. Within the action listener method, set the string Gender to the appropriate value as required.
if(radiomale.isSelected())
String gender="male";
if(femaleradio.isSelected())
String gender="female";
Related
I've problem with a JComboBox SelectedItem can someone help me ?
I've implemented a GUI window where are a various textfield and one JComboBox. When I click button, code must create an Object with the strings of the textfield and string of ComboBox, but I've a NullPointerExceptionin SelectedItem... The code to get is :
(String)combo.getSelectedItem()
I tried too:
combo.getSelectedItem().toString()
but don't work !
I don't know if I need a ActionListener but think it's not required.
you can get the jcombox selected items like this.
String item = jcombox.getSelectedItem().toString ();
I Placed this code on onclick event of a button and it worked.
like i have a radiogroup with 4 Jradiobutton and if i give my function a "3" parameter it checks the third child in the radioGroup, something like Jcombobox.setSelectedIndex(index);
something like Jcombobox.setSelectedIndex(index);
There is no such method.
One solution is to keep an ArrayList of all your radio buttons. Then you can access the appropriate button from the list and make it selected.
Or you can use the ButtonGroup to get the Enumeration of all the buttons. Then you will need to iterate through the Enumeration to find the proper button.
I need to generate the radio button in the zk framework according to database column entries and labelling those radio buttons based on the particular column values. Now I am hardcoding the radio button label values. However these values should be taken from the database column entries. In an arraylist "name" I have taken all the values in column. How can assign these to the radio button labels? Please help me
It should be quite simple, if you use the MVC pattern with a Java class as a controller have a look at the following example:
ArrayList<String> columnEntries = new ArrayList<String>();
//populate the array list here, and then...
Radiogroup radiogroup = new Radiogroup();
for(String entry: columnEntries){
radiogroup.appendItem(entry, entry);
}
The radiogroup.appendItem(entry, entry); adds automatically a radio button to the specified radiogroup. If you want that the radio button should contain a different value than its label, change the second parameter from entry to your desired value.
I have a JFrame form with labels, text fields, a combo box and a button group that has 2 radio buttons. In another form I have a JTable filled with data about students (student id, name...), and when I select a row from a table, the form opens and its field need to be filled with same values (eg. if Peter Peterson was selected from the table, then his name should be shown in text filed Name on the form, so I did txtfieldName.setText(student.getName), my question is how do I do this for radio button? Do I need to have yes/no column in the table, so when I select a table row where the value is, say, ''yes'' in order to have yes radio button selected on the form?
if (result.getString(5).equals("Male")) {
jRadioButton1.setSelected(true);
} else {
jRadioButton2.setSelected(true);
}
This depends. Is the value that is being applied to the radio button part of the underlying table data (ie from your student object)? If it is, then, no, you don't HAVE to have yes/no column, although it might be of use if you are trying to make decisions on it. Instead, you would simply extract it from the student object.
I would display the yes/no value as a column If it was relevant to the decsion making process.
If the value you want to use to set the radio button is NOT part of the student oect then you need to make some decisions.
Personally, I would create a wrapper object that represented the state of the row, this would include the student object and any other relevant details you might want. This makes the data self contained and makes accessing that information easier.
If that wasnt desirable, then, yes, you would need to probably display the value as a column and maintain it as separate value within the table model.
This will also come down to how you've Implemented the table model
Updated with feedback from the OP
Okay, so, with your student object hand. You can get the boolean property from the student (that is begin represented on the screen by you radio buttons) and make a choice...
trueRadioButton.setSelected(booleanValue);
trueRadioButton.setSelected(!booleanValue);
You could make it even more simpler by using a JCheckBox instead...
checkBox.setSelected(booleanValue);
You select the radio buttons through the ButtonGroup:
group.getSelected(button.getModel(), true);
You don't have to know which radio button is selected a priori, so you don't have to have that information in the table if you don't want to. You can instantiate both radio buttons with the 'selected' parameter set to false:
JRadioButton button = new JRadioButton("My button label", false);
JRadioButton otherButton = new JRadioButton("My other button label", false);
ButtonGroup group = new ButtonGroup();
group.add(button);
group.add(otherButton);
I've two jRadioButtons in Java application. Let's say Male and Female. Initially one is selected. While selecting another, the previous should be unselected and vice-versa. How to make it work? Also how can it be used to store in database?
You can give same name to all the radio buttons, from which you want to select only one. That way, you will be able to select just one button out of all. This you can do by creating a ButtonGroup.
Now, if you want to add the selected item to the database, just get the value corresponding to the name of Button Group, you will get the value of selected button.
See documentation and tutorial
What you need to use is a ButtonGroup component. Have a look at: How to Use the ButtonGroup Component