Javafx imput in textfields

Asked

Viewed 34 times

2

I’m making a program to calculate matrices, but I don’t know how to put an action to the "Confirm" button to take the typed values of the Textlabels (tl_rows and tl_columns) and pass to the variables Int x and Int Y.

package matrizes;

import java.net.URL;
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 {


    @FXML
    private TextField tf_linhas;

    @FXML
    private Button btnTexto;

    @FXML
    private Button btnLC;

    @FXML
    private TextField tf_colunas;

    @FXML
    private final AnchorPane apMatriz = new AnchorPane();

      public void btnLC(ActionEvent e){


      }

    @Override
    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();
                btn.setPrefWidth(40);
                btn.setLayoutX(i*45);
                btn.setLayoutY(j*30);
                btn.setText("a"+i+""+j);
            apMatriz.getChildren().add(btn);
            }
        }

        }

    }

1 answer

1

The desired action can be implemented in the method setOnMouseClicked of the button, that way:

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.