Java GWT访问网格元素值
我已经使用com.google.gwt.user.client.ui.Grid;创建了一个网格:Java GWT访问网格元素值,java,gwt,Java,Gwt,我已经使用com.google.gwt.user.client.ui.Grid;创建了一个网格: Grid g = new Grid (5,5); 我通过sql查询向网格中添加了几个元素: g.setWidget(i, 0, texto(i, temp.get(1))); g.setWidget(i, 1, desplegable(temp.get(2))); g.setWidget(i, 2, texto(i, temp.get(3))
Grid g = new Grid (5,5);
我通过sql查询向网格中添加了几个元素:
g.setWidget(i, 0, texto(i, temp.get(1)));
g.setWidget(i, 1, desplegable(temp.get(2)));
g.setWidget(i, 2, texto(i, temp.get(3)));
g.setWidget(i, 3, texto(i, temp.get(4).replace("%%", "")));
g.setWidget(i, 4, gu);
“texto”和“desplegable”都是我创建的自定义文本框和列表框的方法;两者都有一个单击事件,通过该事件可以更改/修改初始值
“gu”是我在上面代码上面创建的一个按钮。“gu”有一个单击事件,它假装要做的是获取网格中包含的单元格元素的值
以下是一个例子:
ItemId ItemName
-----------------------
01 Peak [gu]
02 Paper [gu]
03 Pick [gu]
现在,当我单击“gu”时,根据我单击的“gu”,我想从网格中检索两个值(itemid和itemname)。我知道如何区分单击了哪个“gu”,但我不知道如何访问与“gu”按钮对齐的元素
但直到现在我还没有找到如何做到这一点 有人能告诉我这件事吗 提前感谢您抽出时间
亲切问候,您可以使用地图保留对每个按钮行索引的引用,例如:
Map<Button, Integer> buttonRowMap = new HashMap<Button, Integer>();
...
g.setWidget(i, 4, gu); // Your existing line
buttonRowMap.put(gu, i); // Keep a reference to the row index
我不明白你想干什么?但是,管理列表中的元素,或者通过索引从网格运行时获取元素,并循环遍历网格中的所有元素,然后单击“gu”按钮获取其值。嗨,David Levesque,感谢您提供的解决方案。我已经试过了,这正是我想要的。接受你的答案。干杯!:)
public void onClick(ClickEvent event) {
Button button = (Button) event.getSource();
Integer rowIndex = buttonRowMap.get(button);
TextBox tb = (TextBox) g.getWiget(rowIndex, 2);
...
}