BorderPane spacing between nodes - layout

Is it possible to set a spacing between nodes on a BorderPane? The Swing-equivalent would be hgap and vgap on BorderLayout.
I didn't find anything in the documentation and the only viable workaround I can think of would be to selectively set margins on the child nodes to replicate the effect.

Insets insets = new Insets(10);
BorderPane bp = new BorderPane();
Node topNode = new Label("TOP");
bp.setTop(topNode);
BorderPane.setMargin(topNode, insets);
Node centerNode = new Label("CENTER");
bp.setCenter(centerNode);
BorderPane.setMargin(centerNode, insets);
Node bottomNode = new Label("BOTTOM");
bp.setBottom(bottomNode);
BorderPane.setMargin(bottomNode, insets);
Be aware: this will put a spacing of 20 (10 from top and 10 from center) between top and center.
Similar for the spacing between center and bottom.
The documentation
public static void setMargin(Node child, Insets value)
Sets the margin for the child when contained by a border pane. If set, the border pane will lay it out with the margin space around it. Setting the value to null will remove the constraint.

It's possible to set the padding:
In JFX:
<BorderPane>
<padding>
<Insets top="10" right="10" bottom="10" left="10"/>
</padding>
</BorderPane>
In code:
#FXML
private BorderPane borderPane;
...
this.borderPane.setPadding(new Insets(10, 10, 10, 10));

Related

Setting LayoutParams but its not working android

Trying to create a linear layout programmatically and setting its width and height by layout params. But it seems layout params isn't working.
this is the code:
// CREATING A NEW LINEAR LAYOUT PROGRAMMATICALLY
LinearLayout linearLayout = new LinearLayout(getActivity());
ViewGroup.LayoutParams layoutParams = new
ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(layoutParams);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// CREATING CHILDDRENS (TEXT VIEWS)
TextView name = new TextView(getContext(), null, 0, R.style.item_layout_style);
name.setText("Pine");
TextView qty = new TextView(getContext(), null, 0, R.style.item_layout_style);
qty.setText("10");
TextView cost = new TextView(getContext(), null, 0, R.style.item_layout_style);
cost.setText("785");
TextView tCost = new TextView(getContext(), null, 0, R.style.item_layout_style);
tCost.setText("1000");
// SET TEXT VIEW TO LINEAR LAYOUT
linearLayout.addView(name);
linearLayout.addView(qty);
linearLayout.addView(cost);
linearLayout.addView(tCost);
// SET LINEAR LAYOUT TO PINE LAYOUT
LinearLayout daddy= (LinearLayout) view.findViewById(R.id.layout);
daddy.addView(linearLayout, 2);
// Return the view
return view;
I have root layout (daddy) which has many linear layouts (vertically oriented), but I need to create a linear layout programmatically and add that to "daddy". But the text views are sticked together, they aren't getting the entire space horizontally.
Do help me!
Everything is fine with the code. It is the style that I tried to give to text views, they weren't getting layout weight, width and height. How did I find this out? Well I set the linear layout's background-color to black to see if it's really not getting width and height set by LayoutParams. And I wasn't wrong! Width was set to match parent. So what I did was create a new layout param for text views and set it to them.

Set BorderPane components size

I want to set the size of the Center, Top, Left, Right and Bottom components of a BorderPane.
So far I found this:
BorderPane() mainpane = new BorderPane();
mainPane.getBottom().prefWidth(sizeX);
For example how I can you get the size of a Left side of a BorderPane and set the size?
the "Left" of a BorderPane is whatever you set it to and it's size is whatever you set it to.
example:
BorderPane mainpane = new BorderPane();
StackPane left = new StackPane();
left.setPrefWidth(100);
mainPane.setLeft(left);

JavaFX scene builder questions

I'm working with JavaFX scene builder and have two questions.
Fisrt one:"How to add border to Pane in JavaFX scene builder?"
Second one: "How to split cells in HBox?"
I dont know why you would want to join HBox cells as you can set the resize behaviour for every child of the hbox.
There is a example in HBox's Javadoc:
//For example, if an hbox needs the TextField to be allocated all extra space:
HBox hbox = new HBox();
TextField field = new TextField();
HBox.setHgrow(field, Priority.ALWAYS);
hbox.getChildren().addAll(new Label("Search:"), field, new Button("Go"));
Joining cells would be possible in a GridPane with the row- and/or columnSpan.
GridPane gridpane = new GridPane();
gridpane.add(new Button(), 0, 0, 2, 2); // column=0 row=0, spans over 2 columns and 2 rows
gridpane.add(new Label(), 3, 1); // column=3 row=1 (spans over 1 column and 1 row (default))
row-/columnSpan and the vertical horizontal Grow can be specified in the Properties bar of the Scene Builder, residing on the right side, by default.
You can set border by using setStyle() and use some styles like -fx-border
examples
P.S. styles are the same, as like css, but with -fx- prefix

javafx 2.0 how to add scroll bar in Accordion

the accordion height will increase once the titled pane expanded.
but since the window is a fixed height window, if the height of accordion is bigger than the window height,the content of bottom titled pane will be clipped off.
so how can i show a vertical scroll bar to display those titled panes
is there any way to add a scroll bar in accordion
or i need to put accordion in a control which may show scroll bar when accordion exceed the height the outer control ?
thanks
Add a scroll pane inside the accordion and set the scroll pane size to the size of the accordion minus the heights of the bars. Your scroll pane's contents can then exceed the size of the accordion.
#Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
Accordion accord=new Accordion();
ScrollPane scroll=new ScrollPane();
scroll.setPrefHeight(accord.getHeight());
scroll.prefWidth(accord.getWidth());
TitledPane title=new TitledPane();
title.setText("Accordian");
title.setContent(scroll);
accord.getPanes().add(title);
root.getChildren().add(accord);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Scroll Pane");
primaryStage.setScene(scene);
primaryStage.show();
}

Set scene width and height

I have been trying to set the scene's width and height outside of the constructor and it's been to no avail. After looking through the Scene API I saw a method that lets you get the height and width respectively but not one to set the method.. :s (design flaw maybe).
After further research I came across the SceneBuilder and found methods that could modify the height and width. However, I do not know how to apply it to a scene object already created or how to create a SceneBuilder object that could be used in place of the scene object.
Once you created Scene and assigned it to the Stage you can use Stage.setWidth and Stage.setHeight to change both stage and scene sizes simultaneously.
SceneBuilder can't be applied to an already created object, it can only be used for the scene creation.
It seems not possible to set the size of the Scene after it has been created.
Setting the size of the Stage means to set the size of the window, which includes the size of the decoration. So the Scene is smaller in size, unless the Stage is undecorated.
My solution is to compute the size of the decoration while initialization and add it to the size of the Stage when resizing:
private Stage stage;
private double decorationWidth;
private double decorationHeight;
public void start(Stage stage) throws Exception {
this.stage = stage;
final double initialSceneWidth = 720;
final double initialSceneHeight = 640;
final Parent root = createRoot();
final Scene scene = new Scene(root, initialSceneWidth, initialSceneHeight);
this.stage.setScene(scene);
this.stage.show();
this.decorationWidth = initialSceneWidth - scene.getWidth();
this.decorationHeight = initialSceneHeight - scene.getHeight();
}
public void resizeScene(double width, double height) {
this.stage.setWidth(width + this.decorationWidth);
this.stage.setHeight(height + this.decorationHeight);
}
I just wanted to post another answer for those who might have had a similar problem as mine.
http://docs.oracle.com/javase/8/javafx/api/javafx/scene/Scene.html
There is no setWidth() or setHeight(), and the property is ReadOnly, but if you look at
Constructors
Scene(Parent root)
Creates a Scene for a specific root Node.
Scene(Parent root, double width, double height)
Creates a Scene for a specific root Node with a specific size.
Scene(Parent root, double width, double height, boolean depthBuffer)
Constructs a scene consisting of a root, with a dimension of width and height, and specifies whether a depth buffer is created for this scene.
Scene(Parent root, double width, double height, boolean depthBuffer, SceneAntialiasing antiAliasing)
Constructs a scene consisting of a root, with a dimension of width and height, specifies whether a depth buffer is created for this scene and specifies whether scene anti-aliasing is requested.
Scene(Parent root, double width, double height, Paint fill)
Creates a Scene for a specific root Node with a specific size and fill.
Scene(Parent root, Paint fill)
Creates a Scene for a specific root Node with a fill.
As you can see, this is where you can set the height and width if you need to.
For me, I am using SceneBuilder, just as you described you were doing, and needed the width and height of that. I am creating custom controls, so it was weird that it didn't do it automatically, so this is how to do it if you need to.
I could have used setWidth() / setHeight() from the Stage as well.
I had an similar challenge when I wanted to switch between different size scenes in JavaFX.
I did my scene switching by changing the Parent resource of a Scene. The new resource would have larger dimensions than the previous resource so the entire Scene wasn't visible.
Like user2229691's answer stated - my issue turned out to be the Window not matching the Scene's dimensions.
Although I'm not able to comment about decorations - I was able to solve my issue by using:
sceneName.getWindow().sizeToScene();
It works when downsizing and upsizing from my own testing.
Here's some example code so you can better understand what I did:
public void start(Stage stage) throws IOException {
// Height of 300, width of 300
Parent scene1 = FXMLLoader.load(getClass().getResource("scene1.fxml"));
// Height of 400, width of 400
Parent scene2 = FXMLLoader.load(getClass().getResource("scene2.fxml"));
// Starting off in the smaller scene
Scene mainScene = new Scene(scene1); // Scene & Window are 300x300
stage.setScene(mainScene);
stage.show();
// Switching to the larger scene resource, that doesn't fit in the window
mainScene.setRoot(scene2); // Scene is 400x400 but can only see 300x300
// Resizing the window so the larger scene fits
mainScene.getWindow().sizeToScene(); // Window is now 400x400 & everything is visible
}
I hope this helps someone.
Source:
https://docs.oracle.com/javase/8/javafx/api/javafx/stage/Window.html#sizeToScene--

Resources