1

I see a strange appearance of the text contained in a TextArea aftrer doing some changes of TextArea content and style. With the simplified code shown below I reproducibly see this when I click the button 4 times:

enter image description here

But this is what I expected to see:

enter image description here

Note: If I then click into the TextArea I see the expected result.

What can can be done to get the expected result?

Note that I need to set textarea min/max width and height to get a nice appearance of the content. Of course I could set it to a bigger value, but that would destroy the look that is required.

I tried setCache as proposed here but that did not work.

I have JavaFX-8 on Windows 8.1. I would also be interested what results are seen in newer versions.

EDIT

With JavaFX-13 the result is:

enter image description here

The text seems to be moved to the right instead of centered as specified in the css (and also to the bottom). I had ecpected that the text is postioned the same as on initial start of the application.

CSS:

.text-area-centered *.text {
    -fx-text-alignment: center ;
}
.text-area-centered .scroll-pane {
    -fx-hbar-policy: NEVER;
    -fx-vbar-policy: NEVER;
}

Java:

public class Main extends Application {
    private static final BackgroundFill blackBGF = new BackgroundFill(Color.BLACK, CornerRadii.EMPTY, Insets.EMPTY);
    private static final BackgroundFill whiteBGF = new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY);
    private static double textareaXY = 50; 
    private TextArea textarea = new TextArea();
    private int clickNo = 1;
    @Override
    public void start(Stage primaryStage) {

        BorderPane root = new BorderPane();
        Scene scene = new Scene(root,400,400);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);

        VBox vb = new VBox();
        root.setCenter(vb);
        Button b = new Button("ClickMe");
        b.addEventHandler(ActionEvent.ACTION, this::OnClickButton);
        vb.getChildren().add(b);
        vb.getChildren().add(textarea);

        textarea.setEditable(false);
        textarea.getStyleClass().add("text-area-centered");
        textarea.setBackground(new Background(blackBGF));
        textarea.setMinHeight(textareaXY);
        textarea.setMaxHeight(textareaXY);
        textarea.setMinWidth(textareaXY);
        textarea.setMaxWidth(textareaXY);
        textarea.setFont(new Font("Courier New",10));

        textarea.setText("1 2 3\n4 5 6\n7 8 9");

        primaryStage.show();
    }

    private void OnClickButton(ActionEvent event)
    {
        if(clickNo == 1)
        {
            textarea.setText("7");
            textarea.setFont(new Font("Courier New Bold",24));
        }
        else if(clickNo == 2)
        {
            Region region = ( Region ) textarea.lookup( ".content" );
            region.setBackground(new Background(blackBGF));
            textarea.setStyle("-fx-text-inner-color: white;");
        }
        else if(clickNo == 3)
        {
            Region region = ( Region ) textarea.lookup( ".content" );
            region.setBackground(new Background(whiteBGF));
            textarea.setStyle("-fx-text-inner-color: black;");
        }
        else if(clickNo == 4)
        {
            textarea.setText("1 2 3\n4 5 6\n7 8 9");
            textarea.setFont(new Font("Courier New",10));
        }
        clickNo++;
    }

    public static void main(String[] args) {
        launch(args);
    }
}
user7399085
  • 220
  • 1
  • 12
  • 1
    Unrelated to your problem: please learn java naming conventions and stick to them. – kleopatra Nov 19 '19 at 11:13
  • 2
    worksforme in fx11, misbehaves as described in fx8 - seems to be a bug that's fixed – kleopatra Nov 19 '19 at 11:19
  • If you are really "interested what results are seen in newer versions", why don't you just have a look yourself? Downloading and installing a more recent version of Java and JavaFX is not such a big issue. And as @kleopatra has already said, this bug has been fixed a long time ago. – mipa Nov 19 '19 at 16:36
  • @mipa: Could have been that there is a solution for fx8. Meanwhile I installed fx13 (including the 3rd Eclipse version that I have to run on my computer in parallel). Results see next comment. – user7399085 Nov 20 '19 at 11:57
  • @kleopatra: With fx13 the result looks better, but not quite as I expected. I will edit my post to show the new result. – user7399085 Nov 20 '19 at 11:59
  • yeah you are right - looks like there is still some "memory" to the bigger font somewhere .. – kleopatra Nov 20 '19 at 13:18
  • And why do you have to run multiple Eclipse versions in order to try that? – mipa Nov 20 '19 at 16:55
  • @mipa: Currently I still use Eclipse 2018-12 for most of my apps. For Gluon mobile app development I use 2018-09 since the Gluon PlugIn is apparently still not working in newer versions. To solve the TextArea problem I thought I should try the newest Java/JavaFX version with the appropriate Eclipse version (plugin Java 13 Support for Eclipse 2019-09) – user7399085 Nov 21 '19 at 10:33

0 Answers0