How to load an enum populated ComboBox with no value selected - java

On Netbeans, I have a jDialog with a jComboBox populated from values coming from a Java Enumeration. I need the default value of the Combo Box to be blank at start (or null or whatever), this way forcing the user to choose for a valid option. I have "selectedIndex" set to -1 but still the default value is my first enumeration value. So how can I have my Combo Box load with no value selected?
Thank you very much for your help.
Cheers.

You need to remove and re-add the ItemListener as suggested in this answer to a similar question.

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.

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 to set text to a jcombobox, but the text set should not be an element of jcombobox

is their any way to set text for a combo box, which is not an element of the combobox.
cboSubjects=new JComboBox();
cbo.addItem("Maths"); // and few more subjects are added
cbo.setSelectedItem("subjects"); // this does not set the default text of combobox
Is their any way to solve this problem ? I need something which works like combobox.text
property of combobox in visual basic
I am working on school management system. I need help.
I'd like to show you a possible alternative :)
Note, you need to call label.setDisplayedMnemonic('s'); and label.setLabelFor(combo) to complete the effect!
You can make jComboBox editable, cbo.setEditable(true);, and after that set the text you want: cbo.setSelectedItem("subjects");

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.

How to prevent JComboBox from closing?

After selecting an item from a JComboBox, i want to keep the JComboBox still opened to allow the user to choose another item from it. Is that possible ?
I hope you can provide me an example.
Perhaps what you are after is a JList instead.
As a different approach, maybe you can display in the JComboBox only the "allowed to choose" items in first place?

Categories