我有两个组合框...第一个显示酒店的城市,第二个显示根据第一个组合框中选择的城市的酒店名称。 First Combobox工作正常。第二个节目仅适用于第一个选定的城市。在第一个组合框中更改城市后,它不会对自身进行更改!我需要添加到此代码中,以便能够通过更改第一个组合框来查看第二个组合框中的更改。
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/JEREN","root","");
Statement stat = con.createStatement();
String City_Selected = jComboBox1.getSelectedItem().toString();
String select_HN = "select name from tbl_Hotel where city = '"+City_Selected+"';";
ResultSet rs = stat.executeQuery(select_HN);
while (rs.next())
{
String hotel_name = rs.getString("name");
// jLabel3.setText(hotel_name);
jComboBox2.addItem(hotel_name);
}//end while
rs.close();
stat.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
答案 0 :(得分:0)
喜欢这个吗?
jComboBox1.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
jComboBox2.removeAllItems();
fillComboBoxes();
}
});
private void fillComboBoxes(){
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/JEREN","root","");
Statement stat = con.createStatement();
String City_Selected = jComboBox1.getSelectedItem().toString();
String select_HN = "select name from tbl_Hotel where city = '"+City_Selected+"';";
ResultSet rs = stat.executeQuery(select_HN);
while (rs.next())
{
String hotel_name = rs.getString("name");
// jLabel3.setText(hotel_name);
jComboBox2.addItem(hotel_name);
}//end while
rs.close();
stat.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}