BorderPane root = new BorderPane();
Button chooseFile = new Button("chooseFile");
TextField fileLocation = new TextField("C:/");
Button makeGrid = new Button("Solve");
HBox fileLoad = new HBox(chooseFile, fileLocation, makeGrid);
root.setTop(lastfil);
BorderPane.setAlignment(root, Pos.TOP_CENTER);
BorderPane.setMargin(root, new Insets(12,12,12,12));
root.setPrefSize(500, 500);
I'm having some issues centering this, I want the prefSize to be 500 x 500 and have the file loader top center, but it does not do that. It is top but I am unable to get it to the center of the top. Anything obvious I'm doing wrong?
BorderPane sets the alignment and Margin for the child node.
BorderPane.setAlignment(Node child, Pos value)
BorderPane.setMargin(Node child, Insets value)
So replace Borderpane 'root' to child node 'fileLoad ' as:
BorderPane.setAlignment(fileLoad, Pos.TOP_CENTER);
BorderPane.setMargin(fileLoad, new Insets(12,12,12,12));
Also set the child HBox alignment like:
fileLoad.setAlignment(Pos.CENTER);
Refer Class BorderPane to more info.
Related
I want to have StackPane as my root pane. I want to have a scrollPane as a small box in the middle of the scene and two buttons below the container.
I tried to make it happen by writing this code:
private StackPane root = new StackPane();
private Scene scene = new Scene(root, 1366, 768);
public ContinueScreen() {
Button button1 = new ButtonBuilder("My Button1").setPrefWidth(200).build();
Button button2 = new ButtonBuilder("My Button2").setPrefWidth(200).build();
Button button3 = new ButtonBuilder("My Button3").setPrefWidth(200).build();
Button button4 = new ButtonBuilder("My Button4").setPrefWidth(200).build();
Button button5 = new ButtonBuilder("My Button5").setPrefWidth(200).build();
Button button6 = new ButtonBuilder("My Button6").setPrefWidth(200).build();
VBox vBox = new VBox(5);
vBox.getChildren().addAll(button1, button2, button3, button4, button5, button6);
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(vBox);
scrollPane.setPannable(true);
scrollPane.setMaxSize(500, 180);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
root.getChildren().add(scrollPane);
StackPane.setAlignment(scrollPane, Pos.CENTER);
}
As you can probably notice, the code does work perfectly fine but that is until I add the buttons below I was talking about. I added those buttons in an HBox as
HBox hBox = new HBox(5);
hBox.setAlignment(Pos.BOTTOM_CENTER);
hBox.getChildren().addAll(new Button("cat"), new Button("dog"));
root.getChildren().addAll(hBox);
Now both the scrollpane and the two buttons are shown. However the scroll pane now stops working for some reason. The scrollpane is shown and its content but the horizontal or the vertical scroll, both of them do not work. Anyone knows why is this happening and how to fix it?
As you are using StackPane as root it piles up the nodes on one another so the top pane is HBox and not the ScrollPane, so it you are not able to use.
Use BorderPane or VBox and try.
BorderPane root = new BorderPane();
Scene scene = new Scene(root, 1366, 768);
root.setCenter(scrollPane);
HBox hBox = new HBox(5);
hBox.getChildren().addAll(new Button("cat"), new Button("dog"));
root.setBottom(hBox);
The issue here is that StackPane is allowed to resize the HBox, which it does. The HBox covers the complete scene preventing mouse events from reaching the ScrollPane. You can easily see this by coloring the HBox:
hBox.setStyle("-fx-background-color: rgba(100%, 0%, 0%, 0.5)");
The most simple solution would be to set up the HBox to only receive events on non-(fully-)transparent areas:
hBox.setPickOnBounds(false);
However you could also set up the HBox to take up the space required to fit the preferred size of the content and do the alignment via the StackPane:
hBox.setPrefSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
hBox.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
StackPane.setAlignment(hBox, Pos.BOTTOM_CENTER);
// hBox.setAlignment(Pos.BOTTOM_CENTER);
Note that using a StackPane like this does not provide you with a responsive GUI: If you resize the window to a small enough height, the buttons will cover the ScrollPane.
You may have better luck using a BorderPane or wrapping the StackPane and the HBox in a VBox and setting VBox.vgrow to Priority.ALWAYS for the StackPane...
So: i have here a piece of code that I am using to add a menu to my application.
Stage window;
private void buildWindow() {
window = new Stage();
window.setTitle("FACE");
//MENU
MenuBar mBLaunch = new MenuBar();
Menu fileLaunch = new Menu("File");
MenuItem saveLaunch = new MenuItem("Save");
MenuItem exitLaunch = new MenuItem("Exit");
fileLaunch.getItems().addAll(saveLaunch, new SeparatorMenuItem(), exitLaunch);
mBLaunch.getMenus().add(fileLaunch);
//HouseKeeping
BorderPane bPLaunch = new BorderPane();
bPLaunch.getChildren().addAll(mBLaunch);
Scene launch = new Scene(bPLaunch);
window.setScene(launch);
window.setMinHeight(500);
window.setMinWidth(500);
window.show();
}
However: when I run this code it produces:
So, my question being, how can I make it display something else than ...?
Thanks for the help in advance.
BorderPane places each child at a specific location: center, top, bottom, left, right. If you just add nodes to its list of children, it doesn’t know where to place them.
Replace this:
bPLaunch.getChildren().addAll(mBLaunch);
with this:
bPLaunch.setTop(mBLaunch);
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?
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
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);
I am trying to set up a UI with three split panes. The first two are vertical panes, on the left and right side of the screen. One side of each split has a title pane. The user can select items from these panes to include in fields in the central pane. There is also a horizontal pane at the bottom that is not relevant to this question.
The user can open these side panes either by dragging the vertical dividers, or by clicking on the relevant toggle button (Films, Books etc.) to show that pane.
The issue I have is that I want to make it so that dragging one vertical divider does not move the other. However, since I cannot find a way to set this up without putting one of the vertical split panes into the other vertical pane, this always results in a situation where moving one of the dividers also moves the other. In the case of the below code for instance, moving the vertical divider for the left-hand (Films) split pane moves the right-hand vertical divider.
Can anyone help with this?
package pane2;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.application.*;
import javafx.beans.property.DoubleProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TitledPane;
import javafx.scene.input.*;
import javafx.stage.Stage;
public class Pane2 extends Application {
SplitPane rightSplit;
DoubleProperty rightSplitDividerPos;
TitledPane books;
ToggleButton selectBooks;
VBox booksBox;
VBox centre;
SplitPane leftSplit;
DoubleProperty leftSplitDividerPos;
TitledPane films;
ToggleButton selectFilms;
VBox filmsBox;
VBox centreLeft;
SplitPane mainSplit;
DoubleProperty mainSplitDividerPos;
TitledPane arts;
ToggleButton selectArts;
VBox artsBox;
BorderPane root;
public static void main(String[] args)
{
launch( args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Test");
//Create right-hand titled pane for the books list and centre it in Vbox
books = new TitledPane();
books.setText("Books");
books.setMinWidth(0);
booksBox = new VBox(0,books);
//Create central pane and add toggle buttons to open hidden panes on the
//left, right, and bottom (films, books, and arts respectively)
selectBooks = new ToggleButton("Books");
selectFilms = new ToggleButton("Films");
selectArts = new ToggleButton("Arts");
centre = new VBox(100,selectBooks,selectFilms,selectArts);
centre.setPrefWidth(1300);
centre.setPrefHeight(750);
//Create split pane to divide the central pane and books list
rightSplit = new SplitPane();
rightSplit.getItems().addAll(centre,booksBox);
//Create left-hand titled pane for the films list and centre it in VBox
films = new TitledPane();
films.setText("Films");
films.setMinWidth(0);
filmsBox = new VBox(0,films);
//Create split pane to divide the films list and the central pane
leftSplit = new SplitPane();
leftSplit.getItems().addAll(filmsBox,rightSplit);
//Create mainSplit pane
arts = new TitledPane();
arts.setText("arts");
arts.setMinHeight(0);
artsBox = new VBox(0,arts);
mainSplit = new SplitPane();
mainSplit.setOrientation(Orientation.VERTICAL);
mainSplit.getItems().addAll(leftSplit,artsBox);
root = new BorderPane();
root.setCenter(mainSplit);
//Set divider positions for the three dividers
rightSplitDividerPos = rightSplit.getDividers().get(0).positionProperty();
rightSplitDividerPos.set(1.0);
leftSplitDividerPos = leftSplit.getDividers().get(0).positionProperty();
leftSplitDividerPos.set(0.0);
mainSplitDividerPos = mainSplit.getDividers().get(0).positionProperty();
mainSplitDividerPos.set(1.0);
//Start up scene and stage
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setMaximized(true);
primaryStage.show();
//Event - if the books toggle button is selected, the left divider will
//move to the right to show the books selection pane
selectBooks.setOnAction(event -> {
if(selectBooks.isSelected()){
leftSplitDividerPos.set(0.15);
}
if(!selectBooks.isSelected()){
leftSplitDividerPos.set(0.0);
}else{
}
});
//Event - if the films toggle button is selected, the right divider will
//move to the left to show the films selection pane
selectFilms.setOnAction(event -> {
if(selectFilms.isSelected()){
rightSplitDividerPos.set(0.8);
}
if(!selectFilms.isSelected()){
rightSplitDividerPos.set(1.0);
}else{
}
});
//Event - if the arts toggle button is selected, the bottom divider will
//move up to show the arts selection pane
selectArts.setOnAction(event -> {
if(selectArts.isSelected()){
mainSplitDividerPos.set(0.75);
}
if(!selectArts.isSelected()){
mainSplitDividerPos.set(1.0);
}else{
}
});
}
}
do you really need 3 SplitPane in your layout? because i think you can achieve pretty much the same result with just 1 pane:
SplitPane split = new SplitPane();
VBox left = new VBox(new Label("left"));
left.setStyle("-fx-background-color: cadetblue");
VBox right = new VBox(new Label("right"));
right.setStyle("-fx-background-color: darkorange");
VBox center = new VBox(new Label("center"));
center.setStyle("-fx-background-color: darkgreen");
split.getItems().addAll(left, center, right);
split.setDividerPosition(0,1/(double)3);
split.setDividerPosition(1,2/(double)3);
Scene scene = new Scene(split, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
Here is your code realated Example:
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Test");
//Create central pane and add toggle buttons to open hidden panes on the
//left, right, and bottom (films, books, and arts respectively)
ToggleButton selectBooks = new ToggleButton("Books");
ToggleButton selectFilms = new ToggleButton("Films");
ToggleButton selectArts = new ToggleButton("Arts");
VBox centre = new VBox(100,selectBooks,selectFilms,selectArts);
//Create left-hand titled pane for the films list and centre it in VBox
TitledPane films = new TitledPane();
films.setText("Films");
VBox filmsBox = new VBox(films);
//Create right-hand titled pane for the books list and centre it in Vbox
TitledPane books = new TitledPane();
books.setText("Books");
VBox booksBox = new VBox(books);
//Create mainSplit pane
TitledPane arts = new TitledPane();
arts.setText("arts");
VBox artsBox = new VBox(arts);
SplitPane mainSplit = new SplitPane();
mainSplit.getItems().addAll(filmsBox, centre, booksBox);
mainSplit.setDividerPosition(0,1/(double)12);
mainSplit.setDividerPosition(1,11/(double)12);
SplitPane root = new SplitPane();
root.setOrientation(Orientation.VERTICAL);
root.getItems().addAll(mainSplit, artsBox);
root.setDividerPosition(0,0.9);
root.setPrefWidth(1300);
root.setPrefHeight(750);
//Start up scene and stage
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setMaximized(true);
primaryStage.show();
}
I have TreeView placed as content of ScrollPane. ScrollPane is placed inside SplitPane.
When I drag divider of SplitPane so that it becomes bigger than TreeView size I see border of TreeView.
I want TreeView to be resized as much as space is available after I drag divider of SplitPane. My code:
SplitPane splitPane = new SplitPane();
splitPane.getItems().addAll(createTreeOfConnections());
private ScrollPane createTreeOfConnections() {
ScrollPane scrollPane = new ScrollPane();
scrollPane.setMinSize(100, 300);
scrollPane.setPrefSize(200, 500);
scrollPane.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
scrollPane.setHbarPolicy(ScrollBarPolicy.AS_NEEDED);
scrollPane.setContent(new ConnectionsTree(this));
return scrollPane;
Class ConnectionsTree extends TreeView and its constructor is :
public ConnectionsTree(MainStage mainStage) {
// here we set the root of ConnectionsTree which is not visible
super();
this.mainStage = mainStage;
root = new ConnectionTreeItem();
root.setExpanded(true);
super.setRoot(root);
super.setShowRoot(false);
super.setEditable(false);
super.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
super.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
retrieveExistentConnectionsNodes();
ContextMenu contextMenu = new ContextMenu(createNewConnectionMenuItem(
"New Connection", KeyCombination.valueOf("Ctrl+N")));
super.setContextMenu(contextMenu); }
How to tell TreeView(my TreeConnections) that it should take all available space of left part of SplitPane?
Thank you!
Try this
scrollPane.setFitToHeight(true);
scrollPane.setFitToWidth(true);