2016-09-01 8 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(); 
        } 
      } 
     }     
}); 

私は、フォローコード "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

どうもありがとうございました。私はあなたの助けに感謝します!!! –

関連する問題