I'm working on a javafx project that has a VBox inside a ScrollPane. The problem is that when the height of the vBox decreases, the scrollPane scrolls Up Automatically. How can i prevent that ? Knowing that it does not scrolls when the height increases!
Note: inside the vBox i have buttons which add/remove some TextFields which result in the increasing/decreasing of the VBox height.
I cannot show the whole code due to work regulations but here is where i define and add the VBox inside ScrollPane:
VBox mainVBox = new VBox();
mainVBox.setStyle("-fx-background-color: #0292b7");
mainVBox.setPrefWidth(400);
mainVBox.setSpacing(10);
mainVBox.setPadding(new Insets(3,5,3,5));
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(mainVBox);
scrollPane.setPrefWidth(420);
scrollPane.setPrefHeight(600);
VBox.setVgrow(scrollPane,Priority.ALWAYS);
The reason of this behavior is that you remove the element which currently has the focus. Which lead to a focus-transfer to the first element in the container and the ScrollPane automatically scrolls to this element.
To change this you can add for example mainVBox.requestFocus(); in you code before you remove the Button or/and TextField.
Related
I can't get my scroll pane to properly show the information that I want to with the proper scroll bar. I'm putting an HBox which contains two VBoxes into a ScrollPane and inserting that ScrollPane as the center element of a BorderLayout. It seems like the ScrollPane ignores the bounds of the window and becomes so large that using a scroll bar is unnecessary and I can't view the bottom content. I've tried wrapping it in an AnchorPane and setting the bounds of the AnchorPane which didn't work, I've tried setting the size manually which kind of worked but led to some odd glitches where content would show up all over the screen, I've tried wrapping it in a Pane which I then added to the center, I've tried wrapping the HBox in a VBox before adding it to the ScrollPane and then wrapping the ScrollPane in a Pane, and I've tried various uses of the setFitToHeight/Width functions. I just want it to pay attention to the bounds of the window and conform accordingly
edit: some example code
VBox vbox1 = getvbox1Content();//this is long thus requires a scrollpane
VBox vbox2 = getvbox2Content();
HBox hbox = new HBox();
hbox.getChildren().addAll(vbox1, vbox2);
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(hbox);
borderPane.setCenter(scrollPane);
This is a gif of my problem:
As you can see, the button resizes (elevates? and it moves it's outer layout with itself.
This is the hierarchy:
StackPane
Group // for other content
Group // The make hbox "wrap-content"
HBox // For the 2 buttons
JfxToggleButton // "This is test"
JfxButton // "Restart" button
The only code to add is that I am positioning the HBox's Group top right of the StackPane, like this:
StackPane.setAlignment(hboxGroup, Pos.TOP_RIGHT)
Only StackPane has this issue, BorderPane does not have it.
The reason I am using StackPane is because I want to have the controls on top of the main content.
ToggleButton and HBox have padding.
Actually, padding and alignment do not matter, since the HBox moves relatively regardless.
Btw, I am using jfonix library for button styling.
Thanks for your help.
Edit.:
For those who contribute this problem to JFoenix, this is the same with standard JavaFX controls: (the jump still occurs, only less noticable)
Compared to what I mentioned above, where theHboxhas it's place in the top of a BorderPane, the jump does not happen:
I realized AnchorPane does not have this problem.
So this the final layout hierarchy:
StackPane
Group // Content behind
AnchorPane // That restricts movement, keeps the buttons in place
Group // That keeps the background only around the buttons
HBox // Keeps the buttons next to each other
ToggleButton
Button
So I ended up with the problem that AnchorPane does not let clicks through. Which is fixed by setting the AnchorPane
anchorPane.setPickOnBounds(false);
I would like to add a Label - i.e. some text inside the border of a JavaFX HBox. The text should sit in the top left corner of the HBox and be inside the visible border area. I used to be able to do this in Swing, but I haven't figured out how to do it in JavaFX.
Thanks
I have a VBox that enters an edit mode if the user chooses to. How can I prevent the mouse from the leaving that VBox and clicking on other things?
Alternatively, how do I BLOCK OUT the rest of the application and make sure they aren't clickable? (Making them a lighter tint would show this too. Like how Adobe Illustrator enters a shape's edit mode).
Add a separate Pane to your layout and set its visibility to false and its style to something like -fx-background-color: #00000077. Add your VBox on top of that Pane (but not as a child of the Pane). Your layout should be something like this:
layout
-> controls
-> pane
-> vbox
Where layout is your main layout node, controls is your node containing most of your interface, pane is the dimming Pane, and vbox is the VBox you want to be emphasized. The children should be added in that order.
Whenever a node in vbox has focus, show pane (and hide it when focus is lost or your accept the user's input). pane, when visible, will prevent the user from interacting with other parts of your interface, specifically any node within layout (except vbox).
I have a HBox in my Javafx application with below configuration
HBox belowBox = new HBox(10);
belowBox.getStyleClass().addAll("pane", "vbox");
belowBox.setAlignment(Pos.CENTER);
belowBox.getChildren().addAll( .... );
belowBox.setMaxHeight(200);
belowBox.setPrefHeight(200);
belowBox.setFillHeight(false);
But still if I resize my application the HBox grows vertically. Is there a way to fix the height.
Thank you
But this is the exact behavior of the HBox layout pane. If you read the HBox documentation, you will see the following:
HBox lays out its children in a single horizontal row...
And also:
HBox will resize children (if resizable) to their preferred widths and uses its fillHeight property to determine whether to resize their heights to fill its own height or keep their heights to their preferred (fillHeight defaults to true)...
As said, if for some reason you set the fillHeight property from HBox, you can make the nodes contained within to fill the vacant vertical space, But this will only occur for the nodes that are resizable. For more information about using the JavaFX layout panels, look here. If I'm not mistaken, texts, geometric shapes and ImageView are not resizable objects.