2

I'm trying to make a tabbed application with TabPane. Every tab contains a scroll pane and fxml-loaded node in it.

But tabs are blurry. Before this, it happened in TextArea and I have fixed it with disabling cache. Now, it is not working.

How can I fix this? Thanks.

A Small Example: (It looks like blurry to the my tests.)

HelloApplication.java

    package com.example.demo;
     
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
     
    import java.io.IOException;
     
    public class HelloApplication extends Application {
        @Override
        public void start(Stage stage) throws IOException {
            FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("demo.fxml"));
            Scene scene = new Scene(fxmlLoader.load(), 320, 240);
            stage.setTitle("Hello!");
            stage.setScene(scene);
            stage.show();
        }
     
        public static void main(String[] args) {
            launch();
        }
    }
     
     

Demo.java

    package com.example.demo;
     
    import javafx.collections.FXCollections;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Node;
    import javafx.scene.control.ProgressBar;
    import javafx.scene.control.ScrollPane;
    import javafx.scene.control.Tab;
    import javafx.scene.control.TabPane;
    import javafx.scene.image.ImageView;
    import javafx.scene.layout.AnchorPane;
     
    import java.io.IOException;
    import java.util.List;
     
    public class Demo {
        @FXML
        public ProgressBar progress;
        @FXML
        public AnchorPane menu;
        @FXML
        public ImageView head;
        @FXML
        public TabPane tab;
     
        private final List<Tab> tabs;
     
        public Demo(){
            tabs = FXCollections.observableArrayList();
        }
     
     
        @FXML
        public void initialize(){
            addTab();
        }
     
        private ScrollPane getScroll(){
            var pane = new ScrollPane();
            pane.setFitToWidth(true);
            return pane;
        }
     
        public void addTab(){
            var t = new Tab();
            t.setText("test");
     
            try{
                var loader = new FXMLLoader(getClass().getResource("/com/example/demo/demo2.fxml"));
                var scroll = getScroll();
                Node node = loader.load();
                scroll.setContent(node);
                t.setContent(scroll);
            } catch (IOException ignored) {
     
            }
     
            tabs.add(t);
            tab.getTabs().add(t);
        }
     
    }
     

demo.fxml

    <?import javafx.scene.control.*?>;
    <?import javafx.scene.layout.*?>;
     
    <AnchorPane
                xmlns:fx="http://javafx.com/fxml"
                 fx:c
                prefHeight="700.0" prefWidth="900.0" styleClass="main" stylesheets="@demo.css">
     
        <TabPane fx:id="tab" AnchorPane.topAnchor="10" AnchorPane.leftAnchor="10" AnchorPane.rightAnchor="10" AnchorPane.bottomAnchor="80" maxHeight="Infinity" maxWidth="Infinity"/>
    </AnchorPane>
     

demo2.fxml

    <?xml version="1.0" encoding="UTF-8"?>;
     
    <?import javafx.scene.control.*?>;
    <?import javafx.scene.layout.*?>;
     
    <AnchorPane
                prefHeight="400.0" prefWidth="600.0">
        <Label text="Lorem ipsum dolor sit amet"/>
    </AnchorPane>
     
     
     

demo.css

    .main{
        -fx-background-color: #202020;
    }
     
    *{
        -fx-cache: false;
    }

etkmlm
  • 53
  • 1
  • 6
  • 2
    (a) I see no blurring. (b) I see only a single tab, not plural. (c) "disabling cache" — what cache? – Basil Bourque Aug 27 '23 at 00:43
  • I mean node cache that can be set with setCache() method. It happening in all tabs, that is the reason why I said plural. In the screenshot, maybe it cannot be seen but it is blurry. – etkmlm Aug 27 '23 at 08:30
  • There is no performance issue. Before this, I researched TextArea and found a solution that says set caching to false, it worked, and the blur was gone. Now I tried the exact thing, it's not worked in the TabPane. https://stackoverflow.com/questions/26098295/scrollpane-content-becomes-blurry-after-dragging https://github.com/javafxports/openjdk-jfx/issues/225 – etkmlm Aug 27 '23 at 12:32
  • 1
    Which OS are you using? What's its architecture? Which version of Java and JavaFX are you using? What's your resolution? Can you provide a [mre] (I couldn't reproduce the blurriness from a quick mock-up based on your description)? – Slaw Aug 27 '23 at 14:51
  • @Basil (a) It may just be my less-than-ideal eyesight, my monitor, the image quality, or that the text is simply bigger, but the text in the tab's _content_ area looks blurry compared to the tab's title text. (c) Check out the [`Node.cache`](https://openjfx.io/javadoc/20/javafx.graphics/javafx/scene/Node.html#cacheProperty) property. – Slaw Aug 27 '23 at 14:54
  • @Slaw Windows 11 x64 Java 17 & JFX 17 1920x1080 125% Ok, I'll make a little example. – etkmlm Aug 27 '23 at 16:01
  • 2
    Please create a [mre] (and post it in the actual question) – James_D Aug 27 '23 at 16:30
  • 1
    Sure, I updated the post with the example. – etkmlm Aug 27 '23 at 16:48
  • I can't reproduce the blurriness on my system (JDK 20 and JavaFX 20.0.1 running on MacOS X Monterey 12.3, MacBook Pro M1). I agree that the screenshot you posted looks blurry. FWIW I think the cache is irrelevant; that property is `false` by default, as I understand it. If you remove the stylesheet entirely, do you still see the blurriness? – James_D Aug 27 '23 at 16:59
  • Yes, same as old. – etkmlm Aug 27 '23 at 17:01
  • This is just a wild guess, but see if adding `* { -fx-snap-to-pixel: true; }` to your stylesheet makes any difference. (That property should be true by default, so I'd be surprised if that works, but it looks like your tab content is misaligned with respect to pixel boundaries.) – James_D Aug 28 '23 at 13:17
  • @James_D As you said, no changes. It is true by default. – etkmlm Aug 28 '23 at 15:12

1 Answers1

2

I tried checking your demo on my system (Win 10 x64, JDK 17, FX 21) and it works perfectly well without any issue (as below).

enter image description here

However, I suspect this might be related to subpixel rendering issue. Again saying I SUSPECT only. To check that, I slightly moved the tabPane by .5 px and when ran the code, I can see the blurred text (as below).

@FXML
public void initialize(){
    addTab();
    tab.setTranslateX(.5);
    tab.setTranslateY(.5);
}

enter image description here

Usually I encounter this kind of blur related issues mostly on Path rendering and to fix it, I adjust the Path by .5 px.

So I am not sure whether this approach will work for you as well or not, but can you try adjusting the tabPane position by .5px as shown in above code, and see if it is fixing the issue?

If it works, then assume that THIS IS NOT A SOLUTION, BUT A TEMPORARY FIX.

Sai Dandem
  • 8,229
  • 11
  • 26
  • 3
    It worked. I changed my scale factor to 100% after trying your solution, and it broke again same as your try. So I researched about translation and scaling, your desc was enlightened me. As a result of my my research, I disabled Windows scaling by altering `glass.win.uiScale` to 100%. Now it works, but everything is smaller than old. It is the best solution until I find a more useful solution. – etkmlm Aug 28 '23 at 15:11