java.lang.IllegalArgumentException: Children: duplicate children added: parent = VBox@872be7

断了今生、忘了曾经 提交于 2020-01-11 02:27:27

问题


Am trying to read some values from MySQL database and display it in a Menu in JavaFX. My code is below. Previously I used Buttons instead of Menu. It worked fine, but I don't felt better with Buttons, so I decided to use Menu. I got the exception mentioned in the heading when I Ran this code.

private VBox userSelection() throws ClassNotFoundException, SQLException {

    VBox vb1 = new VBox();
    vb1.setPadding(new Insets(40, 150, 20, 200));
    vb1.setSpacing(20);

    MenuBar menuBar = new MenuBar();
    Menu menuFile1 = new Menu("CHOOSE YOUR ACCOUNT");
    menuFile1.setStyle("-fx-border-color: green; -fx-font-size: 14pt; "
            + "-fx-font-family: Comic Sans MS; -fx-padding: 1 10 1 1");

    Text scenetitle2 = new Text("Choose Your Account");
    scenetitle2.setFont(Font.font("Tahoma", FontWeight.BOLD, 20));
    scenetitle2.setUnderline(true);
    vb1.getChildren().addAll(scenetitle2);

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

    rs = statement.executeQuery("select * from user");

    while (rs.next()) {

        String username = rs.getString("staffname");

        MenuItem add = new MenuItem(username);
        add.setStyle(" -fx-font-size: 18pt; "
            + "-fx-font-family: Calibri; -fx-padding: 1 10 1 1");
        menuFile1.getItems().addAll(add);
        menuBar.getMenus().addAll(menuFile1);

        vb1.getChildren().addAll(menuBar);
    }

    return vb1;
}

The exception details are :

java.lang.IllegalArgumentException: Children: duplicate children added: parent = VBox@872be7
at javafx.scene.Parent$1.onProposedChange(Parent.java:307)
at com.sun.javafx.collections.VetoableObservableList.addAll(VetoableObservableList.java:106)
at com.sun.javafx.collections.ObservableListWrapper.addAll(ObservableListWrapper.java:160)
at com.sun.javafx.collections.ObservableListWrapper.addAll(ObservableListWrapper.java:309)
at frontpage.FrontPage.userSelection(FrontPage.java:466)
at frontpage.FrontPage.access$100(FrontPage.java:44)
at frontpage.FrontPage$2.handle(FrontPage.java:129)
at frontpage.FrontPage$2.handle(FrontPage.java:125)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Node.fireEvent(Node.java:6867)
at javafx.scene.control.Button.fire(Button.java:179)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:336)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3311)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3151)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3106)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2248)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:530)
at com.sun.glass.ui.View.notifyMouse(View.java:924)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
at java.lang.Thread.run(Thread.java:744)
Exception in thread "JavaFX Application Thread" 

How this exception occured ? How can I correct it ?


回答1:


Every time you iterate through your result set, you are adding the same menu (menuFile1) to the menu bar. You can't add the same menu multiple times.

(If you just look at the stack trace, you can figure that out; I assume line 466 must be:

menuBar.getMenus().addAll(menuFile1);

Just add the menu to the MenuBar once, then add the different menu items to it inside the loop.

Similarly, for some reason, you repeatedly add the menu bar to the VBox. Just add it once, outside the loop.



来源:https://stackoverflow.com/questions/23044935/java-lang-illegalargumentexception-children-duplicate-children-added-parent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!