0

I have three JComboBox, one for continents, one for countries and another for cities. All data is loaded from a database.

The first JComboBox has all continents. When I select one of them, the second JComboBox is loaded with the respective countries. Until now works, because I added an "itemStateChanged".

However, when I select a country, the event "itemStateChanged" is called again. What makes my second combo box stay with the first item selected, or (if I do the "RemoveAllItems") duplicates its content?

There is no way in which it created a "itemStateChanged" for each JComboBox? Of the kind .NET, in which it is possible to create a "SelectedIndexChanged" for each combo box?

Some parts of my code:

public class MainFrame extends JFrame implements ItemListener 
{
…
private String iddistrito="VAZIO!!!";
private String idmunicipio="VAZIO!!!"; 
private boolean limpa=false;
private boolean populardistritos=false;
private boolean popularmunicipios=false;
private JComboBox cbFreguesiacliente = new JComboBox();
private JComboBox cbmunicipiocliente = new JComboBox();
private JComboBox cbdistritocliente = new JComboBox();
…
private void jbInit() throws Exception {
…
cbmunicipiocliente.addItemListener(this);
cbdistritocliente.addItemListener(this);     
…
}

This part of code is the content when i click on the button, to enable my components:

private void btnNovocliente_actionPerformed(ActionEvent e) {
    populardistritos = true;
    cbdistritocliente.removeAllItems();
    preenchecbdistritos();
    populardistritos = false;
}

This piece of code fills the contents of the first JComboBox:

private void preenchecbdistritos(){
        query = "select Distrito from distritos;";
        try{
            Class.forName(JDBC_DRIVER);
            connection = DriverManager.getConnection(DATABASE_URL);
            statement = connection.createStatement();
            ResultSet resultset = statement.executeQuery(query);
            StringBuffer results = new StringBuffer();
            ResultSetMetaData metaData = resultset.getMetaData();
            int numberOfColumns = metaData.getColumnCount();
            cbdistritocliente.addItem("");
            while (resultset.next()) {
                for (int i =1; i<= numberOfColumns; i++) {
                    if (metaData.getColumnName(i).equals("Distrito")) {
                        cbdistritocliente.addItem(resultset.getObject(i).toString());
                    }
                }
            }
        } catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(this,"rebentou no 1º catch " + e.toString(),"Inane error", JOptionPane.ERROR_MESSAGE);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this,"rebentou no 2º catch " + e.toString(),"Inane error", JOptionPane.ERROR_MESSAGE);
        }
}

This is the famous event, which would like to get individually for each JComboBox:

public void itemStateChanged(ItemEvent e) {
    if (populardistritos == false) {
        if (limpa == false) {
            if ((!cbdistritocliente.getSelectedItem().toString().equals("")) && (idmunicipio.equals("VAZIO!!!")))
            {
                query = "select id_distrito from distritos where distrito = '" + cbdistritocliente.getSelectedItem().toString() + "';";
                try{
                    Class.forName(JDBC_DRIVER);
                    connection = DriverManager.getConnection(DATABASE_URL);
                    statement = connection.createStatement();
                    ResultSet resultset = statement.executeQuery(query);
                    StringBuffer results = new StringBuffer();
                    ResultSetMetaData metaData = resultset.getMetaData();
                    int numberOfColumns = metaData.getColumnCount();
                    while (resultset.next()) {
                        for (int i =1; i<= numberOfColumns; i++) {
                            if (metaData.getColumnName(i).equals("id_distrito")) {
                                iddistrito = resultset.getObject(i).toString();
                            }
                        }
                    }
                        preenchecbmunicipios();    
                } catch (ClassNotFoundException e2) {
                } catch (SQLException e2) {
                }
            }
        }


        if (!cbmunicipiocliente.getSelectedItem().toString().equals(""))
        {
            query = "select id_municipio from municipios where municipio = '" + cbmunicipiocliente.getSelectedItem().toString() + "';";
            try{
                Class.forName(JDBC_DRIVER);
                connection = DriverManager.getConnection(DATABASE_URL);
                statement = connection.createStatement();
                ResultSet resultset = statement.executeQuery(query);
                StringBuffer results = new StringBuffer();
                ResultSetMetaData metaData = resultset.getMetaData();
                int numberOfColumns = metaData.getColumnCount();
                while (resultset.next()) {
                    for (int i =1; i<= numberOfColumns; i++) {
                        if (metaData.getColumnName(i).equals("id_municipio")) {
                            idmunicipio = resultset.getObject(i).toString();
                        }
                    }
                }
            } catch (ClassNotFoundException e2) {
            } catch (SQLException e2) {
            }
        }            
    }
}

This piece of code fills the contents of the second JComboBox:

private void preenchecbmunicipios(){
    if (!iddistrito.equals("VAZIO!!!")) {
        query = "select municipio from municipios where municipios.id_distrito = " + iddistrito + ";";
        populardistritos=true;
        //cbmunicipiocliente.removeAllItems();
        try{
            Class.forName(JDBC_DRIVER);
            connection = DriverManager.getConnection(DATABASE_URL);
            statement = connection.createStatement();
            ResultSet resultset = statement.executeQuery(query);
            StringBuffer results = new StringBuffer();
            ResultSetMetaData metaData = resultset.getMetaData();
            int numberOfColumns = metaData.getColumnCount();
            cbmunicipiocliente.addItem("");
            while (resultset.next()) {
                for (int i =1; i<= numberOfColumns; i++) {
                    if (metaData.getColumnName(i).equals("municipio")) {
                        cbmunicipiocliente.addItem(resultset.getObject(i).toString());
                    }
                }
            }
            populardistritos=false;
        } catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(this,"rebentou no 1º catch do cbmunicipio com " + e.toString(),"Inane error", JOptionPane.ERROR_MESSAGE);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this,"rebentou no 2º catch do cbmunicipio com " + e.toString(),"Inane error", JOptionPane.ERROR_MESSAGE);
        }            
    }
}
Pepper
  • 97
  • 1
  • 10

2 Answers2

2

the docs tell you that event contains info about originator. So just check for e.getItemSelectable().equals(firstSelectBox) etc.

Hope that helps.

atamur
  • 1,567
  • 1
  • 14
  • 25
2

You may be able to leverage the approach shown here, in which a selection in the first combo replaces the model in a second combo. In that example, combo1 would hold continents, and the combo2 model would be set to the one containing countries on the selected continent. The example uses an array, but a List<ComboBoxModel<String>> might be a more flexible choice.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045