How to add default item like Add New Item at top of JComboBox
How can I add a default entry at the top of a JComboBox such as Add New Item in order to allow a user to click this entry and get a dialog allowing them to create a new entry? Should I try to use a custo开发者_StackOverflowm renderer or what is the best approach? I appreciate anyone's assistance with this.
1) Add an entry at the top of the source populating the combo
2) Add a listener to the combobox, launching an external dialog when selectedindex=0
I think that it would be better User Interface Design to have a dedicated button for adding new items to your model.
Consider this: If you use the JComboBox to ALSO control addition, do you also extend it to control removal? What happens after you create your first entry? Does that default entry disappear? What happens when you have LOTS of entries?
Also, I've found that its easier to control permissions by having dedicated add/remove buttons.
you can have the first item of the combo box be an empty string, and put directions that tell the user that they can enter their own text or edit any entry.
if the combobox is marked as editable, then whenever a user types in the combobox, a text entry box will pop up.
like so:
Vector<String> options = new Vector<String>();
options.add("");
options.add("blue");
options.add("red");
...
JComboBox result = new JComboBox(options);
result.setEditable(true);
addititonally, if the combobox is in a table, then you can use a different combobox for the renderer and the editor; as in : if the value of that cell is null or the empty string, then you can make the renderer combobox have a single option that says "pick or enter a value" or " select a value" and then have the editor be the actual combo box that I defined up above.
精彩评论