How to obtain the information inside a gridpane (javaFX)

蹲街弑〆低调 提交于 2019-12-11 10:33:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!