I've been developing a master's work in javafx: something that gets an image, places a grid with textFields on all cells above the image.
The user can then put values in textFields. The grid is automatically created, depending on the number of rows and columns the user wants.
This is my view class controller:
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 function. At the end of the user filling in the cells and clicking the "defineArea" button was supposed to be able to save the values of the grid in an Array. I tried to convert to Int, but I could not.
I can not access the content of the gridPane's textField, maybe I'm not building my function correctly, but I think I'm thinking well, when I have access to the line:
painelCriaGrelha.getChildren().get(fillLabel[1][1]);