2017-03-02 13 views
0

私はjavafxで新しくなっています。これで解決策を思いつくことはできません。javafxのComboboxにmysqlデータをバインドする

私はmysqlテーブルpeopleを持っています。

------------------ 
id  | name 
------------------ 
int,ai,pk | string 
------------------ 

私はコンボボックスにnameのリストだけをデータを取り込むために、その後、私はコンボボックスをクリックするたびに、値はidでなければなりませんしたいです。私を助けてください。

答えて

0

id_valuenameの関連idでこのデモを、試してみてください。

public class PopulateComboBoxDemo extends Application { 

    private ComboBox<String> people = new ComboBox<>(); 
    private List<String> ids = new ArrayList<>(); 

    @Override 
    public void start(Stage primaryStage) { 
     this.populateData(); 

     BorderPane root = new BorderPane(); 
     root.setCenter(people);  
     Scene scene = new Scene(root, 300, 250); 

     people.setOnAction(e -> { 
      int index = people.getSelectionModel().getSelectedIndex(); 
      //here is the id_value 
      String id_value = ids.get(index); 
      System.out.println("The id of " + people.getItems().get(index) + " is : " + id_value); 
      // 
      //......... 
      // 
     });  
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    private void populateData() { 
     this.people.getItems().clear(); 
     this.ids.clear(); 
     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/database","user","password"); 
      String sql = "select name, id from person"; 
      PreparedStatement st = con.prepareStatement(sql); 
      ResultSet rs = st.executeQuery(); 
      int index = 0; 
      while(rs.next()) { 
       this.people.getItems().add(index, rs.getString("name")); 
       this.ids.add(index, String.valueOf(rs.getInt("id"))); 
       index++; 
      } 
      con.close(); 
     } catch (ClassNotFoundException | SQLException ex) { 
      Logger.getLogger(PopulateComboBoxDemo.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
    public static void main(String[] args) { 
     launch(args); 
    } 
} 
1

あなたは、リレーショナル・データを管理するためにJPAを使用している場合、このコードは、仕事をする必要があり、そうでなければ、最初のオブジェクトに、テーブルの行をマップする必要があります。 幸運を祈る!

List<People> PeopleList = em.createQuery("SELECT p FROM People p").getResultList(); 
    ObservableList<People> peopleData = FXCollections.observableList(PeopleList); 
    PeopleList.add(null); 

yourCombo.setCellFactory((comboBox) -> { 
     return new ListCell<People>() { 
      @Override 
      protected void updateItem(People item, boolean empty) { 
       super.updateItem(item, empty); 

       if (item == null || empty) { 
        setText("Select"); 
        yourCombo.getSelectionModel().clearSelection(); 
       } else { 
        setText(item.getName(); 
       } 
      } 
     }; 
    }); 


     yourCombo.setConverter(new StringConverter<People>() { 
     @Override 
     public String toString(People people) { 
      if (people == null) { 
       return "Select"; 
      } else { 
       return people.getName(); 
      } 
     } 

     @Override 
     public People fromString(String nameString) { 
      return null; // No conversion fromString needed. 
     } 
    }); 



    yourCombo.setItems(peopleData); 
+0

ご返信ありがとうございます。後であなたのコードをチェックします。JPA cuzを使うつもりなら、私は人のために私のカスタムコードエンティティを使っています。私はjpaを最初に勉強しようとしています..あまり前もってありがとう.. –

+0

あなたは大歓迎です! –

関連する問題