0

I have this TextField in FXMLDocument.fxml:

<TextField fx:id="searchBox" onKeyPressed="#updateText">
   <StackPane.margin>
      <Insets left="60.0" right="30.0" top="50.0" />
   </StackPane.margin>
</TextField>

The FXML document (where the above code resides) has the controller class named FXMLDocumentController.java. Inside this, I have the following code:

public class FXMLDocumentController {

    public TextField searchBox;

    @FXML
    public void updateText (KeyEvent e) {
        System.out.println(searchBox.getText());
    }
}

Note that the searchBox property is public. When I set it to private, my code throws an Exception at runtime. I have seen people specifying the "event bounded property" as private in Stack Overflow answers (here's one). Seeing those answers, I assume that specifying a property as private should not give any error. What's wrong with my code then?

  • This question is a duplicate. Thanks to James_D for pointing out :) The original post solved my problem –  Jul 09 '17 at 13:03

1 Answers1

0

Use @FXML Annotation over your TextField searchBox. See below code:-

public class FXMLDocumentController {

        @FXML
        private TextField searchBox;

        @FXML
        public void updateText (KeyEvent e) {
            System.out.println(searchBox.getText());
        }
    }
Satish Pahuja
  • 209
  • 1
  • 6
  • 20