Hello,
I have two JComboBoxs, the first one is filled from database, the second one is filled after user's choice on the first comboBox.
Once a choice in second comboBox is done, some JLabels are filled from database.
The problem is that when I select an item in the first combobox and then I change the choice thr first item is always present and the second is added with dupplicates.
final JComboBox comboBox1 = new JComboBox(); final JComboBox comboBox2 = new JComboBox(); try { String sql = "Select matricule FROM clients"; rs = stat.executeQuery(sql); comboBox1.addItem("Select item"); while(rs.next()) { comboBox1.addItem(rs.getInt("matricule")); } } catch (SQLException e) { e.printStackTrace(); } comboBox1.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent arg0) { int matricule = (Integer) comboBox1.getSelectedItem(); String sqll = "Select * FROM clients WHERE matricule = " +matricule; try { rs = stat.executeQuery(sqll); while(rs.next()) { nom.setText(rs.getString("nom")); prenom.setText(rs.getString("prenom")); cin.setText(rs.getString("cin")); adresse.setText(rs.getString("adresse")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } String sql = "Select DISTINCT p.code FROM parcelle p, clients c WHERE c.matricule = p.exploitant AND c.matricule = " +matricule; try { rs = stat.executeQuery(sql); while(rs.next()) { comboBox2.addItem(rs.getInt("code")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); comboBox1.setBounds(154, 317, 105, 20); contentPane.add(comboBox1); comboBox2.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent arg0) { int code = (Integer) comboBox2.getSelectedItem(); String sqll = "Select * FROM parcelle WHERE code = " +code; try { rs = stat.executeQuery(sqll); while(rs.next()) { sau.setText(rs.getString("sau")); sol.setText(rs.getString("type_sol")); irrigation.setText(rs.getString("mode_irrigation")); exploitation.setText(rs.getString("type_exploitation")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); comboBox2.setBounds(154, 423, 105, 20); contentPane.add(comboBox2);