GWT:访问面板内的列表框

发布于 2024-11-29 01:57:23 字数 478 浏览 0 评论 0原文

我有一个面板,其中包含一堆(多选)列表框,每个列表框都在自己的面板内,我需要弄清楚列表框中选定的值是什么。通过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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

音盲 2024-12-06 01:57:23

成功了。显然,除了您自己添加的小部件之外,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

拥醉 2024-12-06 01:57:23

为什么不能将它们声明为实例变量?

Why can't you just declare them as instance variables?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文