问题
I have a method that creates new TextFields in a gridPane (2x8). I was wondering, once they are created, how do I access the information within each index (as in: 0,0 - 1,0, etc).
Here's the code:
private void llenarGridJugadores(ArrayList<NodoJugadores> array)
{
if( (array.get(0).getCategoria() == 3) && (array.get(0).getSexo().equalsIgnoreCase("m")) )
{
for(int i = 0 ; i < array.size() ; i++)
{
TextField text = new TextField(array.get(i).getNombre());
grid.add(text, i, 0);
}
}
else if( (array.get(0).getCategoria() == 3) && (array.get(0).getSexo().equalsIgnoreCase("f")) )
{
for(int i = 0 ; i < array.size() ; i++)
{
TextField text = new TextField(array.get(i).getNombre());
grid.add(text, 1, i);
}
And here's what I'm trying to do:
public ArrayList<NodoJugadores> retornarGridJugadores(ArrayList<NodoJugadores> array, NodoCategorias aux)
{
if( (aux.getNumCategoria() == 3) && (aux.getSexo().equalsIgnoreCase("m")) )
{
for(int i = 0 ; i < array.size() ; i++)
{
array.get(i).setNombre(grid.getChildren().get(i).getAccessibleText());
}
}
}
回答1:
I stored the components in a List of a new class I created. Then I iterated through the list to check if a node existed at (row,col).
public class Triple{
Node n;
int row;
int col;
public Triple(Node n,int row, int col){
this.row = row;
this.col = col;
this.n = n;
}
}
This class is to store a node to respective position.
public static Node getItem(List <Triple> l, int findRow, int findCol){
for(int i=0; i < l.size(); i++){
if(l.get(i).row == findRow && l.get(i).col == findCol){
return l.get(i).n;
}
}
return null;
}
This static class takes in the list, desired row and desired column. Then, if exists an element at (row,col), it returns the node.
public static void testThis(){
List <Triple> list = new ArrayList<>();
GridPane gp = new GridPane();
Text a1 = new Text("Click here");
Text a2 = new Text("Click there");
gp.add(a1, 5, 5);
gp.add(a2, 5, 5);
for(int i=0; i<gp.getChildren().size; i++){
list.add(new Triple(gp.getChildren().get(i),
GridPane.getRowIndex(gp.getChildren().get(i)), GridPane.getColumnIndex(gp.getChildren().get(i))));
}
Text tempText = (Text) getItem (list, 5, 5);
System.out.println(tempText.getText());
Produces "Click here"
来源:https://stackoverflow.com/questions/33815075/how-to-obtain-the-information-inside-a-gridpane-javafx