Blurred text in JavaFX TextArea
Asked Answered
T

6

14

I am having a weird issue with JavaFX. I designed a simple Scene in SceneBuilder. The text looks ok in a preview, but in the running the app the text looks blurred, consider the following image. example Here is the FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>

<AnchorPane id="AnchorPane" fx:id="root" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="br.mviccari.javafxtest.controllers.TelaMensagemController">
  <children>
    <TitledPane alignment="CENTER_LEFT" animated="false" collapsible="false" contentDisplay="LEFT" prefHeight="400.0" prefWidth="600.0" text="Janela legal" textAlignment="LEFT" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
      <content>
        <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
          <children>
            <HBox id="HBox" alignment="CENTER" spacing="5.0" AnchorPane.bottomAnchor="14.0" AnchorPane.rightAnchor="14.0">
              <children>
                <Button mnemonicParsing="false" onAction="#acaoFodase" prefHeight="53.9609375" prefWidth="128.0" text="Foda-se" />
                <Button mnemonicParsing="false" onAction="#acaoOk" prefHeight="53.9609375" prefWidth="128.0" text="OK" />
              </children>
            </HBox>
            <TextArea fx:id="campoTexto" disable="false" editable="true" focusTraversable="true" prefHeight="278.0" prefWidth="570.0" rotate="0.0" text="Aqui vai um texto padrão motherfucker!" wrapText="true" AnchorPane.bottomAnchor="82.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="12.0" AnchorPane.topAnchor="14.0" />
          </children>
        </AnchorPane>
      </content>
    </TitledPane>
  </children>
</AnchorPane>

And here is the application class code:

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/br/mviccari/javafxtest/layouts/telaMensagem.fxml"));
Parent root = fxmlLoader.load();
TelaMensagemController controller = fxmlLoader.getController();
controller.setParametroDeTeste("PIKACHU EU ESCOLHO VC!");
controller.init();
Stage stage = new Stage(StageStyle.DECORATED);
stage.initModality(Modality.WINDOW_MODAL);
stage.initOwner(labelStatus.getScene().getWindow());
stage.setOpacity(1);
stage.setTitle("My New Stage Title");
stage.setScene(new Scene(root, 450, 450));
stage.showAndWait();

I have tried to run the app using JDK 7, this way the text is not blurred anymore, but how can I run it with JDK 8 showing a normal text

I have put my project on bitbucket, if anyone tries to see the code or checkout to your machine, you can see this on your own

https://[email protected]/mateusviccari/testejavafx.git

Tavern answered 19/5, 2014 at 1:23 Comment(2)
Are you on a HiDPI screen? What version of Java are you using?Motorman
no it's not a hiDPI screen, java -version = 1.8.0. Actually, i forgot to mention it before, but the two image samples that i put in the question were taken on the same machine.Tavern
H
16

I have figured out a solution for this issue. I was able to ascertain that the issue is centered around a bug introduced in JavaFX 8 which causes some blurriness of the content displayed within a ScrollPane when said ScrollPane has decimal value constraints, the bug has to do with the cached image of the content so turning off caching works. TextAreas make use of ScrollPanes.

textArea.setCache(false);
ScrollPane sp = (ScrollPane)textArea.getChildrenUnmodifiable().get(0);
sp.setCache(false);
for (Node n : sp.getChildrenUnmodifiable()) {
    n.setCache(false);
}
Homogeneity answered 25/7, 2015 at 3:38 Comment(1)
Nice catch, tnaks! I tried running this on initialization of the control, but it seems the ScrollPane isn't ready at that point yet. I had to wrap it in Platform.runLater([...]) to work correctly.Comptom
I
4

If someone has a similar problem inside PopOver from ControlsFX: Blurred text inside text controls (WebView, TextArea, etc).

The solution is to override the -fx-stroke-width attribute inside the .popover > .border selector:

.popover > .border { -fx-stroke-width: 1; }

Example:

val popOver = PopOver()
val webView = WebView()
webView.engine.loadContent("<html>Some text that should be not blurred</html>")
popOver.contentNode = webView
popOver.root.stylesheets.add(this.javaClass.getResource("test.css").toString())


var b = Button("Press")
val scene = Scene(StackPane(b), 800.0, 600.0)

b.onAction = EventHandler { popOver.show(b) }

primaryStage.scene = scene
primaryStage.show()

enter image description here

Illeetvilaine answered 12/4, 2018 at 10:53 Comment(1)
Thanks a lot, this fixed my issue with blurry text in my personal project :)Contradictory
N
2

As it has been pointed out, the decimal value constraints cause the blurry text. Thus, setting padding of the scroll Pane to 0 is also enough to make text non-blurry.

This css will fix the issue:

.text-area > .scroll-pane {
    -fx-padding: 0;
}
Natter answered 27/12, 2020 at 9:37 Comment(1)
My Javafx WebView suffered from blurry text and elements e.g. buttons etc. Turns out the solution is to find the nearest parent pane and set its style to "-fx-padding: 0" which can be done either via SceneBuilder (in the inspector for e.g. AnchorPane go to Properties / Style - and add the style entry ) or via code e.g. anchorPaneWebview1.setStyle("-fx-padding: 0”)Date
L
0

I can't say why the text is blur, maybe it is because of the font used, or styles applied. But that difference in your image is coming from the different versions of JavaFX used in. SceneBuilder is using the JavaFX 2 while executed one is using javaFX 8. Configure your Java environment to use the same version.

Determine the version used like as:

primaryStage.setTitle(com.sun.javafx.runtime.VersionInfo.getRuntimeVersion());

On my Windows 8 machine the text in text area shown as follow without blurring. Note the version numbers in titles: enter image description here

Longs answered 19/5, 2014 at 6:11 Comment(4)
The version shown is 8.0.0-b132, same as yours. I have tried it on a windoes 7 machine in my work, also with the same runtime version as my home pc (home pc uses runs 8)Tavern
I have tried switching to JDK 7 and this way the text is shown without blur... Bot how to show it like this with JDK 8?Tavern
@MateusViccari. Are you applying any styles? Is the blurring effect shown only in textarea or other controls (textfield, label etc) as well?Longs
No styles applied, the blurry effect happens only with TextArea.Tavern
A
0

I had a similar issue with the blurry text. Using JavaFX Scene Builder, I noticed that under Layout: Position, I had Layout X / Y a non zero value (like 9.893739 etc). when I replaced those with 0, the blurry text went away.

Aguascalientes answered 30/5, 2014 at 21:11 Comment(0)
S
0

The related bug JDK-8211294 is fixed now in OpenJfx16 release, although I still see same problem with text areas at Dpi scaling level > 100%

My current scaling level is 125% and even the treeView and ListViews were also blurry, So at least I could fix them, by turning OFF 'Cache' and 'Snap To Pixel'. And it worked like charm.

Sinuation answered 18/6, 2021 at 15:39 Comment(2)
turning OFF 'Cache' and 'Snap To Pixel' <- How to do that?Soliz
In Fxml file, add below properties. (You can easily do this using SceneBuilder UI) scaleShape="false" cacheShape="false"snapToPixel="false" For scrollpane, In some places, I also had to use the following for better results - scrollPane.lookup(".viewport").setCache(false); I saved it as a method and call it on all the scrollPanes and this takes out all the blurrinessSinuation

© 2022 - 2024 — McMap. All rights reserved.