java swing combo box - java

I am working on a UI design project in java swing where in I am expected to query a database based on the value selected from the Combo box. I have five combo boxes and a text field. I use .getSelectedItem() to include the value selected from the combo box in the where condition. If I run the query, apparently the first entry in the combo box which according to my design is Select.. gets selected and it returns nothing.I want the first entry to be a null value so that unless and until I select a value from combo box it remains null. Please help me out. Thank you.

I want the first entry to be a null value so that unless and until I select a value from combo box it remains null.
You should not use a null item in the combo box. However, after you load the data into the combo box you can use:
comboBox.setSelectedIndex(-1);
and no item will be selected in the combo box.
You may also want to consider using the Combo Box Prompt class. It will display a "hint" for the usage of the combo box until the user selects an item.

You can create a wrapper class that wrap the actual value like:
public class MyComboItem {
public MyComboItem(Object actualValue) {
this.actualValue = actualValue;
}
public Object getActualValue() {
return this.actualValue;
}
}
You may need to override toString() in this class so the combo box knows how to render the items property.

Related

getCellEditorValue() of CellEditor.java is Only Called When the Cell is Clicked

Consider the following Jtable, and more precisely the first column which has a JComboBox in it:
When I try to save the value of the first JComboBox (the one that has "auth2" as its value) without clicking on it first, when I check the database, I find an empty String.
However, when I click on it first and then save, I get the right value stored in the databse.
Using the debugger, I found out that the method getCellEditorValue() of CellEditor.java is only called when you click on the JComboBox itself first.
This explains why in the database, I get the right value when I click on the JComboBox first and when I don't click on it, I get an empty String.
So my question is, is there a way to call the getCellEditorValue() method every time I save, regardless of whether or not I click on the JComboBox?
Thank you
I get the right value when I click on the JComboBox first and when I don't click on it, I get an empty String.
You should NOT try to access a value from the combo box. The combo box is shared as an editor for all rows in the table.
is there a way to call the getCellEditorValue() method every time I save
Data is stored in the TableModel, not the combo box.
You can use the getValueAt(…) method of the JTable or TableModel at any time to get the value from the model.
Note: it is possible (depending on what you are doing) that the data has not been saved from the editor to the TableModel. If this is the case then check out: JTable stop cell editing without user click for solutions.

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.

Difference between getItem and getSelectedItem

In working with the JComboBox in Swing and reading up all the interfaces and classes and their various properties, I am not confused between the ComboBoxEditor's getItem method, and the JComboBox's getSelectedItem.
I am talking from the point of view of an editable combo box. When I call both these methods after editing some text in the combo box and pressing enter, both methods return the same value. Of course, that is expected, but then what are the specific uses of these two methods ? Is there any difference in the order in which they are called from within the combo box's code ?
The ComboBoxEditor's getItem() of function returns the edited item
And JComboBox getSelectedItem() returns the selected item. However to be specific to your question:
Is there any difference in the order in which they are called from
within the combo box's code ?
An editable Combo Box uses an editor to display the selected item. As soon as an item is edited, an action event is fired on registered ComboBoxEditor instance of the JComboBox . The implemented actionPerformed function of editor's action listener, gets the edited item from editor and then set as selected using the model's setSelectedItem(item) function which is evident from the following source code:
public void actionPerformed(ActionEvent e) {
Object newItem = getEditor().getItem(); // get edited's item
setPopupVisible(false);
getModel().setSelectedItem(newItem); // model set the item as selected
String oldCommand = getActionCommand();
setActionCommand("comboBoxEdited");
fireActionEvent();
setActionCommand(oldCommand);
}
Then, when we ask to get getSelectedItem() it returns the selected item by asking the model.
Why would you ever want to use the ComboBoxEditor's getItem() method? It is extra work to get the editor in order to get its value.
In general you should only ever get the data from the components model. The model is responsible for tracking the data in the model as well as the selected item in the model. The getSelectedItem() method of the combobox is a convenience method to get the selected item from the model.
It is the editors responsibility to update the model when the data is changed. So I would say the getItem() method is used by the editor to update the model at the appropriate time and you should not really be using it.

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.

SWT Combo Box Default selection

Im a newbie to swt..I have an SWT Combo box with 2 entries. Currently, it shows a blank selection as the user did not select any. Is there any way to make sure that even if the user does not select any entry in the combo box, he still gets to see the default value selected, is this even possible?..
Use the Combo's select method to select the default option when you create it.

Categories