Labels with icons don't fit scrollpane - java

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

Related

Sizing ScrollPane inside a BorderLayout properly in javafx

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

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.

JavaFX: bind to scrollpane width and height without bar sizes

I have a StackPane, which is inside a ScrollPane. I want that minimal size of StackPane was as the size of ScrollPane. This is my code
stackPane.minWidthProperty().bind(scrollPane.widthProperty());
stackPane.minHeightProperty().bind(scrollPane.heightProperty());
However, using this code the scroll bars (vertical and horizontal) are always visible and there is a small scrolling. I suppose that I include scroll bar size in minimal size of StackPane. How can I make StackPane minimal size be equal to ScrollPane and scroll bars are not visible if they are not needed?
Late Answer but maybe someone needs a solution.
All you need to do is to add a small offset like that:
stackPane.minWidthProperty().bind(scrollPane.widthProperty().subtract(20));
stackPane.minHeightProperty().bind(scrollPane.heightProperty().subtract(20));

How can I set scroll tab layout to my JTabbedPane?

I have a JTabbedPane. I drag it from the palette and paste it in my form. I put 5 panel on it. It is OK but when I put 6, panel on it it does not seen on the form because the place is not enough width. I put this code
tab_group.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
But doesn't affect my code. How can I fix it?
try this it will help.
JScrollPane pane = new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(pane);
add Jscrollpane to panel. problem solve

How to set only the preferred width of Panel with flow layout?

I have a panel with flow layout, and it can contain a variable number of items - from 1 to 2000. I want to put it inside a scroll pane, scrollable in vertical direction, and with fixed width. The problem is, when I set preferred size of panel to something like (800,600), some items are missing, and there is no scroll. If I set up preferred size of scroll pane, then all elements in flow pane are put on one very long line.
Setting maximum size on any element seems to do nothing at all - layout managers ignore it.
How can I fix this?
I want to put it inside a scroll pane, scrollable in vertical direction, and with fixed width
You can use the Wrap Layout for this.
Don't set the preferred size of the panel. But you can set the preferred size of the scroll pane so the frame.pack() method will work.
You could use BoxLayout to do this:
JPanel verticalPane = new JPanel();
verticalPane.setLayout(new BoxLayout(verticalPane, BoxLayout.Y_AXIS));
JScrollPane pane = new JScrollPane(verticalPane);
//add what you want to verticalPane
verticalPane.add(new JButton("foo"));
verticalPane.add(new JButton("bar"));
This of course will use the preferred size of each component added. If you want to modify the preferred size for example of a JPanel, extend it and override getPreferredSize:
class MyPanel extends JPanel(){
public Dimension getPreferredSize(){
return new Dimension(100,100);
}
}
A note: BoxLayout will take in consideration getPreferredSize, other LayoutManager may not.
Please criticize my answer, I'm not sure it's completely correct and I'm curious to hear objections in order to know if I understood the problem.

Categories