JavaFX TextArea - unwanted scrollbars when resizing

I have a problem with TextArea resize / event in JavaFX. To illustrate, I created an empty JavaFX project through IntelliJ Idea with AnchorPane as the root panel and that AnchorPane contains TextArea with prospectuses AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"in the sample.fxml file (in short: TextArea takes up the whole scene space).

Description of the problem: I run the application and it starts in a small window (300 x 275). I just maximize it. There is normal behavior, but when I returned to the window, both scroll bars were shown. A similar situation occurs when I resize a window to a smaller window. When I start scrolling, nothing happens with the TextArea viewport. When I start writing a letter or resizing a window to a larger window, the scroll bars disappear. This is a very strange behavior!

My question is: Is there any listener or catch / stop method showing scroll bars when not needed? Do you have this problem too?

Screenshot after returning 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: , , JavaFX, , !

+4
3

, , ​​ JDK 8u60. , MacBook Pro (Retina). arround:-) ( , - , Windows .)

+2

:

textArea.setWrapText(true);

:

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

.

This is a listener class that takes TextAreaas an argument, listens for its width and height properties, and performs some tricks that fix the problem.

As far as I checked, the functionality TextArearemains 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);
        }
    }

}
0
source

All Articles