JCombobox editable enabled - java

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.

Related

What text to put to a JComboBox where you want to prompt the user to selection?

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.

JTable - Checkbox add action listener

I've created a simple JTable with check box like below:
DefaultTableModel model = new DefaultTableModel();
jTable1.setModel(model);
model.addColumn("No:", no1);
model.addColumn("Remark", remark1);
model.addColumn("Color", colors1);
model.addColumn("Done");
TableColumn col1 = jTable1.getColumnModel().getColumn(0);
col1.setPreferredWidth(1);
TableColumn col4 = jTable1.getColumnModel().getColumn(3);
col4.setCellEditor(jTable1.getDefaultEditor(Boolean.class));
col4.setCellRenderer(jTable1.getDefaultRenderer(Boolean.class));
col4.setPreferredWidth(50);
jTable1.setShowGrid(true);
jTable1.setGridColor(Color.BLACK);
jTable1.setAutoCreateRowSorter(true);
It's working fine but how to do if I want to add action listener for the check box. For an example, when my check box is checked I need to pop up a confirmation message.
For an example, when my check box is checked I need to pop up a
confirmation message.
You don't need to add an ActionListener to the renderers/editors but you need to listen to table model data changes. Take a look to Listening for Data Changes section of How to Use Tables tutorial:
Add a new TableModelListener to your TableModel
Validate if the updated cell value is a Boolean and its value is true.
Ask the user to confirm the update. If s/he doesn't then set the cell's value back to false.
See TableModelEvent API as well.
Edit
Note in this case as you're working with booleans then there's 2 possible values to do the check. However for input validation in other cases the described procedure won't work simply because the listener will be notified when the change already happened and you won't be able to set the value back just because it won't be there any longer.
Take a look to #kleopatra's answer to this question: JTable Input Verifier. As stated there a better approach is providing a custom CellEditor and do the validation in stopCellEditing() method implementation. Just as a suggestion I'd use a DefaultCellEditor that takes a JCheckBox as parameter and override the aforementioned method.

Radio buttons and setSelected or something else?

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

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

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.

Categories