user5942674
user5942674

Reputation:

JavaFX: Add textField and Labels to scene

I'm trying to get two tabs, each with their own little form that contain labels and textfields/textareas. I thought my making a "Master" VBox that should display everything but nothing other than the tabs are showing and neither am i getting any errors. How do I add them to the scene?

    // Label and Button variables
private Label fname,lname, appt, AppointmentInfo;
protected String input;

@Override
public void start(Stage primaryStage) throws Exception {
    primaryStage.setTitle("Person Appointments");
    primaryStage.setWidth(500);
    primaryStage.setHeight(500);

    //Create and set Tab Names
    Tab InfoTab = new Tab();
    Tab ApptTab = new Tab();
    InfoTab.setText("PersonInfo");
    ApptTab.setText("Appointments");

    // VBoxes
    VBox personInfoBox = new VBox();
    VBox FirstBox = new VBox();
    VBox appointmentBox = new VBox();

    // Setup labels and fields
    fname = new Label();
    lname = new Label();
    appt = new Label();
    AppointmentInfo = new Label();
    fname.setText("First Name");
    lname.setText("Last Name");
    appt.setText("Appt Count");
    AppointmentInfo.setText("Appt Info");

    // Text Fields
    TextField firstNameText = new TextField();
    TextField lastNameText = new TextField();
    TextField appointmentText = new TextField();
    TextArea apptInfo = new TextArea();
    firstNameText.setText("First Name Here");
    lastNameText.setText("Last Name Here");
    appointmentText.setText("Appointment Here");
    personInfoBox.getChildren().add(fname);
    personInfoBox.getChildren().addAll(firstNameText);
    personInfoBox.getChildren().add(lname);
    personInfoBox.getChildren().addAll(lastNameText);
    appointmentBox.getChildren().add(AppointmentInfo);
    appointmentBox.getChildren().addAll(apptInfo);


    // Grid for Tabs
    GridPane grid = new GridPane();
    GridPane grid2 = new GridPane();
    grid.add(fname, 0, 0);
    grid.add(firstNameText, 0, 1);
    grid.add(lname, 1, 0);
    grid.add(lastNameText, 1, 1);
    grid.add(appt, 2, 0);
    grid.add(appointmentText, 2, 1);
    grid2.add(AppointmentInfo, 0, 0);
    grid2.add(apptInfo, 0, 1);
    InfoTab.setContent(grid);
    ApptTab.setContent(grid2);

    // TabPane
    TabPane tabPane = new TabPane();
    tabPane.getTabs().add(InfoTab);
    tabPane.getTabs().add(ApptTab);
    InfoTab.setClosable(false);
    ApptTab.setClosable(false);


    InfoTab.setContent(personInfoBox);
    ApptTab.setContent(appointmentBox);
    VBox one = new VBox(tabPane, personInfoBox);
    Scene scene = new Scene(one, 500, 500);
    primaryStage.setScene(scene);
    primaryStage.show();`

Upvotes: 1

Views: 5633

Answers (1)

jns
jns

Reputation: 6952

You can't assign a Node more than once to the SceneGraph:

A node may occur at most once anywhere in the scene graph. Specifically, a node must appear no more than once in all of the following: as the root node of a Scene, the children ObservableList of a Parent, or as the clip of a Node.

http://docs.oracle.com/javase/8/javafx/api/javafx/scene/Node.html

You assigned e.g. lname to personInfoBox AND grid. So finally lname will be assigned to grid only. Then you set grid as content for a Tab and after that you set personInfoBox as content, which is now empty

Upvotes: 3

Related Questions