如何将数据库数据导入javafx中的组合框 [英] How to import database data into combo box in javafx

查看:197
本文介绍了如何将数据库数据导入javafx中的组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此代码我初始化组合框

Using this code I Initialized Combo box

@FXML
private ComboBox category;

并使用以下方式获取价值:

And get value using:

String Category = category.getValue().toString();

并将值插入mysql数据库。
现在在类别组合框中插入下一个值之前,我需要将数据库中的值导入到组合框中的下拉列表中,值应该显示在组合框中。

And inserted value to mysql database. Now before inserting next value in category Combo box I need to import the values in the database to the drop down in Combo Box and value should be displayed in the combo box.

推荐答案

我建议从数据库中读取值并将其保存到ObservableList中,一旦获得所有值,就可以填充组合框:

I recommend to read the values from the database and save it into a ObservableList, once you get all the values you can fill the combobox with:

 combobox.setItems(myObservableList);

如果你的组合框的类型不是字符串你应该使用字符串转换器,例如,如果你想用组合名称填充组合框,用户是一个类并命名属性,你只需要:

if your type of combobox is not "String" you should use a String converter, for example, if you wanna fill the combobox with the name of users, being "user" a class and name an attribute, you just have to:

myCombo.setConverter(new StringConverter<user>() {

            @Override
            public String toString(user object) {
                return object.getName();
            }

            @Override
            public user fromString(String string) {
                // TODO Auto-generated method stub
                return null;
            }
        });

这篇关于如何将数据库数据导入javafx中的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆