views:

30

answers:

2

This Java Swing JComboBox is modified based on changes to the system configuration. In the example image, "Press to Select" is the case where nothing is selected and there is one "Test Unit" in the configuration, but "Press to Select" is displayed twice in the drop down. The additional "Press to Select" item behaves like item 0 so it is functional, but it looks crappy this way. Any ideas?

public class Controller extends javax.swing.JFrame implements Observer {
    ...
    public void update(Observable o, Object arg) {
        jComboBox.removeAllItems();
        jComboBox.addItem("Press to Select");
        String[] names = Configuration.getNames();
        for (String n : names) {
            jComboBox.addItem(n);
        }
        ...

alt text

A: 

Is the update method called by the Event Dispatch Thread or by some other thread? (log SwingUtilities.isEventDispatchThread() if you're unsure). If it's any other thread, move your update into a Runnable and use SwingUtilities.invokeLater() to schedule it for execution on the EDT.

Try setSelectedItem(null) as part of your update.

Barend
A: 

Dumb mistake. Adding it twice (@Geoffrey). Taking too long to find the 2nd add in another class. My grandmother warned me not to get old.

JackN