Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
组合框中未显示的所有值 试试看{ Class.forName(“com.mysql.jdbc.Driver”); connect=DriverManager .getConnection(“jdbc:mysql://localhost:3306/project?" +“用户=根用户&密码=病毒”); 语句=connect.createStatement(); preparedStatement=connect .prepareStatement(“从主题中选择子名称”); rs=preparedStatement.executeQuery(); while(rs.next()){ 主题=rs.getString(“子名称”); ObservableList options1=FXCollections.observableArrayList(主题); ComboBox 1=新的ComboBox(选项1); } }catch(ClassNotFoundException | SQLException e){ 投掷e; }最后{ close2(); } comboBox1.setPrompText(“选择主题”); comboBox1.setPrefSize(280,30);_Mysql_Combobox_Javafx_Javafx 2 - Fatal编程技术网

组合框中未显示的所有值 试试看{ Class.forName(“com.mysql.jdbc.Driver”); connect=DriverManager .getConnection(“jdbc:mysql://localhost:3306/project?" +“用户=根用户&密码=病毒”); 语句=connect.createStatement(); preparedStatement=connect .prepareStatement(“从主题中选择子名称”); rs=preparedStatement.executeQuery(); while(rs.next()){ 主题=rs.getString(“子名称”); ObservableList options1=FXCollections.observableArrayList(主题); ComboBox 1=新的ComboBox(选项1); } }catch(ClassNotFoundException | SQLException e){ 投掷e; }最后{ close2(); } comboBox1.setPrompText(“选择主题”); comboBox1.setPrefSize(280,30);

组合框中未显示的所有值 试试看{ Class.forName(“com.mysql.jdbc.Driver”); connect=DriverManager .getConnection(“jdbc:mysql://localhost:3306/project?" +“用户=根用户&密码=病毒”); 语句=connect.createStatement(); preparedStatement=connect .prepareStatement(“从主题中选择子名称”); rs=preparedStatement.executeQuery(); while(rs.next()){ 主题=rs.getString(“子名称”); ObservableList options1=FXCollections.observableArrayList(主题); ComboBox 1=新的ComboBox(选项1); } }catch(ClassNotFoundException | SQLException e){ 投掷e; }最后{ close2(); } comboBox1.setPrompText(“选择主题”); comboBox1.setPrefSize(280,30);,mysql,combobox,javafx,javafx-2,Mysql,Combobox,Javafx,Javafx 2,这是我从表中读取多个值并将其显示在组合框中的代码。实际上有3个值要显示。while循环工作正常。每次它从表中读取每个值并将其放入组合框中 但是,当下一个值出现时,它与上一个值重叠,因此组合框中只显示一个值,即上次读取的值。但是我需要在组合框中显示所有的值,也就是说,当一个新值出现时,我需要将它作为一个新条目添加,而不是与以前的值重叠 我怎么做 值-属性用于当前选定的项目。您必须将结果添加到项目-可观察列表中: try { Class.forName("com.mysql.j

这是我从表中读取多个值并将其显示在组合框中的代码。实际上有3个值要显示。while循环工作正常。每次它从表中读取每个值并将其放入组合框中

但是,当下一个值出现时,它与上一个值重叠,因此组合框中只显示一个值,即上次读取的值。但是我需要在组合框中显示所有的值,也就是说,当一个新值出现时,我需要将它作为一个新条目添加,而不是与以前的值重叠


我怎么做

值-属性用于当前选定的项目。您必须将结果添加到
项目
-可观察列表中:

   try {
        Class.forName("com.mysql.jdbc.Driver");
        connect = DriverManager
                .getConnection("jdbc:mysql://localhost:3306/project?"
                        + "user=root&password=virus");
        statement = connect.createStatement();

        preparedStatement = connect
                .prepareStatement("select subname from subject");
        rs=preparedStatement.executeQuery();

        while (rs.next()) {
            subject = rs.getString("subname");
            ObservableList<String> options1 = FXCollections.observableArrayList(subject);
            comboBox1 = new ComboBox(options1);
        }
        } catch (ClassNotFoundException | SQLException e) {
        throw e;
    } finally {
        close2();
    }

    comboBox1.setPromptText("Select Subject");
    comboBox1.setPrefSize(280, 30);
如果要显示提示文本,请使用以下命令:

List items = comboBox1.getItems(); // java.util.List by the way
items.add("item1"); // use the values from result set here instead
items.add("item2");
//...
当然,您不会为
ResultSet
中的每一行创建一个组合框,而是为所有行创建一个组合框:

comboBox1.setPromptText("your prompt text");
/。。。
rs=preparedStatement.executeQuery();
ArrayList子名称=新的ArrayList();
//将所有“子名称”值添加到列表中
while(rs.next()){
subnames.add(rs.getString(“subname”);
}
//从子名称列表创建ObservableList
ObservableList选项1=FXCollections.observableArrayList(子名称);
ComboBox 1=新的ComboBox(选项1);
//...

javafx中是否有列表控件?@Tom,有一个名为
ListView
javafx.scene.control.control
,如果这就是您的意思(?)@Tom如果您想在ListView中显示条目,请阅读位于的教程。顺便说一下,
ListView
ComboBox
的代码几乎相同
ListView
还有一个属性
items
,因此还有函数
getItems()
//...
rs = preparedStatement.executeQuery();
ArrayList<String> subnames = new ArrayList<>();

// add all "subname" values to a List
while (rs.next()) {
    subnames.add(rs.getString("subname"));
}

// create a ObservableList from the subnames List
ObservableList<String> options1 = FXCollections.observableArrayList(subnames);
comboBox1 = new ComboBox(options1);
//...