Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScrollPane content becomes blurry after dragging

Tags:

java

javafx-8

JavaFX 8.0 has this bug and I don't know how to solve it.
Example: http://i.imgur.com/tavh6XA.png

If I drag ScrollPane, its content becomes blurry, but if I drag it back, the content restores its sharpness. If I do not modify coords, the content looks pretty well.

Did anybody solve this problem?

Example code:

@Override
public void start(Stage stage) {
            Pane pane = new Pane();

    Scene scene = new Scene(pane, 500, 500);
    pane.prefWidthProperty().bind(scene.widthProperty());
    pane.prefHeightProperty().bind(scene.heightProperty());

    ScrollPane scroll = new ScrollPane();

    // Center ScrollPane
    scroll.layoutXProperty().bind(pane.widthProperty().divide(2).subtract(scroll.widthProperty().divide(2)));
    scroll.layoutYProperty().bind(pane.heightProperty().divide(2).subtract(scroll.heightProperty().divide(2)));
    scroll.setPrefSize(200, 200);

    ListView<String> list = new ListView<>();
    scroll.setContent(list);

    list.getItems().addAll("Resize the window", "and see how this list", "becomes blurry");

    // Label indicates x, y coords of ScrolPane and their ratio.
    TextField size = new TextField();
    size.setPrefWidth(500);
    size.textProperty().bind(
            scroll.layoutXProperty()
            .asString("x: %s; ").concat(
                    scroll.layoutYProperty()
                    .asString("y: %s; ")));

    pane.getChildren().addAll(size, scroll);

    stage.setScene(scene);
    stage.show();
}
like image 373
turikhay Avatar asked Sep 29 '14 10:09

turikhay


1 Answers

I tried the thebradness's answer, but I get a java.lang.reflect.InaccessibleObjectException (Java 10) probably because of the Java Platform Module System. Rather than searching a workaround for making the reflection working, I prefered looking for another way to achieve it.

After checking the ScrollPaneSkin internal code, I noticed that the viewport css class is given to the viewRect argument. So I can Node#lookup it via CSS selectors rather than get it via reflection.

It's a simplier and more readable solution (and working in JAVA 9+) :

public void fixBlurryText(ScrollPane scrollPane) {
    StackPane stackPane = (StackPane) scrollPane.lookup("ScrollPane .viewport");
    stackPane.setCache(false);
}

Hope it will help someone else.

like image 166
TheZopo Avatar answered Sep 28 '22 12:09

TheZopo