0
votes

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.

2

2 Answers

0
votes

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.

0
votes

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.