absolutelydevastated
absolutelydevastated

Reputation: 1747

setSelectedIndex(-1) not working for JComboBox

So this is my code for a Java Swing UI. Basically I have 2 combo boxes and I'm trying to set the default index for both to -1 (blank). The setSelectedIndex(-1) works fine for the first but not the second. Is it something to do with the ActionListener for the first? But shifting it downward doesn't work as well.

public Panel(JFrame parent) {
    this.setBounds(0, 0, 0, 0);
    this.setBorder(new EmptyBorder(5, 5, 5, 5));
    this.setLayout(null);

    ...     

    // This is working
    fstCB = new JComboBox(SomeEnum.values());
    fstCB.setSelectedIndex(-1);
    fstCB.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // Do something
            }
        }
    });
    fstCB.setEditable(true);
    this.add(fstCB);

    // This is not working.
    JComboBox<String> sndCB = new JComboBox<String>();
    sndCB.setSelectedIndex(-1);
    sndCB.setVisible(false);
    this.add(sndCB);

    List<String[]> rs = db.select("SELECT smth FROM table", 1);
    for (String[] r : rs) {
        sndCB.addItem(r[0]);
    }

    JCheckBox chckbx = new JCheckBox("Check here");
    chckbx.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (chckbx.isVisible()) {
                chckbx.setVisible(false);
            } else {
                chckbx.setVisible(true);
            }
        }
    });
    this.add(chckbx);

}

Thanks in advance.

Upvotes: 0

Views: 1303

Answers (1)

Peter
Peter

Reputation: 1612

From a quick look the issue seems to be coming from when you are setting your index, not the usage of your listeners

In the code you provided, the JComboBox you are having issues with sets the index before it has any items in it. When you then add items from your result set, it will revert to the default behaviour of selecting the first item

I've added a quick example below to highlight this

enum SomeEnum{
        One, Two, Three;
    }

public static void main(String[] args){
    JFrame frame = new JFrame();
    JComboBox prePopulatedComboBox = new JComboBox(SomeEnum.values());
    prePopulatedComboBox.setSelectedIndex(-1);

    JComboBox postPopulatedComboBox = new JComboBox();
    postPopulatedComboBox.setSelectedIndex(-1);
    for(SomeEnum someEnum : SomeEnum.values()){
        postPopulatedComboBox.addItem(someEnum);
    }
    //Uncomment the below line to see the difference
    //postPopulatedComboBox.setSelectedIndex(-1);

    JPanel panel = new JPanel(new BorderLayout(5,5));
    panel.add(prePopulatedComboBox, BorderLayout.NORTH);
    panel.add(postPopulatedComboBox, BorderLayout.SOUTH);

    frame.add(panel);
    frame.setMinimumSize(new Dimension(250,250));
    frame.setVisible(true);
}

My suggestion is to try moving:

sndCB.setSelectedIndex(-1);

to here:

List<String[]> rs = db.select("SELECT smth FROM table", 1);
    for (String[] r : rs) {
        sndCB.addItem(r[0]);
    }
sndCB.setSelectedIndex(-1);

Hopefully this helps, and if it doesn't please update your question with a more complete example to clarify the issue as suggested by Andrew

Upvotes: 2

Related Questions