2

1) When I run it, all the objects placed in the TOP_CENTER as you can see in the code I tried to place the button in the BOTTOM_RIGHT and it didn't work.

2) Can a Scene include two layouts? (Two VBox-es for example).

public void NewQuestion ()
{
    sum++;
    t=new Text("Question number: "+sum);

    textfield=new TextField();
    pane = new VBox();

    Button NextQuestion = new Button ("Next Question");
    NextQuestion.setOnAction(e-> NextQuestionButtonClicked(e));
    pane.getChildren().addAll(t, textfield, NextQuestion);
    pane.setAlignment(Pos.TOP_CENTER);
    NextQuestion.setAlignment(Pos.BOTTOM_RIGHT);//<---


    Scene mainscene = new Scene(pane,420, 530);


    Qstage.setScene(mainscene);


}
Naor Levi
  • 37
  • 1
  • 1
  • 4

2 Answers2

4

1) button.setAlignment sets how the text and icons are places inside the button (javadoc). To align the button within a pane you should apply desired alignment to the pane itself. In you case:

pane.setAlignment(Pos.BOTTOM_RIGHT);

instead of

pane.setAlignment(Pos.TOP_CENTER);

2) The Scene should have a single root. But the root itself may be a VBox or HBox and you could put multiple boxes inside other box.

kgeorgiy
  • 1,477
  • 7
  • 9
  • i want to place only the button in the BOTTOM_RIGHT and the rest i want to place in the TOP_CENTER – Naor Levi Nov 06 '16 at 09:23
  • In this case you need a [BorderPane](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/BorderPane.html) and place main content to the center, and the button to the bottom. – kgeorgiy Nov 06 '16 at 09:36
  • @kgeorgiy i don't think that's correct. i am still learning but according to the javafx documentation this method is to position **The overall alignment of children within the hbox's width and height.** and **NOT** the text inside the button https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/HBox.html – Ishan Srivastava Jul 14 '17 at 12:28
  • @NaorLevi you have been given wrong information. I think ishan has given you the correct answer –  Jul 14 '17 at 13:04
3

Answering your second question first : each layout is a node so you can make a Hbox and then add 2 Vboxs to it and in a similar fashion have all combinations of it. since each button is also a node, you can add a Vbox and a button to an Hbox to position them horizontally with respect to each other

Now back to your first question: Here is a list and an example with Pos.CENTER and Pos.CENTER_LEFT : enter image description here

Hope this helps. Feel free to ask any more questions about this

Community
  • 1
  • 1
Ishan Srivastava
  • 1,129
  • 1
  • 11
  • 29