Access a textField that is inside a gridPane in javafx

Asked

Viewed 288 times

1

I have been developing a master’s degree in javafx: something that receives an image, puts a grid with textFields on all cells above the image.

The user can then put values in the textFields. The grid is created automatically, depending on the number of rows and columns the user wants.

This is my view controller class:

package vistas;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ResourceBundle;

import javax.print.DocFlavor.URL;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.stage.FileChooser;

import principal.main;

public class AreaProjectoController {

    @FXML
    private Button meuBotaoImagem, meuBotaoGrelha, btnLimpaGrelha, btnArea;

    @FXML
    private ImageView minhaImagem;

    @FXML
    private TextField txtGrelhaLeft, txtGrelhaRight;

    @FXML
    private AnchorPane painelGrelha, painelArea;

    @FXML
    private GridPane painelCriaGrelha;

    public void initialize(URL location, ResourceBundle resources) {
    }

    public void mostraImagem(ActionEvent evento) throws FileNotFoundException {

        FileChooser imagemEscolhida = new FileChooser();

        // Define a extensão do ficheiro
        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("JPEG/PNG", "*.jpeg", "*.png");
        imagemEscolhida.getExtensionFilters().add(extFilter);

        // abre a janela para procurar uma imagem
        File ficheiro = imagemEscolhida.showOpenDialog(main.getPrimaryStage());

        // O if trata quando se cancela o carregamento de uma imagem
        if(ficheiro != null) {
            Image imagem = new Image(new FileInputStream(ficheiro));
            minhaImagem.setImage(imagem);
            painelGrelha.setVisible(true);
            txtGrelhaLeft.setText("0");
            txtGrelhaRight.setText("0");
        } 

    }

    public void criaGrelha(ActionEvent evento) throws FileNotFoundException, IOException {

        int txtLeft = 0, txtRight = 0, i = 1, ii = 1, l = 0, j = 0;

        ColumnConstraints colunas = null; 
        RowConstraints linhas = null;

        txtLeft = Integer.parseInt(txtGrelhaLeft.getText());
        txtRight = Integer.parseInt(txtGrelhaRight.getText());

        //Este for cria a grelha após receber os valores
        for(i = 1; i <= txtLeft; i++) {
            colunas = new ColumnConstraints();
            colunas.setPercentWidth(25);
            painelCriaGrelha.getColumnConstraints().add(colunas);
        }

        for(ii = 1; ii <= txtRight; ii++) {
            linhas = new RowConstraints();
            linhas.setPercentHeight(25);
            painelCriaGrelha.getRowConstraints().add(linhas);
        }

        //Label fillLabel[][] = new Label[txtLeft][txtRight];
        TextField fillLabel[][] = new TextField[txtLeft][txtRight];

        for (l = 0; l < txtLeft; l++) {
            for (j = 0; j < txtRight; j++) {
                fillLabel[l][j] = new TextField();
                //fillLabel[l][j] = new Label();
                fillLabel[l][j].setText(String.valueOf(0));
                fillLabel[l][j].setBackground(null);
                painelCriaGrelha.add(fillLabel[l][j], l, j);


            }
        }

        /*for (l = 0; l < fillLabel.length; l++) {
            for (j = 0; j < fillLabel.length; j++) {
                System.out.println(fillLabel[l][j].getText());
            }
            System.out.println(" ");
        }*/

        painelCriaGrelha.setGridLinesVisible(true); //isto faz aparecer as linhas da grelha a preto
        painelArea.setVisible(true);



        //for(l = 1; l <= fillLabel.length; l++) {

        //System.out.println(l);
    //  }

        //Func.salvaInfoGrelha(listaElementosArea);

    }

    public void limpaGrelha(ActionEvent evento) {

        painelCriaGrelha.getColumnConstraints().clear();
        painelCriaGrelha.getRowConstraints().clear();
        txtGrelhaLeft.setText("0");
        txtGrelhaRight.setText("0");

    }

    public void defineArea(ActionEvent evento) {

        int txtLeft = 0, txtRight = 0;
        int l = 0, j = 0;

        txtLeft = Integer.parseInt(txtGrelhaLeft.getText());
        txtRight = Integer.parseInt(txtGrelhaRight.getText());


        TextField fillLabel[][] = new TextField[txtLeft][txtRight];// = new S[txtLeft][txtRight];
    //GridPane painel = new GridPane();

    System.out.println(txtLeft);
    System.out.println(txtRight);

    //TextField texto = fillLabel[1][1];

    painelCriaGrelha.getChildren().get(fillLabel[1][1].getText());
    System.out.println(painelCriaGrelha.getChildren().get(fillLabel[1][1]).getText());

        /*for (l = 0; l < txtLeft; l++) {
            for (j = 0; j < txtRight; j++) {
                System.out.println(painelCriaGrelha.getChildren().get(fillLabel[1][1]));
            }
        }*/

        //System.out.println(texto);

    }

}

My question is in the last job. At the end of the user fill the cells and click on the "defineArea" button he was supposed to be able to store the grid values in an Array. I tried to convert to Int but also could not.

I can’t access the contents of the gridPane textField, maybe I’m not building my function correctly, but I think I’m thinking well, when I have access with the line:

painelCriaGrelha.getChildren().get(fillLabel[1][1]);

1 answer

0


the method getChildren() returns a type object Node, You should ask if the node is of the type TextField. You may ask:

  if(node instanceof TextField)

and then do the following;

 ((TextField)painelCriaGrelha.getChildren().get(fillLabel[1][1])).getText();

Browser other questions tagged

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