3

I'm trying to learn JavaFX. To do so I've been attempting to make a text editor that includes multiple line text box support, as well as the possibility of having syntax highlighting down the road.

Currently, the biggest problem I've been facing is that the ScrollPane I've been encapsulating all my FlowPanes in won't resize according to the size of the Pane it's in. I've been researching this problem for about half a week now and simply cannot get the ScrollPane to just fill the window it's in. The code below displays a JavaFX stage that has working keyboard input and the ScrollPane is always the same size no matter what. Thanks to all in advance!

Here's my Main:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Launcher extends Application {
    public static void main(String[] args) {
        launch(args);
    }


    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setScene(new Scene(new DynamicTextBox(),500,500));
        primaryStage.show();
    }
}

TextBox class:

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.geometry.Orientation;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;

public class DynamicTextBox extends Pane {
    //currentLinePane is made to handle all the direct user inputs
    //multiLinePane, while not really used yet will create a new line when                   the enter key is struck.
    private FlowPane currentLinePane, multiLinePane;
    private ScrollPane editorScroller;

    public DynamicTextBox() {
        super();

        currentLinePane = new FlowPane(Orientation.HORIZONTAL);
        multiLinePane = new FlowPane(Orientation.VERTICAL);
        multiLinePane.getChildren().add(currentLinePane);

        editorScroller = new ScrollPane(multiLinePane);
        editorScroller.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
        editorScroller.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        editorScroller.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                configureInput(event);
            }
        });

        super.getChildren().add(editorScroller);
        editorScroller.requestFocus();
    }

    private void configureInput(KeyEvent event) {
        currentLinePane.getChildren().add(new Text(event.getText()));
    }
}
Ben Rasmussen
  • 297
  • 3
  • 5

1 Answers1

0

You're using

ScrollPane.ScrollBarPolicy.AS_NEEDED

which, according to the docs at Oracle, "Indicates that a scroll bar should be shown when required." Instead, use

ScrollPane.ScrollBarPolicy.ALWAYS

alternatively, recall these are constants. you can get the height of the parent using boundsInParent: https://docs.oracle.com/javafx/2/api/javafx/scene/Node.html#boundsInParentProperty

alternatively, you can use getParent() to get the parent and then get its height using computeMinWidth() https://docs.oracle.com/javafx/2/api/javafx/scene/Node.html#getParent()

Don Larynx
  • 685
  • 5
  • 15
  • hmm, well the scroll bar policy I did on purpose and works perfectly fine, that's how i wanted it. When I run this there's just no reason I can find as to why it's choosing this random size. I'm pretty salty about this switch to javafx so far hah I already know swing and although swing has a lot of quirks I'm pretty sure it would just adjust the FlowPane according to the window size – Ben Rasmussen Apr 06 '15 at 01:12
  • Actually I may of just found something good, my FlowPanes may be the culprit since apparently their default wrap length is 400 https://docs.oracle.com/javafx/2/api/javafx/scene/layout/FlowPane.html – Ben Rasmussen Apr 06 '15 at 01:16