我这里有一个问题,combobox2中的项目不是被替换,而是堆栈。我试图将bookss.removeAllItems();放在bookss.addItem(Names)之上;但每当我单击combobox1中的其他项时,都会得到一个错误。
这是我的代码
String caa = categorys.getSelectedItem().toString();
if ("Fiction".equals(caa))
{
String acct = categorys.getSelectedItem().toString();
try{
pst = con.prepareStatement("select * from books where category=?");
pst.setString(1, acct);
ResultSet rsf = pst.executeQuery();
rsf.next();
try{
rsf = stmt.executeQuery("select * from BOOKS WHERE category='Fiction'");
while(rsf.next())
{
String names = rsf.getString("book_title");
bookss.addItem(names);
}
}catch(SQLException err){
System.out.println(err);
}
}catch(SQLException e){
JOptionPane.showMessageDialog(null, e);
}
}
if ("Horror".equals(caa))
{
String acct = categorys.getSelectedItem().toString();
try{
pst = con.prepareStatement("select * from books where category=?");
pst.setString(1, acct);
ResultSet tts = pst.executeQuery();
tts.next();
try{
tts = stmt.executeQuery("select * from BOOKS WHERE category='Horror'");
while(tts.next())
{
String names = tts.getString("book_title");
bookss.addItem(names);
}
}catch(SQLException err){
System.out.println(err);
}
}catch(SQLException e){
JOptionPane.showMessageDialog(null, e);
}
}发布于 2014-12-02 22:22:06
使用以下命令:
DefaultComboBoxModel model = (DefaultComboBoxModel) ComboBox.getModel();
model.removeAllElements();https://stackoverflow.com/questions/26365558
复制相似问题