Radio buttons and setSelected or something else? - java

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);

Related

String Tokenizer.nextElement for JComboBox

I'm using
int TxtAge = Integer.parseInt(tfAge.getText().trim());
to get value from my textfield and search it in database.
Then, I'm using Integer age = Integer.parseInt(stringTokenizer.nextElement().toString()); to go to next attributes in my database.
I have no problem using those codes for textfield but when I'm using the JComboBox the result won't display. How to use the StringTokenizer.nextElement() for JComboBox? Is is the same with TextField?
String sex=(String) stringTokenizer.nextElement();
I tried this code but still failed :(
You seem to have left out the relevant portions of your code, i.e. how you are handling setting/getting items in the JComboBox. Whether you read these values from a database, a file or have them hardcoded is irrelevant to the question.
Since you do ask whether it is the same as with a JTextField, I can at least answer this; it is not the same. The question indicates that you're quite new to Swing. You would probably benefit from working through a basic Swing tutorial, just to get a grip on how to work with these basic GUI elements. For JComboBox, check out Oracles own How to Use Combo Boxes.
Anyways, when working with JComboBox, you will need to first populate it with the values that users can choose from and set the currently selected value. Retrieving the currently selected value is just a simple method call.
Further, you have the possibility of making a combobox editable. This means that the user can edit the text in the combo box to something that was not pre-populated. By default, this option is turned off.
I'll provide some examples.
Initialize:
JComboBox sexComboBox = new JComboBox();
sexComboBox.addItem("Not selected");
sexComboBox.addItem("Male");
sexComboBox.addItem("Female");
sexComboBox.addItem("Do not want to disclose");
By default, the first item you added is selected. To select another one, you need to add one of the following lines:
sexComboBox.setSelectedIndex(1); // zero-based index, "Male" is selected item
sexComboBox.setSelectedItem("Female"); // sets the selected item to "Female"
To enable user to edit the contents to something that was not pre-defined, just add the line:
sexComboBox.setEditable(true);
To retrieve the currently selected value:
String selectedItem = (String) sexComboBox.getSelectedItem();

Working with radio button in java

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

How can I save the value typed by user in dropdown & use that in a editable combo of `org.eclipse.swt.widgets`?

I have created one org.eclipse.swt.widgets.Combo field which in editable.
tableCombo = new Combo(cmposite, SWT.NONE);
I stored some value in the combo at the time of creating the widgets.I want that if user write some value may be string in the combo ,then it will save it the combo dropdown list & user should be able to select the given input.How can I do that?
Just add a listener to the combo (e.g. on focus loss) which calls Combo.add(String). See this example.

Retrieving data from Radio Buttons

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";

JCombobox editable enabled

What is the difference between the setEditable() and the setEnabled() in a jCombobox? Can a combobox be editable but not enabled and other way around? In which situation would you use which method?
Can you imagine a situation in which you would do setEnabled(false) together with setEditable(true)?
setEditable(boolean) determines if the JComboBox allows text entry in addition to selecting a value via pull-down.
setEnabled(boolean) determines if the JComboBox is able to be interacted with at all. If it is not enabled, it is displayed as grayed out.
A JComboBox can have any mix of these properties -
setEditable(true) + setEnabled(true) = JComboBox allows text input in addition to pull down values and user can interact with it.
setEditable(false) + setEnabled(true) = JComboBox only allows values from the pull down to be selected and user can interact with it.
setEditable(true) + setEnabled(false) = JComboBox allows text input in addition to pull down values but user cannot interact with it.
setEditable(false) + setEnabled(false) = JComboBox only allows values from the pull down to be selected and user cannot interact with it.
A situation where you may have a JComboBox with setEnabled(false) and setEditable(true) would be where you want a JComboBox that allows text input, but the form is in a state where the value of the JComboBox isn't applicable. You would usually have some action that would call setEnabled(true) on the JComboBox once it does become applicable.
For example, if you have something like a student housing form, there may be a question on the form like 'Do you need a parking space?' with a JCheckbox. There's a JComboBox for the brand of car and a JTextFied for the license plate number. You may have the JComboBox pre-populated with common car brands - Ford, Chevy, Toyota, Honda, etc. - but decide you also want to allow it to be editable in case someone owns something like a Lamborghini (and is staying in student housing - yeah, right...). The value for car brand and license plate number aren't needed unless the user selects the JCheckBox signifying that they need a parking space. You would add a listener to the JCheckBox that would call setEnabled(true) on the JComboBox and JTextField when it was selected, and setEnabled(false) when it wasn't.
If you call setEditable(true), the JComboBox's text field becomes editable, allowing the user to type text with the keyboard in addition to selecting an item from the list.
If you call setEnabled(false), the entire control becomes disabled, preventing the user from interacting with it at all.
SetEnable() - Enables the combo box so that items can be selected.
SetEditable() - Determines whether the JComboBox field is editable.

Categories