Issue
I am trying to place Hboxes inside a Vbox, which means each Hbox should be a row, but I am getting this result
The label first name and its corresponding text box should be one row, and the last name should be another row.
What can I do to make each hbox become a separate row in the vbox.
My code:
public class App extends Application {
Button btn1=new Button("Add");
Button btn2=new Button("Subtract");
Button btn3=new Button("Multiply");
Button btn4=new Button("Devide");
Button reset=new Button("reset"); //reset X?
TextField T1=new TextField();
TextField T2=new TextField();
TextField T3=new TextField();
Label L1=new Label("Number 1: ");
Label L2=new Label("Number 2: ");
Label L3=new Label("Result: ");
Label L=new Label();
Label I=new Label();
Button create=new Button("Create");
Button update=new Button("Update");
Label idlabel=new Label("Id ");
Label fname=new Label("First Name ");
Label lname=new Label("Last Name ");
TextField firstName=new TextField();
TextField lastName=new TextField();
@Override
public void start(Stage primaryStage)
{
HBox H4=new HBox();
H4.getChildren().add(create);
H4.getChildren().add(update);
H4.setAlignment(Pos.CENTER);
H4.setSpacing(3);
VBox V=new VBox();
V.getChildren().add(H4);
V.setSpacing(6);
HBox fnameBox=new HBox();
fnameBox.getChildren().add(fname);
fnameBox.getChildren().add(firstName);
fnameBox.setAlignment(Pos.CENTER);
fnameBox.setSpacing(5);
HBox lnameBox=new HBox();
fnameBox.getChildren().add(lname);
fnameBox.getChildren().add(lastName);
fnameBox.setAlignment(Pos.CENTER);
fnameBox.setSpacing(5);
VBox Form=new VBox();
Form.getChildren().add(fnameBox);
Form.getChildren().add(lnameBox);
create.setOnAction((e->
{
Scene scene = new Scene (Form, 500, 750);
primaryStage.setTitle("Create Student");
primaryStage.setScene(scene);
}
));
Scene scene = new Scene (V, 500, 750);
primaryStage.setTitle("calculator");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
Solution
Here is your problem
HBox lnameBox=new HBox();
fnameBox.getChildren().add(lname);
fnameBox.getChildren().add(lastName);
fnameBox.setAlignment(Pos.CENTER);
fnameBox.setSpacing(5);
You create lnameBox
but use fnameBox
instead. So lnameBox
does not have children
Answered By - alex valuiskyi
Answer Checked By - Clifford M. (JavaFixing Volunteer)