如何获取网格窗格内的信息(javaFX)
How to obtain the information inside a gridpane (javaFX)
我有一个方法可以在 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());
}
}
}
我将组件存储在我创建的新 class 列表中。然后我遍历列表以检查节点是否存在于 (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;
}
}
这个class是存储一个节点到各自的位置
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;
}
此静态 class 接受列表、所需的行和所需的列。然后,如果在 (row,col) 处存在一个元素,它 returns 节点。
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());
产生"Click here"
我有一个方法可以在 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());
}
}
}
我将组件存储在我创建的新 class 列表中。然后我遍历列表以检查节点是否存在于 (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;
}
}
这个class是存储一个节点到各自的位置
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;
}
此静态 class 接受列表、所需的行和所需的列。然后,如果在 (row,col) 处存在一个元素,它 returns 节点。
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());
产生"Click here"