Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX getText from textfield when button pressed(FX Builder)

Tags:

javafx

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" />
like image 320
miniHessel Avatar asked Sep 13 '25 11:09

miniHessel


1 Answers

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());


}
like image 116
Sajidur Rahman Avatar answered Sep 16 '25 20:09

Sajidur Rahman