【问题标题】:How to obtain the information inside a gridpane (javaFX)如何获取网格窗格内的信息(javaFX)
【发布时间】:2016-02-22 06:08:10
【问题描述】:

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

代码如下:

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

【问题讨论】:

  • 你用西班牙语写代码让我发笑。我以前从未见过。

标签: java indexing javafx gridpane


【解决方案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());

产生“点击这里”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-09
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 2014-09-17
    • 2017-06-23
    • 1970-01-01
    相关资源
    最近更新 更多