2016-09-01 95 views
0

我正在尋求如何執行第一個組合框的ActionPerformed過濾下一個。這是基於Mysql的。 我使用以下代碼來填充第一個組合,其工作正常Java,Mysql:如何連接從屬組合框基於另一個

Vector<String> comboBoxItems = new Vector<String>(); 
final DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>(comboBoxItems); 
     try { 
      new MconnectionDB(); 
     } catch (SQLException e2) { 
      e2.printStackTrace(); 
     } 
     PreparedStatement st1 = null; 
     ResultSet rs1=null; 
     String strPro = ""; 

     String sql1 ="select distinct T_AdressePro from t_adresse order by T_AdressePro"; 

     try { 
      st1= MconnectionDB.con.prepareStatement(sql1); 
      rs1 = st1.executeQuery(); 
     } catch (SQLException e1) { 
      e1.printStackTrace(); 
     } 

     try { 
      while (rs1.next()){ 
       strPro =rs1.getString("T_AdressePro"); 
       comboBoxItems.add(strPro); 
       comboBoxPro= new JComboBox<String>(model); 
      } 

     } catch (SQLException e) { 
     e.printStackTrace(); 
     }finally { 
      try { 
       rs1.close(); 
       st1.close(); 
       new MdeconnectionDB(); 
      } 
      catch (SQLException e) { 
       e.printStackTrace(); 
      } 
     } 

然後,我在第一個組合中添加另一個類似的代碼中的actionPerformed過濾第二個:

comboBoxPro.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent arg0) { 
      Vector<String> comboBoxItemsC = new Vector<String>(); 
      final DefaultComboBoxModel<String> modelC = new DefaultComboBoxModel<String>(comboBoxItemsC); 

      String strCir = ""; 
      PreparedStatement st2 = null; 
      ResultSet rs2=null; 

      String sql2 ="select distinct T_AdresseCir from t_adresse where T_AdressePro=? order by T_AdresseCir"; 

      try { 
       new MconnectionDB(); 
      } catch (SQLException e2) { 
       e2.printStackTrace(); 
      } 
      comboBoxPro.getSelectedItem(); 
      strProCombo = comboBoxPro.getSelectedItem().toString(); 
      System.out.println(strProCombo); 

      try { 

       st2= MconnectionDB.con.prepareStatement(sql2); 
       st2.setString(1, strProCombo); //strProCombo 
       rs2 = st2.executeQuery(); 
      }catch (SQLException e1) { 
       e1.printStackTrace(); 
      } 

      try { 
       while (rs2.next()){ 
        System.out.println(rs2.getString("T_AdresseCir")); 
        strCir =rs2.getString("T_AdresseCir"); 
        comboBoxItemsC.add(strCir); 
        comboBoxCir= new JComboBox<String>(modelC); 
       } 

      } catch (SQLException e) { 
       e.printStackTrace(); 
      }finally { 
       try { 
        rs2.close(); 
        st2.close(); 
        new MdeconnectionDB(); 
        } 
        catch (SQLException e) { 
         e.printStackTrace(); 
        } 
      } 
     }     
}); 

我注意到follogwing代碼「System.out.println(rs2.getString(」T_AdresseCir「));」正在返回預期的結果,但不是組合框。仍然是空的。請你支持,謝謝。

回答

0

在您的actionPerformed方法中,您正在創建新的JComboBox,但不會將其添加到gui中。這可能就是爲什麼你看不到它的內容。您應該改爲創建新的ComboBoxModel並將其設置在現有的JComboBox上。您應該也可以使用try with resources以使您的代碼更具可讀性。

僞代碼(我沒有你的數據庫):

// create new model for your comboBox 
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>(); 

// fill model with data 
try (Connection con = /*get connection to your db by any means necessary*/; 
    PreparedStatement stmt = con.prepareStatement(/*your query*/); 
    ResultSet rs = stmt.executeQuery();) { 

    while (rs.next()) { 
     model.addElement(rs.getString(/*your column*/)); 
    } 

    comboBox.setModel(model); // set model for your JComboBox 

} /*catch and all that*/ 
// no need for finally because try-with-resources. 
+0

非常感謝。我感謝您的幫助!!! –

相關問題