Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

requestFocus for TextField

MuzibApr 29 2017 — edited May 8 2017

I know that a node's requestFocus() method doesn't work until -

  • it is visible,
  • disableProperty() is set to false, and
  • a scene owns it

but, if it is like this:

    TextField tf = new TextField(), tf2 = new TextField(), tf3 = new TextField();
    VBox holder = new VBox(5, tf, tf2, tf3);
    TitledPane tp = new TitledPane("Testing",holder);
    AnchorPane root = new AnchorPane(tp);

    @Override
    public void start(Stage primaryStage) {
        tp.setExpanded(false);
        tp.expandedProperty().addListener((c,o,n)->{
            if(n)
                tf.requestFocus();
        });
        Scene scene = new Scene(root, 300, 250);
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

then, all of the above is fulfilled but, still, textfield doesn't get focus if I expand the TitledPane.

Also, this doesn't seem to work:

Platform.runlater(()->tf.requestFocus());

Could anybody help me?

This post has been answered by jsmith on May 3 2017
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 2 2017
Added on Apr 29 2017
2 comments
7,117 views