JavaFX imput in textfields


I'm doing a program to calculate arrays, but I do not know how by an action for the "Confirm" button to get the values typed in the TextLabels (tl_lines and tl_columns) and move to the Int x and Int Y variables.

package matrizes;

import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;

public class FXMLDocumentController implements Initializable {

    private TextField tf_linhas;

    private Button btnTexto;

    private Button btnLC;

    private TextField tf_colunas;

    private final AnchorPane apMatriz = new AnchorPane();

      public void btnLC(ActionEvent e){


    public void initialize(URL url, ResourceBundle rb) {

        int x = 0;
        int y = 0;

        for(int i = 1; i < x+1; i++) {
            for(int j = 1; j < y+1; j++){
                Button btn = new Button();


asked by anonymous 20.03.2018 / 01:13

1 answer


The desired action can be implemented in the setOnMouseClicked method of the button, like this:

btn.setOnMouseClicked((event) -> {
    x = Integer.parseInt(tlLinhas.getText());
    y = Integer.parseInt(tlColunas.getText());

However, the variables x and y will have to become class variables. Java does not allow local variables to be used in an inner class, unless the variable is final.

06.04.2018 / 03:41