Javanshir Mammadov
Javanshir Mammadov

Reputation: 93

ComboBox (setSelectedIndex()) is not working properly

I have 7 items in comboBox and every time when i select one of them and click "next" button it selects first item not next. Do anyone knows why?

    if ("Цена (EURO)".equals((String) comboBox.getSelectedItem())) {
            if (!"".equals(txtArea.getText().toString())) {
                Cenaeuroa = null;
                String data = (String) txtArea.getText();
                String[] temp = data.split("\n");
                Cenaeuroa = new String[temp.length];
                System.arraycopy(temp, 0, Cenaeuroa, 0, temp.length);
                len = Cenaeuroa.length;
            }
            comboBox.setSelectedIndex(0);
//            Object sort = "СОРТ";
//            comboBox.setSelectedItem(sort);
        }
        txtArea.setText(null);

Upvotes: 2

Views: 7640

Answers (1)

Igor Rodriguez
Igor Rodriguez

Reputation: 1246

You are selecting the first element, with comboBox.setSelectedIndex(0). You should use getSelectedIndex() to retrieve the selected item and use it to set the next.

For example:

final int selectedIndex = comboBox.getSelectedIndex();
if (selectedIndex < comboBox.getItemCount()) {
    comboBox.setSelectedIndex(selectedIndex + 1); 
}

Upvotes: 4

Related Questions