JavaFX + SQL:为SQL数据库的每一行添加1个带有3个组合框的新HBox

时间:2019-06-06 20:36:40

标签: java mysql javafx

我正在一个项目上,我需要为MySQL数据库的每一行添加一个带有3个ComboBox的HBox到VBox。

我已经尝试过一些方法,但是总是出错。所以我尝试了诸如动态变量名之类的方法。

@FXML
    private VBox mon_routes;

public void initialize(URL url, ResourceBundle rb) {
        PreparedStatement pr;
        ResultSet rs;
        String sql = "SELECT * FROM routes";

        try {
            pr = this.connection.prepareStatement(sql);
            rs = pr.executeQuery();
            int n = 1;
            HBox r[] = new HBox[n];
            while (rs.next()) {
                r[n].getChildren().add(new CheckBox());
                r[n].getChildren().add(new ComboBox());
                r[n].getChildren().add(new ComboBox());
                mon_routes.getChildren().add(r[n]);
                n++;
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
    }

我希望获得具有7个HBox的VBox,因为我的数据库有7行。 但是我只会遇到很多错误。

javafx.fxml.LoadException: 
unknown path

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425)
    at loginapp.LoginController.adminLogin(LoginController.java:110)
    at loginapp.LoginController.login(LoginController.java:92)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$358(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at adminapp.AdminController.initialize(AdminController.java:56)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 61 more

Process finished with exit code 0

2 个答案:

答案 0 :(得分:1)

除非必须这样做,否则应避免使用Arrays。我会使用List中的HBoxes。您的代码应类似于以下代码。阅读代码中的注释。

VBox mon_routes = new VBox(); //You never create the VBox. This leads to a null pointer exception.

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
    PreparedStatement pr;
    ResultSet rs;
    String sql = "SELECT * FROM routes";

    try {
        pr = this.connection.prepareStatement(sql);
        rs = pr.executeQuery();

        List<HBox> r = new ArrayList();//Avoid Arrays. Use List instead.

        while (rs.next()) {
            HBox tempHBox = new HBox();//Create an HBox to store into the List or Array if you continue to use the Array.
            tempHBox.getChildren().add(new CheckBox(rs.getString(0)));//You never retrieve anything from the result set. <- rs.getSomething(). Add the CheckBox to the HBox
            tempHBox.getChildren().add(new CheckBox(rs.getString(1)));//You never retrieve anything from the result set. <- rs.getSomething(). Add the CheckBox to the HBox
            tempHBox.getChildren().add(new CheckBox(rs.getString(2)));//You never retrieve anything from the result set. <- rs.getSomething(). Add the CheckBox to the HBox
            r.add(tempHBox);//Add the HBox to the List
        }

        mon_routes.getChildren().addAll(r);//Add the List of HBox to the VBox.
    } catch (SQLException e1) {
        e1.printStackTrace();
    }
}  

答案 1 :(得分:0)

r[]的大小为1,添加第二个hbox会使事情中断。您可能只想创建一个List<HBox>。最好的办法可能是为要重复的组件和一个控制器创建一个FXML。创建一个基于传入的行来初始化值的方法。那么您只需创建该自定义视图并将其添加到每行的VBox中即可。