GWT:访问面板内的列表框
我有一个面板,其中包含一堆(多选)列表框,每个列表框都在自己的面板内,我需要弄清楚列表框中选定的值是什么。通过API,我能看到执行此操作的唯一方法是(伪代码):
for (Wigdet w : outerPanel)
Panel innerPanel = (Panel) w;
for (Widget s : innerPanel) // only has the ListBox in it
ListBox box = (ListBox) s;
// do stuff with the ListBox to populate the list of selected options
问题在于转换 - eclipse 不会抱怨并且编译得很好,但是当运行时它会产生 ClassCastException (在第一次转换时 - 我假设第二个也会有同样的问题,但由于我无法做到这一点,所以我不能肯定地说)
执行此操作的正确方法是什么?
I've got a panel, which contains a bunch of (multiselect) ListBoxes each inside their own panel, and I need to figure out what the selected values in the ListBoxes are. Going through the API, the only way I can see of doing this is (pseudocode):
for (Wigdet w : outerPanel)
Panel innerPanel = (Panel) w;
for (Widget s : innerPanel) // only has the ListBox in it
ListBox box = (ListBox) s;
// do stuff with the ListBox to populate the list of selected options
The trouble is with the casting - eclipse doesn't complain and it compiles fine, but when run it produces a ClassCastException (on the first cast - I assume it would also have the same problem on the second, but since I can't get to it I can't say for sure)
What is the correct way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
成功了。显然,除了您自己添加的小部件之外,GWT 还将自己的小部件插入到您的面板中(因此创建一个面板然后向其中添加一个列表框将导致迭代时不仅仅是一个小部件)。
因此,要解决这个问题,请在转换之前添加一个instanceof检查
Got it working. Apparently GWT inserts its own widgets into your panel, besides the ones you add yourself (so creating a Panel then adding a ListBox to in will result in more than just the one widget on iteration).
So to solve the problem, throw in an instanceof check before the casting
为什么不能将它们声明为实例变量?
Why can't you just declare them as instance variables?