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
Related
I am using multiple JComboBox in a single JPanel. I was wondering what is the go to initial JComboBox selection text?
For example I can put "None" but that will seem as if some value is already selected in the JComboBox. I am using labels for which type of data they are so writing their type is unnecessary.
I can either put "" but something like "----" seems better. Is there a standard text when you want the user to select some value from JComboBox?
If you want to set an initial value to JComboBox that should be non-selectable (as soon as user selects another option, it will not be possible to select the initial one again), than it has been already answered here.
One way is to use a custom renderer. The renderer will simply display a default prompt when no item of the combo box has been selected.
Check out Combo Box Prompt for an example of this approach.
I'm fairly new to android Java and I was wondering if you could assign a value to a radio button, check box or button like you can with HTML?
I have data coming from MySQL which I would like to be assigned to radio buttons.
Yes that is possible in Android.You can add it dynamically(programmatically) in the code.Refer the below link for how to do it How to add radio button dynamically as per the given number of counts?
Yes you can assign value to radio button , Check box and Buttons.
Please follow a good tutorial for android.
Please check this Link
Hope this helps.
ComboBox cmbCategory, cmbType;
I have two ComboBoxes. The 2nd ComboBox depends on the first ComboBox.
For example.
If I chose "Food" on cmbCategory, the choices on cmbType would be "desert, appetizer..")
How will I do that without clicking a button. I mean when "Food" is chosen on cmbCategory, the choices for food on cmbType would showed up automatically without clicking a button. Because what I have come up to is that my cmbType is hidden and when a button is clicked, then that's the time it will be visible.
I believe this is about MouseListener or MouseClicked but I have no idea on how to do it.
You can add an ActionListener to the cmbCategory. On select call getSelectedItem() to get category.
Define a Map> the map should keep the list f items for each category. Fill the map (or you can define some logic to get list of types by selected category). Then just remove all the existing items in cmbType and add new list of types for the selected category.
See the ombobox related code snippets here
how can I create (with JAVA) Combobox that contains Checkboxes for multiple selection and display the selected items in the Combobox like this picture:
click to see the pic
and thnx for advance.
EDIT:
I found this API (JAPURA API) and it's great but when I select multiple things I want to display the selected items instead of "* multiple items *".
Here's the link to achieve your goal:
JComboBox Providing a Custom Renderer
I would suggest you create a JButton and style it in the form of a Combobox if you want that.
Then, at the onclick function (actionperformed), you create a JPanel and make it visible under the button. In the panel, you can put whatever you want, so you just put checkboxes there.
I know this is kind of a workaround of your problem, but it should be easy for you to do so, and the actual user does not see a difference at all.
Hope I could help you.
Cheers,
Lucky
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);