JavaFX - Dealing with duplicate children error - RadioButtons


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

I had referred to this guide: 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>() {
    public void handle(ActionEvent event) {

            //Remove all existing children
        } catch(Exception e)
            //Add email and email text into a horizontal box
            hb.getChildren().addAll(email, email_text);
            //Added the box to the vertical box

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


final ToggleGroup group = new ToggleGroup();

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

final RadioButton test2 = new RadioButton("Test 2");
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?

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.

10/15/2014 4:18:33 PM

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