2 Replies Latest reply: Aug 21, 2013 10:00 AM by 0ba15464-6294-40de-bcf8-7cd809e203bc RSS

    Access to TextArea's Scroll Pane or Scroll Bars

    0ba15464-6294-40de-bcf8-7cd809e203bc

      I am trying to get notified by the TextArea to tell me when the scrolling has reached the bottom.  I know I can do this with ScrollPane's vvalueProperty, but I don't have access to that in the TextArea.  is there a way to get at this or to know when the scrolling has stopped and I can check the location of the scroll?  I tries the TextAreas onScrollFinished with out any success.  It never fired the event except when the mouse wheal tried to over scroll when it was already at the bottom.

        • 1. Re: Access to TextArea's Scroll Pane or Scroll Bars
          James_D

          You can get the ScrollPane via a lookup. I don't really like recommending lookups, but sometimes there seems to be no other way; and in this case the css structure is at least documented in the css reference.

           

           

          import javafx.application.Application;
          import javafx.beans.binding.Bindings;
          import javafx.scene.Scene;
          import javafx.scene.control.Label;
          import javafx.scene.control.ScrollPane;
          import javafx.scene.control.TextArea;
          import javafx.scene.layout.BorderPane;
          import javafx.stage.Stage;
          
          public class TextAreaScrollTest extends Application {
          
           @Override
            public void start(Stage primaryStage) {
            final BorderPane root = new BorderPane();
            final TextArea textArea = new TextArea();
            final Label label = new Label();
            root.setTop(label);
            root.setCenter(textArea);
            primaryStage.setScene(new Scene(root, 400, 300));
            primaryStage.show();
          
            ScrollPane scrollPane = (ScrollPane) textArea.lookup(".scroll-pane");
            label.textProperty().bind(Bindings.format("Vertical scroll at %.3f", scrollPane.vvalueProperty()));
            }
          
            public static void main(String[] args) {
            launch(args);
            }
          }
          

           

          Be careful when you call textArea.lookup(...). The text area needs to have had css styles applied in order for this to work; typically this means it has been attached to a scene which is displayed in a stage. (If you move the lookup call above to just before the call to primaryStage.show(), for example, you will get a nice NullPointerException.)

          • 2. Re: Access to TextArea's Scroll Pane or Scroll Bars
            0ba15464-6294-40de-bcf8-7cd809e203bc

            Worked like a charm.  thanks for your help!!!