JavaFX TextArea - unwanted ScrollBars when resizing

2.6k views Asked by At

I have a problem with resizing update/event of TextArea in JavaFX. For illustration I created empty JavaFX project through IntelliJ Idea with AnchorPane as root pane and that AnchorPane contains TextArea with propecties AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" in sample.fxml file (in a short: TextArea takes all space of scene).

Problem description: I launch application and it starts in a small window (300 x 275). I just maximalize it. There is normal behavioral, but when I got back in a window, both of scrollbars were shown. The similar situation happens when I am resizing window into smaller window. When I start with scrolling, nothing happens with TextArea viewport. When I start to write some letter or resize window into bigger window, scrollbars disappeared. That's very strange behavioral!

My question: Is there any listener or method for catch/stop showing scrollbars when it isn't necessary? Have you got this problem too?

Screenshot after return from maximized form

Screenshot after return from maximized form

Main.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


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

Controller.java

package sample;

public class Controller {
}

sample.fxml

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

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="480.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <TextArea layoutX="162.0" layoutY="66.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
   </children>
</AnchorPane>

PS: Sorry for my english and repeat insertion of that question, but if it's bug of JavaFX, it's very acute to resolve where is problem!

3

There are 3 answers

1
mipa On BEST ANSWER

This bug seems to be fixed in JDK 8u60. At least I can't reproduce it anymore on my MacBook Pro (Retina). So there is no need anymore for a complicated work arround :-) (Maybe someone is so kind to confirm that for Windows too.) Michael

3
Marco Montalto M. On

This will hide the horizontal scroll bar:

textArea.setWrapText(true);

This will hide the vertical scroll bar:

ScrollBar scrollBarv = (ScrollBar)textArea.lookup(".scroll-bar:vertical");
verticalScrollBar.setDisable(true);
0
krsi On

This worked for me.

It is a listener class that takes TextArea as an argument, listens to its width and height properties, and does some tricks that fix the problem.

As far as I tested, functionality of TextArea remains intact.

public class TextAreaListener implements ChangeListener {

    private TextArea textArea;
    private ScrollPane textAreaScrollPane;
    private Region textAreaContent;

    protected TextAreaListener(TextArea textArea) {
        this.textArea = textArea;
        textArea.skinProperty().addListener(this);
        textArea.widthProperty().addListener(this);
        textArea.heightProperty().addListener(this);
    }

    @Override
    public void changed(ObservableValue observable, Object oldValue, Object newValue) {
        if (observable == textArea.skinProperty()) {
            TextAreaSkin textAreaSkin = (TextAreaSkin) textArea.getSkin();
            textAreaScrollPane = (ScrollPane) textAreaSkin.getChildren().get(0);
            textAreaContent = (Region) textAreaScrollPane.getContent();
            textAreaContent.widthProperty().addListener(this);
            textAreaContent.heightProperty().addListener(this);
        } else if (observable == textAreaContent.widthProperty()
                || observable == textArea.widthProperty()
                || observable == textAreaContent.heightProperty()
                || observable == textArea.heightProperty()) {
             String text = textArea.getText();
             int caretPosition = textArea.getCaretPosition();
             double hValue = textAreaScrollPane.getHvalue();
             double vValue = textAreaScrollPane.getVvalue();
             textArea.layout();
             textArea.clear();
             textArea.setText(text);
             textArea.positionCaret(caretPosition);
             textAreaScrollPane.setHvalue(hValue);
             textAreaScrollPane.setVvalue(vValue);
        }
    }

}