Sizing ScrollPane inside a BorderLayout properly in javafx - java

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

Related

How to prevent autoscroll in SrollPane in javafx

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.

Labels with icons don't fit scrollpane

I'm adding labels with icons to a VBox inside a scrollPane.
My problem is using this method the contents doesn't fit scroll pane even if i fix height ( i have the impression that it has been overrided)
Sphere sphere = new Sphere(10);
Label l = new Label(s,sphere);
vbox.getChildren().add(l);
scrollpane.setContent(vbox);
Using labes without icons fit correctly to the scrollpane ..
How can i fix this problem?
Edit:
Thanks

Add a Label inside the border of an HBox or a VBox

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

Javafx HBox restrict to specific height

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.

Scrollable flow panel

I need to create a panel where I can put some rectangles and it automatically reorder just inserting a scrollbar and growing up vertically. Also this panel can be resizable and again the rectangles must to be reordered to correctly be displayed inside the panel.
If I understand the question you want components to wrap to the next line so that the panel grows vertically while the width remains fixed.
If so then check out the WrapLayout
Note: the FlowLayout already supports the wrapping of components to a new row on the panel. This issue is that the preferred size calculation assumes all components are placed on a single row. The WrapLayout overrides the preferred size calculation to support the wrapping of components on a new row.
Use a JScrollPane. If you never want a horizontal scroll bar you can add the following:
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
(By default the scroll pane will add horizontal and vertical scroll bars when required.)
The scroll pane itself will only be resizeable if you add it to a Container with the appropriate layout manager; e.g.
JFrame frm = new JFrame();
frm.setLayout(new BorderLayout());
JScrollPane sp = new JScrollPane();
frm.add(sp, BorderLayout.CENTER); // Adding a component to the CENTER will cause the component to grow as the frame is resized.

Categories