programmer
programmer

Reputation: 11

How to create new items and show in scene JavafX?

I want to add and remove new items on menu click. Now here is my menu

    Menu m = new Menu("Click For Options"); 

    // create menuitems 
    MenuItem m1 = new MenuItem("Coin Calculator"); 
    m.getItems().add(m1); 

Now I have a scene but in the same scene on clicking m1 I want to create textArea and add to scene and validate user input on click OK

 //create event for menu items
    m1.setOnAction(e-> {
        
        TextArea text1=new TextArea("Enter total coin to be exchanged");
        TextArea text2=new TextArea("Enter coin type to exchange to");
        VBox newBox=new VBox();
        newBox.getChildren().removeAll();
        newBox.getChildren().addAll(text1,text2);
        Group group = new Group(newBox);
         newBox.getChildren().addAll(group);
    });

Its giving long lists of exceptions

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: cycle detected: parent = VBox@313448d4, node = Group@2beac4d5
at javafx.graphics/javafx.scene.Parent$3.onProposedChange(Parent.java:549)
at javafx.base/com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:234)
at javafx.base/com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:103)
at assignment1/com.york.algorithm.CoinSorterGUI.lambda$0(CoinSorterGUI.java:101)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.controls/javafx.scene.control.MenuItem.fire(MenuItem.java:465)
.....

Upvotes: 0

Views: 195

Answers (1)

Ranjit Vamadevan
Ranjit Vamadevan

Reputation: 534

You can do like this;

TextArea text1=new TextArea("Enter total coin to be exchanged");
TextArea text2=new TextArea("Enter coin type to exchange to");
VBox newBox=new VBox();
Group group = new Group();

m1.setOnAction(e-> { 
        group.getChildren().clear();
        group.getChildren().addAll(text1,text2 );
        newBox.getChildren().clear();
        newBox.getChildren().add(group);
    });

Upvotes: 1

Related Questions