【问题标题】:Binding mysql data into Combobox in javafx将mysql数据绑定到javafx中的Combobox
【发布时间】:2017-07-21 22:10:13
【问题描述】:

我是 javafx 的新手,我无法解决这个问题。

我有mysql表people

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

我只想将数据填充到组合框name 的列表中,然后每次单击组合框时,值应该是id。请帮帮我。

【问题讨论】:

    标签: java mysql javafx combobox


    【解决方案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);
    

    【讨论】:

    • 感谢您的快速回复。如果我要使用 JPA,我稍后会检查你的代码因为目前我正在为人们使用我的自定义代码实体.. 我要先学习 jpa.. 非常感谢你提前..
    【解决方案2】:

    试试这个演示!其中,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);
        }   
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 2013-07-17
      • 2020-09-24
      相关资源
      最近更新 更多