JavaFX getText from textfield when button pressed(FX Builder)


Question

Currently in the process of learning myself JavaFX.

I have used Scene Builder to create a simple Scene with a button, and a textfield.

How come I can not click the button, and get the text from the textfield?

@FXML
private void handleButton1Action(ActionEvent event) {

    System.out.println(tittel.getText());


}

The FXML code is :

  <TextField id="tittel" fx:id="tittel" layoutX="120.0" layoutY="64.0" promptText="Tittel" />
1
1
5/28/2014 9:06:17 AM

Accepted Answer

Either add the action handler in the scene builder or add this to the FXML:

onAction="#handleButton1Action"

So in all:

<TextField id="tittel" fx:id="tittel" layoutX="120.0" layoutY="64.0" onAction="#handleButton1Action" promptText="Tittel" />
3
5/28/2014 10:03:05 AM

Add this line to your FXML code onAction="#handleButton1Action"

like

<TextField id="tittel" fx:id="tittel" onAction="#handleButton1Action" layoutX="120.0" layoutY="64.0" promptText="Tittel" />

and add this line after your controller class declaration

@FXML private TextField tittel;

then add the action handler

@FXML
private void handleButton1Action(ActionEvent event) {

    System.out.println(tittel.getText());


}

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon