JavaFX从MySql填充ComboBox

时间:2017-11-18 23:20:38

标签: java mysql jdbc combobox scenebuilder

我正在使用Java Fx和Scene构建器,我的代码运行没有异常,但组合框仍然是空的,我无法理解为什么。我设置了身份ID和OnAction字段,但它仍然不起作用。这是我的控制器:

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.lang.String;
import java.sql.PreparedStatement;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.stage.Stage; 

public class AdaugareSediuController {

    @FXML
    ComboBox<String> meniu;

    public void meniuCombo() throws SQLException{

        try {

            if(Conexiune.conexiuneBd !=null){
                String query = "SELECT * FROM maginfo";
                PreparedStatement dindb = Conexiune.conexiuneBd.prepareStatement(query);
                ResultSet rezultate = dindb.executeQuery();

                while (rezultate.next()) {
                    String Nume = rezultate.getString("nume");
                    ObservableList<String> list = FXCollections.observableArrayList(Nume);
                    meniu.getItems().addAll(list);
                }

                dindb.close();
                rezultate.close();   
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}

0 个答案:

没有答案