JavaFX - Dealing with duplicate children error - RadioButtons


Question

I have three radio buttons, I want my GUI to be uploaded depend on the checkbox I click.

I had referred to this guide: http://docs.oracle.com/javafx/2/ui_controls/radio-button.htm which seems ideal for my needs. However, it cannot recognize the type despite importing the correct libraries. I decided to write my own little code.

We have horizontal boxes and vertical boxes. I have added the labels and text elements into one horizontal box (indicated by hb, hb1, hb2..) and clubs them all together in one vertical box.

Here is my code for event listener for my radio button:

test1.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {

        try{
            //Remove all existing children
            vb.getChildren().removeAll(hb,hb2,hb3,hb4,hb5);
            log("Cleared");
        } catch(Exception e)
        {
            log(e.getMessage());
        }
        try{
            //Add email and email text into a horizontal box
            hb.getChildren().addAll(email, email_text);
            hb.setSpacing(10);
            //Added the box to the vertical box
            vb.getChildren().add(hb);

            log("Added the email input box");
        }
        catch(Exception e){
            log(e.getMessage());
        }
    }
});

Declarations:

final ToggleGroup group = new ToggleGroup();

final RadioButton test1 = new RadioButton("Test 1");
test1.setToggleGroup(group);
grid.add(test1, 0, 1);

final RadioButton test2 = new RadioButton("Test 2");
test2.setToggleGroup(group);
grid.add(test2, 0, 2);

final  VBox vb = new VBox(10); // main container
final HBox hb = new HBox(); // Email , Email Text
final HBox hb2 = new HBox(); // Corporate , Corporate Name

I am getting an error : Children: duplicate children added: parent = HBox@359889fd. I did a little research on this : java.lang.IllegalArgumentException: Children: duplicate children added: parent = VBox@872be7 . Honestly, even if we both are working on the same issue, I am having a real hard time getting his code.

My main issue: If I am removing ALL the element in the beginning, why is there a duplicate error?

Perhaps, my code may be completely wrong and made no sense. Do you recommend any other approach?

1
0
7/10/2018 1:48:37 PM

Accepted Answer

The main issue was I was removing the vb box but I was not removing the contents of hb. Whereas, I am adding hb. I should have also removed from hb.

0
10/15/2014 4:18:33 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon