Lizurt
Lizurt

Reputation: 15

How to make outer pane scroll instead of inner one

Firstly, as you probably noticed in tags, I use RichTextFX. CodeArea is just its text area with some features.

I have ScrollPane with inner elements:

ScrollPane
|   AnchorPane
|   |   VBox
|   |   |   HBox
|   |   |   |   Label
|   |   |   |   CodeArea
|   |   |   HBox
|   |   |   |   Label
|   |   |   |   CodeArea
|   |   |   HBox
|   |   |   |   Label
|   |   |   |   CodeArea
...........................

When I'm trying to scroll while my cursor isn't hovering CodeArea, ScrollPane scrolling like it should do.

But if my cursor is on CodeArea, as I assume, it trying to scroll the CodeArea, even if there is nothing to scroll (content fits well).

ScrollEvent isn't being called on ScrollPane, AnchorPane nor CodeArea, but VBox.

I tried to bind properties like scrollPane.onScrollProperty().bind(vBox.onScrollProperty()) or scrollPane.onScrollProperty().bind(codeArea.onScrollProperty()) and many others but that didn't work.

This question doesn't affect classic TextArea.

How to make ScrollPane scroll instead of CodeArea when hovering CodeArea?

You can also use this example to reproduce the problem:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.control.ScrollPane;
import org.fxmisc.richtext.CodeArea;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        ScrollPane scrollPane = new ScrollPane();
        VBox vBox = new VBox();
        scrollPane.setContent(vBox);
        CodeArea codeArea = new CodeArea();
        codeArea.setPrefHeight(2000);
        Label label = new Label("Can you scroll here while hovering code area?");
        vBox.getChildren().addAll(codeArea, label);
        for (int i = 0; i < 256; i++) {
            codeArea.appendText("Sample text\n");
        }
        Scene scene = new Scene(scrollPane, 400, 400);
        stage.setScene(scene);
        stage.show();
    }
}

Upvotes: 0

Views: 149

Answers (1)

Jurgen
Jurgen

Reputation: 2154

In your example do:

codeArea.addEventFilter( ScrollEvent.ANY, scroll -> 
{
    vBox.fireEvent( scroll );
    scroll.consume();
});

Upvotes: 0

Related Questions