如何获取gridpane(javaFX)中的信息 [英] How to obtain the information inside a gridpane (javaFX)

查看:910
本文介绍了如何获取gridpane(javaFX)中的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

这是代码:

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


推荐答案

我将组件存储在我创建的新类的List中。然后我遍历列表以检查节点是否存在于(row,col)。

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

此静态类接收列表,所需行和所需列。然后,如果在(row,col)处存在元素,则返回节点。

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

生成点击此处

这篇关于如何获取gridpane(javaFX)中的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆