13

I would like to change the contents of the list of a JComboBox (like adding another list in place of and older one). Is there any way I might be able to do that? Thanks in advance!

1
  • 1
    i need this answer also. Eventough I used JCombobox(newData); again and again previously. But it seems overwhelming. Thanks @AndreiC. :D
    – gumuruh
    Commented Apr 17, 2012 at 2:40

5 Answers 5

11

If you want to be able to add and remove items from an existing combo box at runtime, the underlying data model for the combo box needs to be a MutableComboBoxModel

Google for that class for interface information and for examples of how to use it.

Note that if you want the user to type in new values, you also need to make the box editable.

You can find some links to examples here.

5

I found this thread and came up with a quick (and probably dirty) solution:

oldComboBox.setModel(new JComboBox<>(new_items).getModel());
5
  • 1
    dirty indeed - longest distance away from the straightforward solution I have ever seen (and I have seen quite a lot, creativity in doing things wrong is huge :-)
    – kleopatra
    Commented Dec 5, 2011 at 14:44
  • plus there already was an accepted solution to this question. any reason why you think that wasn't reasonable and why the OP/ community would benefit from this (3 years after the solution was accepted) ?
    – aishwarya
    Commented Dec 5, 2011 at 17:03
  • I didn't read the answers thoroughly - lesson learnt. It's a rubbish solution for replacing the model - which was already suggested, and can be done more gracefully
    – pal
    Commented Dec 18, 2011 at 12:50
  • Why do you say this is a bad answer? Unlike the others, I think this is a quite clever answer! Thank you @pal.
    – Mehran
    Commented Nov 28, 2012 at 9:44
  • @Mehran I thank pal for posting this solution because it should work. However, the other comments are right to point out that it is a "dirty" fix and not a straightforward one. Resetting the entire model for a JComboBox is close to asking the GUI to completely remake that JComboBox. It would be better for the program to use the MutableComboBoxModel mentioned in the accepted answer, which allows the program to edit the existing JComboBox instead of doing this, which is the equivalent of scratching out most of the JComboBox's data and re-creating it. Commented Aug 29, 2014 at 16:18
2

Of course you can. There are several methods for manipulating JComboBoxes using the default list model. Have a look at the remove* methods and add* methods:

http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html

1
  • To me, the best answer. x.removeAllItems(); x.addItem(obj1); x.addItem(obj2); encompasses most all situations I would tend to encounter. And its KISS. Commented Mar 3, 2016 at 20:51
2

You can also replace the model in its entirety with setModel().

But after writing more and more user interfaces, I find it more useful to write a custom ComboBoxModel to wrap the data structure the ComboBox is presenting. This is more unit testable and cleaner, IMHO.

1
  • 1
    Unfortunately not everything copes seemless with the model changing. Commented Dec 29, 2008 at 7:24
0

The Glazed Lists library is mighty helpful when you want to wire any sort of mutable list to a GUI control. It's a large-ish library which may not be appropriate for your project, but take a look at their screencasts and judge for yourself. It supports a lot of related stuff like filtering and auto-completion and can save you a lot of manual work.

Not the answer you're looking for? Browse other questions tagged or ask your own question.