Set BorderPane components size - javafx-2

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);

Related

JAVAFX - Adding Nodes To TitledPane's Header

I want to add nodes into the TitledPane's header. The only way I found to do it is using setGraphic() method.
The problem of using that way is I cannot place them neatly. Here is what I get:
The Codes
TitledPane tpane = new TitledPane();
tpane.setContent(new Text("Content"));
tpane.setExpanded(false);
Button b = new Button("Delete");
BorderPane box = new BorderPane();
box.setLeft(new Text("1 "));
box.setCenter(new Text("Single Pane"));
box.setRight(b);
AnchorPane par = new AnchorPane(box);
tpane.setGraphic(par);
VBox vbox = new VBox();
vbox.getChildren().add(tpane);
The Goal
I want to put the Delete button to the right, that's why I am using Borderpane. So the BorderPane need to fill the entire of the TitledPane's header width minus the space for small triangle
How can it be done inside setGraphic() method? Or are there any better way to achieve it?
To fix your problem try setting the width of the BorderPane to the width of the scene minus the arrow like this.
TitledPane tpane = new TitledPane();
tpane.setContent(new Text("Content"));
tpane.setExpanded(false);
Button b = new Button("Delete");
BorderPane box = new BorderPane();
box.setLeft(new Text("1 "));
box.setCenter(new Text("Single Pane"));
box.setRight(b);
AnchorPane par = new AnchorPane(box);
tpane.setGraphic(par);
VBox vbox = new VBox();
vbox.getChildren().add(tpane);
box.setPrefWidth(scene.getWidth()-30);//You can adjust the number to fit your needs and change scene to whatever the name of your scene is
I hope this helped
Well, it seems that support for javafx users is not as massive as the mainstream framework.
After some tries by the suggestion from #Austin, I found the way to do what I want, and this is also give dynamic sizing.
box.prefWidthProperty().bind(scene.widthProperty().subtract(35));
TitledPane tpane = new TitledPane();
tpane.setContent(new Text("Content"));
tpane.setExpanded(false);
Button b = new Button("Delete");
BorderPane box = new BorderPane();
box.setLeft(new Text("1 "));
box.setCenter(new Text("Single Pane"));
box.setRight(b);
tpane.setGraphic(box);
box.prefWidthProperty().bind(scene.widthProperty().subtract(35));
VBox vbox = new VBox();
vbox.getChildren().add(tpane);

BorderPane spacing between nodes

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));

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

How to set tabs position in JavaFX

I have this JavaFX code with tabs. Can you tell me how I can set the position of the tabs panel to be always on the left side of the main stage:
VBox stackedTitledPanes = createStackedTitledPanes();
ScrollPane scroll = makeScrollable(stackedTitledPanes);
TabPane tabPane = new TabPane();
BorderPane mainPane = new BorderPane();
tabPane.setStyle("-fx-font-size: 12pt;"); // Set global size for the font
// Create Tabs
Tab tabA = new Tab();
tabA.setText("Main Component");
tabA.setStyle("-fx-font-size: 12pt;"); // Set size of the tab name
// Add something in Tab
StackPane tabA_stack = new StackPane();
tabA_stack.setAlignment(Pos.CENTER);
tabA_stack.getChildren().add(scroll);
tabA.setContent(tabA_stack);
tabPane.getTabs().add(tabA);
Tab tabB = new Tab();
tabB.setText("Second Component");
tabB.setStyle("-fx-font-size: 12pt;"); // Set size of the tab name
// Add something in Tab
StackPane tabB_stack = new StackPane();
tabB_stack.setAlignment(Pos.CENTER);
tabB_stack.getChildren().add(new Label("Label#Tab B"));
tabB.setContent(tabB_stack);
tabPane.getTabs().add(tabB);
Tab tabC = new Tab();
tabC.setText("Last Component");
tabC.setStyle("-fx-font-size: 12pt;"); // Set size of the tab name
// Add something in Tab
StackPane tabC_vBox = new StackPane();
tabC_vBox.setAlignment(Pos.CENTER);
tabC_vBox.getChildren().add(new Label("Label#Tab C"));
tabC.setContent(tabC_vBox);
tabPane.getTabs().add(tabC);
mainPane.setCenter(tabPane);
mainPane.setPrefSize(395, 580);
mainPane.setLayoutX(850);
mainPane.setLayoutY(32);
scroll.setPrefSize(395, 580);
scroll.setLayoutX(850);
scroll.setLayoutY(32);
root.getChildren().add(mainPane);
Update
Sounds like you want to change your stackedTitledPane from a VBox to a BorderPane.
So this code:
VBox stackedTitledPanes = new VBox();
becomes:
BorderPane stackedTitledPanes = new BorderPane();
Then when you want to add your nodes, you specify which section you want to put them (I put a label in the center pane as a filler):
stackedTitledPanes.setLeft(mainPane);
stackedTitledPanes.setCenter(new Label("Main Content"));
Old answer
This will put the tabs on the left (I'm assuming this is what you're looking for):
tabPane.setSide(Side.LEFT);

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();
}

Resources