JcomboBox showing only one item
Whats wrong with my code? the second JcomboBox is showing only one item even if there is more in the database.
Code :
@Override
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if(o == comboBox1) {
Object matricule = 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 e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
comboBox2.removeAllItems();
Object index = comboBox1.getSelectedItem();
String sql = "Select distinct p.code FROM parcelle p, clients c WHERE c.matricule = p.exploitant AND c.matricule = " +index;
try {
rs = stat.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getInt("code"));
comboBox2.addItem(rs.getInt("code"));
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return;
}
Object code = 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 e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
Re: JcomboBox showing only one item
Quote:
only one item even if there is more in the database.
It doesn't matter how many are in your database....does your query return more than one? Does the println in your loop print out more than one?
1 Attachment(s)
Re: JcomboBox showing only one item
Yes the query return more than one:
http://i46.tinypic.com/2uyr6e8.jpg
The println print out just one.
Re: JcomboBox showing only one item
I can't see anything obvious from the code you posted that would cause this. When the value prints...does it print what you would expect (eg 10)? Have you tried using a PreparedStatement?
Re: JcomboBox showing only one item
I expect for exemple 10 and 11 but it only prints 10.
I just realised that if i delete this part:
Code :
Object code = 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 e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
It works perfectly but i need some Jlabels to be filled with data from the second comboBox item choice.
Re: JcomboBox showing only one item
Just as a thought, could you try the getItemCount() method in a println to check to see how many items are indeed in the ComboBox?
That way you could tell if only one item is being added or if there's more than one but somehow only one is showing up.
Re: JcomboBox showing only one item
Quote:
Originally Posted by
javapenguin
Just as a thought, could you try the getItemCount() method in a println to check to see how many items are indeed in the ComboBox?
That way you could tell if only one item is being added or if there's more than one but somehow only one is showing up.
The original poster knows only one is being added, which was discussed above (and under what circumstances would a JComboBox have multiple items but only show one?)
Quote:
Originally Posted by ishiro
I just realised that if i delete this part:
I don't see anything obvious which would have the affect you are describing. Again though, I would recommend a) using a PreparedStatement b) if you have not yet, print out the query you are executing to be sure it is what you think it is c) create different ActionListeners per component to separate the behaviors