So the main issue what i have is when clicking on a ScrollBar Button. The Viewport of the Scrollpane changes, but not the children (Node) of the pane. I have either to request the focus on the pane or click on the icon in the taskmenu, that the Viewport content get's refreshed. Swing does this automatically when you move the scrollbars.
Is there another way to get this functionality working as described above and by code? The call requestFocus() seems strange to me. By the way: it highlights everytime that bothers.
Here is what i got so far:
public class ScrollPaneTest extends Application {
private ScrollPane scrollPane;
private StackPane stackPane;
private Pane pane;
private Button button;
@Override
public void start(Stage primaryStage) throws Exception {
scrollPane = new ScrollPane();
stackPane = new StackPane();
pane = new Pane();
button = new Button();
scrollPane.setPrefHeight(400.0);
scrollPane.setPrefWidth(600.0);
stackPane.setPrefHeight(1000.0);
stackPane.setPrefWidth(1000.0);
pane.setPrefHeight(200.0);
pane.setPrefWidth(200.0);
button.setMnemonicParsing(false);
button.setText("Button");
pane.getChildren().add(button);
stackPane.getChildren().add(pane);
scrollPane.setContent(stackPane);
scrollPane.viewportBoundsProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
Bounds oldViewportBounds = (Bounds) oldValue;
Bounds newViewportBounds = (Bounds) newValue;
double x = newViewportBounds.getMinX() * -1;
double y = newViewportBounds.getMinY() * -1;
button.setLayoutX(x);
button.setLayoutY(y);
// Working if focus is requested ...
//pane.requestFocus();
// ... or when clicked on the application icon in the taskmenu of windows
// Doesn't work
//pane.requestLayout();
}
});
VBox vbox = new VBox();
vbox.setPrefHeight(400.0);
vbox.setPrefWidth(600.0);
vbox.getChildren().add(scrollPane);
Scene scene = new Scene(vbox);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
} }
For illustration purposes: