Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX Have multiple Panes in one scene?

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?

like image 462
Hatefiend Avatar asked Oct 26 '15 06:10

Hatefiend


People also ask

Can a scene have multiple panes JavaFX?

The Scene it self can only have one root Pane. So if you want 2 panes in the Scene you need 3. 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.

What is the difference between HBox and VBox pane?

HBox is a subclass of Pane that lays out its children next to each other, in a horizontal row. VBox is a subclass of Pane that lays out its children in vertical column. An HBox might be used as the bottom component in a BorderPane, making it possible to line up several components along the bottom of the border pane.

What is HBox layout pane?

HBox. The HBox layout arranges all the nodes in our application in a single horizontal row. The class named HBox of the package javafx. scene. layout represents the text horizontal box layout.

Which JavaFX layout pane lays out its children consecutively one after another?

FlowPane. The nodes within a FlowPane layout pane are laid out consecutively and wrap at the boundary set for the pane.


1 Answers

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

like image 106
Marcel Avatar answered Sep 22 '22 14:09

Marcel