7

I am trying to make an application which will have a date at the top (always automatically centered) and content at the bottom which is not going to be aligned to any direction.

I figured the best way to do this would be to have:

Pane normalLayout = new Pane();
StackPane centeredLayout = new Stackpane();
Label centeredText = new Label("I want this text centered!");
Button unorganizedButton = new Button("Press me");
centeredLayout.getChildren().add(centeredText);
normalLayout.getChildren().add(unorganizedButton);

But then I can't do something like:

Scene myScene = new Scene(centeredLayout, normalLayout, 500, 500);
Window myWindow = new Window();
myWindow.setScene(myScene);
myWindow.show();

So how can this be done? How can multiple panes exist on the same scene?

Hatefiend
  • 3,416
  • 6
  • 33
  • 74
  • 1
    `Scene myScene = new Scene(new VBox(centeredLayout, normalLayout), 500, 500);` ? – Uluk Biy Oct 26 '15 at 06:50
  • @UlukBiy So a scene MUST have only one pane at a time, but that pane can have multiple panes inside of it? Is doing: `new VBox(centeredLayout, normalLayout)` the same as doing `VBox myLayout = new VBox(); myLayout.getChildren().add(centeredLayout, normalLayout);` ? – Hatefiend Oct 26 '15 at 07:38

2 Answers2

14

The Scene it self can only have one root Pane. So if you want 2 panes in the Scene you need 3.

Scene  
  |   
  V
Root Pane (Vbox for example)
  |                   |
  V                   V
Pane1                Pane2

In your code this can look like this:

StackPane rootPane = new StackPane();
Scene scene = new Scene(rootPane,...);
Pane pane1 = new Pane();
Pane pane2 = new Pane();
rootPane.getChildren().addAll(pane1,pane2);

Depending on how your Application should be layouted you have to choose the right Pane implementations.

As a little Tip to get familiar whit all the Layout Containers try the SceneBuilder Application. http://gluonhq.com/open-source/scene-builder/

Maybe this link will help you understanding how layouting works in JavaFX: http://docs.oracle.com/javafx/2/scenegraph/jfxpub-scenegraph.htm https://docs.oracle.com/javafx/2/layout/builtin_layouts.htm

Marcel
  • 1,606
  • 16
  • 29
  • Thank you for this information. I have one question. In SceneBuilder, how do I see the code generated for the application? I'd like to make something, then read from the code to see how it is done. – Hatefiend Oct 26 '15 at 10:07
  • SceneBuilder produces no Java Code. It produces FXML files. See here: http://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm Over fxml you can create your SceneGraph in "xml" format. – Marcel Oct 26 '15 at 10:17
  • Man that's a bummer, because it would have been nice to learn from it a bit better. I do see however how it makes understanding your build plan easier though. So let's say I started out with a BorderPane, and I wanted a Menu Bar with Menu items at the top, but right under that and STILL in the Top section of BorderPane, I wanted to add a button and text beside it. How would I dictate that I want the menu bar right ontop of my button, as it would make no sense to have a menu bar right next to a button, etc. – Hatefiend Oct 26 '15 at 10:26
  • Take a Look at the AnchorPane its the easiest LayoutContainer. There you would say the Menubar has an top/left/right Anchor of 0.0. Your buttons and Text I would wrap into an Vbox/Hbox and the VBox gets an Top Anchor of the height of the Menubar. – Marcel Oct 26 '15 at 10:53
  • 1
    @Hatefiend If an answer has solved your problem, it is a good practice to accept it. It helps future users' who have similar problem and are looking for solution. – ItachiUchiha Oct 26 '15 at 11:27
  • @ItachiUchiha Sorry, I'm new to SO's system. I've done so now. Thank you! – Hatefiend Oct 26 '15 at 11:56
2

I would suggest you to create a "root"-Pane. In your case, you could use a BorderPane.

Example:

BorderPane root = new BorderPane();

Label centeredText = new Label("I want this text centered!");
Button unorganizedButton = new Button("Press me");

BorderPane.setAlignment(centeredText, Pos.CENTER);

root.setTop(centeredText);
root.setBottom(unorganizedButton);

Afterwards just call the constructor with the newly created pane.

Scene scene = new Scene(root, 500, 500);

Addition:

You could also just set new panes.

AnchorPane anchorPane = new AnchorPane();
root.setTop(anchorPane);
Yannick Rot
  • 196
  • 2
  • 12