0

I have a couple of line charts inside GridPane:

@FXML
private GridPane LineChartVBox = new GridPane();

And this is my Scrollbar:

ScrollBar sc = new ScrollBar();
sc.setMin(0);
sc.setOrientation(Orientation.VERTICAL);
hbox1.getChildren().addAll(LineChartVBox,sc);
sc.valueProperty().addListener(new ChangeListener<Number>() {
    public void changed(ObservableValue<? extends Number> ov,
                        Number old_val, Number new_val) {
        LineChartVBox.setLayoutY(-new_val.doubleValue());
    }
});

I have a problem with the scrollbar because it does not do anything and I can not even see the end to it. Here is the example of it: scrollbar_with enter image description here

Community
  • 1
  • 1
Lazar Gugleta
  • 115
  • 1
  • 2
  • 14
  • 1
    Instead of using a ScrollBar, place your GridPane inside a [ScrollPane](https://openjfx.io/javadoc/13/javafx.controls/javafx/scene/control/ScrollPane.html). – VGR Jan 05 '20 at 21:16
  • I tried that too, but then it does not even give me the scrolling option... – Lazar Gugleta Jan 05 '20 at 21:26
  • 2
    Usually, the default behavior of a ScrollPane is not to show a scrollbar until it is needed. Meaning, if your GridPane isn’t larger than the display area, no scrollbar will be visible. This behavior can be altered by changing the [vbarPolicy](https://openjfx.io/javadoc/13/javafx.controls/javafx/scene/control/ScrollPane.html#vbarPolicyProperty) of the ScrollPane. – VGR Jan 05 '20 at 21:53
  • A `HBox` assigns `layoutX` and `layoutY` of children during a layout pass. If you modify those properties for one of it's children, this simply triggers another layout pass undoing those modifications. You could use `translateY` instead to move the child relative to the position assigned by it's parent, but I strongly recommend using `ScrollPane`. If you put a child in a `HBox` with a min height that exceeds the scene size, this results in undefined behaviour. in this case `fillHeight` results in all children being resized to that height... – fabian Jan 05 '20 at 22:22

1 Answers1

2

I agree with VGR you should use a ScrollPane. However its seems your ScrollBar is too height, you can try :

sc.maxHeightProperty().bind(scene.heightProperty());

I copy-paste your code and your Listener is working, I had'nt tested with the same HBox and I suppose its also the HBox which fix the position of the GridPane.

You can try to bind() manually your ScrollBar position, but you can may be try to use LineChartVBox.setTranslateY(-new_val.doubleValue());

ROMAINPC
  • 81
  • 6