2

In Swing I was to add JTextArea into JScrollPane in order JTextArea to have scroll bars. When I do the same in JavaFX, the behavior is different.

This example

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;

public class SlideshowForSlipryPage2 extends Application {

    @Override
    public void start(Stage primaryStage) {


        primaryStage.setTitle("SlideshowForSlipryPage");
        primaryStage.setScene(
            new Scene(


                    new ScrollPane(
                        new TextArea() {{
                            setPromptText("[PROMPT 1]");
                        }}
                    )


                , 300, 250
            )
        );
        primaryStage.show();

    }

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

gives

enter image description here

i.e. text area is wider than a window and shorter than it.

Why and how to fix?

Dims
  • 47,675
  • 117
  • 331
  • 600
  • 1
    What exactly is the desired result. The `ScrollPane` occupies exactly the area available in the window, nothing more, nothing less. – fabian Oct 13 '14 at 11:12

3 Answers3

2

This issue has nothing to do with the pref width of the text area.

You must set the wrap property to true so that the text will wrap onto two lines when the cursor hits the edge of the container. 2 ways:

1) Using scene builder is the most simple way. Simple click "Wrap Text" in the properties of the TextArea.

2) Call the following method of the TextArea:

textArea.setWrapText(true);

2

You have to call setFitToWidth(true) and setFitToHeight(true) on the ScrollPane, you can set those properties in the scene builder Layout tab after selecting the ScrollPane.

enter image description here

Fabio Frumento
  • 300
  • 2
  • 8
1

Try this:

textArea.setPrefSize( Double.MAX_VALUE, Double.MAX_VALUE ); 

and set scrollpane size same as parent like this:

scrollPane.prefWidthProperty().bind(<parentControl>.prefWidthProperty());
scrollPane.prefHeightProperty().bind(<parentConrol>.prefHeightProperty());

I hope it resolves your query !!!

DeepInJava
  • 1,871
  • 3
  • 16
  • 31