How to lock the divider in SplitPane JavaFX? - javafx

I have a SplitPane and I need to divide the layout 25% and 75%. Also, I need to disallow dragging towards right side beyond the 25% split. However I can drag to any extent within the 25% space. Please help.

SplitPane will respect the min and max dimensions of the components (items) it contains. So to get the behavior you want, bind the maxWidth of the left component to splitPane.maxWidthProperty().multiply(0.25):
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ConstrainedSplitPane extends Application {
#Override
public void start(Stage primaryStage) {
StackPane leftPane = new StackPane(new Label("Left"));
StackPane rightPane = new StackPane(new Label("Right"));
SplitPane splitPane = new SplitPane();
splitPane.getItems().addAll(leftPane, rightPane);
splitPane.setDividerPositions(0.25);
//Constrain max size of left component:
leftPane.maxWidthProperty().bind(splitPane.widthProperty().multiply(0.25));
primaryStage.setScene(new Scene(new BorderPane(splitPane), 800, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

This worked for me
private final double dividerMaxWidth = .15;
splitPane.getDividers().get(0).positionProperty().addListener((observable,oldValue,newValue) -> {
if(splitPane.getDividers().get(0).getPosition() > dividerMaxWidth)
splitPane.setDividerPosition(0, dividerMaxWidth);
});
And if you want to lock a divider into a hard position that cannot be moved
private final double absolutePosition = .15;
splitPane.getDividers().get(0).positionProperty().addListener((observable,oldValue,newValue) -> {
splitPane.setDividerPosition(0, absolutePosition);
});

Related

Align a Hbox to the bottom center inside a StakePanel

I'm learning Javafx layouts and I would like to obtain the following result:
HBox in the bottom center fo a Pane I have tried with the following code but I'm not able to do that
Can you please explain what I'm doing wrong? and How to achieve that?
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class TestLayout extends Application {
#Override
public void start(Stage stage) {
initUI(stage);
}
private void initUI(Stage stage) {
Scene scene =null;
HBox hbox = new HBox();
hbox.setStyle("-fx-background-color: #FFFAAA;");
StackPane pane = new StackPane();
hbox.setStyle("-fx-background-color: #AAFAAA;");
hbox.prefWidthProperty().bind(pane.widthProperty().divide(4));
hbox.prefHeightProperty().bind(pane.heightProperty().divide(10));
pane.getChildren().add(hbox);
scene = new Scene(pane, 600, 600);
pane.prefWidthProperty().bind(scene.widthProperty());
pane.prefHeightProperty().bind(scene.heightProperty());
stage.setTitle("Test");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class AppLauncherTest {
public static void main(String[] args) {
TestLayout.main(args);
}
}
I would Use a VBox or BorderPane as the root node. In this example, I use a VBox. I am assuming more nodes will go into this so I used a StackPane as the top node. This may need to be changed out or some other Pane may need to be added to this. That depends on what you are trying to do with the end product. For the bottom, I used an HBox and I set it's Margins.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.Priority;
import javafx.geometry.Insets;
/**
* JavaFX App
*/
public class App extends Application {
#Override
public void start(Stage stage) {
StackPane subRootTop = new StackPane();
VBox.setVgrow(subRootTop, Priority.ALWAYS);
//subRootTop.setStyle("-fx-background-color: yellow;");
HBox subRootBotton = new HBox();
VBox.setVgrow(subRootBotton, Priority.ALWAYS);
subRootBotton.setStyle("-fx-background-color: green;");
subRootBotton.setMaxHeight(150);
VBox.setMargin(subRootBotton, new Insets(40, 40, 40, 40));
VBox root = new VBox(subRootTop, subRootBotton);
root.setStyle("-fx-background-color: red;");
Scene scene = new Scene(root, 600, 600);;
stage.setTitle("Test");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
The binding of the StackPane is not size shouldn't be done. The scene automatically resizes its root to fill the whole area available. Assuming you want relative sizes, using StackPane is not really a good choice.
If you want the child to have a fixed distance to left right and bottom of the StackPane, you can do so specifying margins and alignment. Make sure the child does not grow to fit the parent though by setting the maxHeight to use the preferred height:
private void initUI(Stage stage) {
HBox hbox = new HBox();
hbox.setStyle("-fx-background-color: #FFFAAA;");
hbox.setMaxHeight(Region.USE_PREF_SIZE);
// could be calculated based on children instead of assigning
// an absolute value
hbox.setPrefHeight(30);
StackPane.setAlignment(hbox, Pos.BOTTOM_CENTER);
StackPane.setMargin(hbox, new Insets(20));
StackPane pane = new StackPane(hbox);
pane.setStyle("-fx-background-color: #AAFAAA;");
Scene scene = new Scene(pane, 600, 600);
stage.setTitle("Test");
stage.setScene(scene);
stage.show();
}
Maybe you can use an AnchorPane as a base layout instead of the StacKPane. I made a small exmaple how it would look like. Of cause you could keep the StackPane as the base layout and just put the AnchorPane with the HBox onto it, but you should definitly use an AnchorPane for your plan.
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
private void initUI(Stage stage) {
// Create an anchor pane as base layout and set a color:
AnchorPane root = new AnchorPane();
root.setStyle("-fx-background-color: #FFFAAA;");
// Create a second container and set a minimum height and a color;
HBox hbox = new HBox();
hbox.setMinHeight(100d);
hbox.setStyle("-fx-background-color: #AAFAAA;");
// Give the child container a fixed location:
AnchorPane.setBottomAnchor(hbox, 50d);
AnchorPane.setLeftAnchor(hbox, 75d);
AnchorPane.setRightAnchor(hbox, 75d);
// Add the horizontal box to the base anchor pane:
root.getChildren().add(hbox);
stage.setScene(new Scene(root, 600, 600));
stage.show();
}
#Override
public void start(Stage stage) throws Exception {
initUI(stage);
}
public static void main(String[] args) {
launch(args);
}
}
Preview:

How to make a suttering window in JavaFx?

I need a window that can be move up down in my main window. Mainly IDE(IntelliJ, CodeBlock, Netbeans etc) output window feature.
I use TitledPane. By using this I can give a height that is expanded when I click the pane, but I can't expand the pane in any height.
see this - not expanded:
and this- expanded:
My code is here
Try using SplitPane. I guess this is what you want. You can learn more from the following link
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/SplitPane.html
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
SplitPane splitPane = new SplitPane(new Pane(), new Pane());
splitPane.setDividerPositions(0.5);
splitPane.setOrientation(Orientation.VERTICAL);
primaryStage.setScene(new Scene(splitPane,400,400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

JavaFX moving Objects within ScrollPane by drag and drop

I have a very large StackPane(3000x2000) inside of a ScrollPane. The idea is to use it like a large "drawing board", in which users can create nodes and drag them around, for creating Mindmaps and such. The problem is drag and drop: It works fine if the scrollPane is in its "start position", so that both Hvalue and Vvalue are 0. But once you scrolled a bit, the values that DragEvent.getX() and .getY() return are relative to the visible part of the pane, not to its entire size. That means you cannot drag and drop something properly. I created a test class to illustrate the problem without any obsolete code:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class DragTestClass extends Application{
public static void problem(Circle circle, ScrollPane scrollPane, StackPane pane, DataFormat dataFormat){
circle.setOnDragDetected(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
Dragboard db = circle.startDragAndDrop(TransferMode.ANY);
ClipboardContent content = new ClipboardContent();
content.put(dataFormat,0); // normally, ID of node
db.setContent(content);
event.consume();
}
});
scrollPane.setOnDragDropped(new EventHandler<DragEvent>() {
#Override
public void handle(DragEvent event) {
Dragboard db = event.getDragboard();
if(db.hasContent(dataFormat) && db.getContent(dataFormat) instanceof Integer){
int index = (Integer) db.getContent(dataFormat);
Circle node = (Circle) pane.getChildren().get(index);
node.setManaged(false);
// this is the problematic part
node.setTranslateX(event.getX() - node.getCenterX());
node.setTranslateY(event.getY() - node.getCenterY());
event.setDropCompleted(true);
event.consume();
}
}
});
scrollPane.setOnDragOver(new EventHandler<DragEvent>() {
public void handle(DragEvent event) {
event.acceptTransferModes(TransferMode.ANY);
event.consume();
}
});
}
#Override
public void start(Stage primaryStage) throws Exception {
ScrollPane scrollPane = new ScrollPane();
StackPane pane = new StackPane();
DataFormat dataFormat = new DataFormat("DragDropFormat");
int width = 1000;
int height = 1000;
pane.setMinHeight(height);
pane.setMaxHeight(height);
pane.setMinWidth(width);
pane.setMaxWidth(width);
Circle circle = new Circle(50,50,20); // normally a StackPane
circle.setManaged(false);
pane.getChildren().add(circle);
pane.setAlignment(Pos.TOP_LEFT);
scrollPane.setContent(pane);
problem(circle, scrollPane, pane, dataFormat);
Scene scene = new Scene(scrollPane, 400, 400);
primaryStage.setTitle("Problem");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
My idea was to add something to the translation of the nodes, like scrollPane.getHvalue() * width, but that doesn't seem to be working. Maybe I just googled the wrong keywords, but I haven't found anything helpful, so I'm sorry if the question was answered elsewhere. Thanks in advance for your help!
I figured it out! Setting the EventHandler on the StackPane (the content of the ScrollPane, instead of the ScrollPane itself) did the trick. Apparently, the coordinates that event.getX() returns are relative to the node that has the EventHandler. In this example:
pane.setOnDragDropped...

How to get Treeview DisclosureNode to right , in Javafx?

In Javafx the DisclosureNode of Treeview is on left by default. How can I I place the Disclosure Node to Right (float right)..??
Any Help would be appreciated.
You only need to set the Node Orientation to get a right to left behaviour. But be aware of, that all childrens of this TreeView will inherit the orientation as a default.
import javafx.application.Application;
import javafx.geometry.NodeOrientation;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class TreeViewRightLeft extends Application {
#Override
public void start(Stage primaryStage) {
TreeItem<String> item = new TreeItem<>("Root Node");
item.setExpanded(true);
item.getChildren().addAll(
new TreeItem<>("Item 1"),
new TreeItem<>("Item 2"),
new TreeItem<>("Item 3")
);
TreeView<String> treeView = new TreeView<>(item);
// Here you can select the orientation.
treeView.setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);
BorderPane root = new BorderPane(treeView);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("TreeView");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Right to Left
Left to Right (default)

Auto resize ScrollPane

I want to resize ScrollPane to fit the parent component. I tested this code:
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class MainApp extends Application {
#Override
public void start(Stage stage) throws Exception {
BorderPane bp = new BorderPane();
bp.setPrefSize(600, 600);
bp.setMaxSize(600, 600);
bp.setStyle("-fx-background-color: #2f4f4f;");
VBox vb = new VBox(bp);
ScrollPane scrollPane = new ScrollPane(vb);
scrollPane.setFitToHeight(true);
scrollPane.setFitToWidth(true);
scrollPane.setHbarPolicy(ScrollBarPolicy.AS_NEEDED);
scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
Scene scene = new Scene(scrollPane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
But as you can see I don't see tree scroll bars. Is there anything wrong into my code?
The scrollbars won't show up because
You have the policy set to ScrollBarPolicy.AS_NEEDED
The width and height of the scroll bars are automatically resized to that of its' container, which, in this case, is your resizable Vbox.
To fix this, you could remove setFitToHeight and setFitToWidth and leave them as false.
Note that ScrollBarPolicy could also be set to ALWAYS as opposed to AS_NEEDED, which will keep the scroll bars even when the window is expanded.
Refer here for more information using ScrollPane
ScrollPane API: setFitToHeight
public class MainApp extends Application {
#Override
public void start(Stage stage) throws Exception {
BorderPane bp = new BorderPane();
bp.setPrefSize(600, 600);
bp.setMaxSize(600, 600);
bp.setStyle("-fx-background-color: #2f4f4f;");
VBox vb = new VBox(bp);
ScrollPane scrollPane = new ScrollPane(vb);
//scrollPane.setFitToHeight(true);
//scrollPane.setFitToWidth(true);
scrollPane.setHbarPolicy(ScrollBarPolicy.ALWAYS);
scrollPane.setVbarPolicy(ScrollBarPolicy.ALWAYS);
Scene scene = new Scene(scrollPane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Resources