2015-11-19 26 views
1

我有一个方法可以在gridPane(2x8)中创建新的TextField。我想知道,一旦创建它们,我如何访问每个索引中的信息(如:0,0 - 1,0等)。如何获取gridpane内部的信息(javaFX)

下面的代码:

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); 
     } 

这里就是我想要做的事:

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()); 
     } 
    } 
} 
+0

它让我窃喜,你在西班牙语编写代码。我以前从来没有见过。 – Steven

回答

1

我存储的组件在我创建了一个新的类的列表。然后我遍历列表来检查一个节点是否存在于(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; 
    } 
} 

这个类是存储节点到各自的位置。

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; 
} 

这个静态类需要列表,期望的行和期望的列。然后,如果在(row,col)存在一个元素,它将返回该节点。

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()); 

生产的“点击这里”