JLists 中 JButton 侦听器事件的多个实例

发布于 2024-10-01 09:15:35 字数 315 浏览 0 评论 0原文

我创建了一个对话框,其中显示两个 JList 和一个按钮,该按钮从第二个 JList 中获取所选值并执行某些操作(假设它输出所选值)。第二个 JList 中的值列表根据对第一个 JList 中值的选择进行更新。

最初显示该对话框时,该按钮被禁用。当从第一个 JList 和第二个 JList 中选择一个值时,该按钮将被启用,并且所需的选择侦听器将添加到其中。

问题在于,每次单击按钮时,输出消息的数量等于选择第二个 JList 的值的时间。例如,如果我从第二个 JList 中选择一个值,然后我改变主意并选择另一个值,则单击按钮将输出该消息两次。 有谁知道防止这种事情发生的方法吗?

I have created a dialog with two JLists displayed and a button that takes the selected value from the second JList and does something (say it outputs the selected value). The list of values from the second JList is updated according to the selection of the value from the first JList.

When the dialog is displayed initially, the button is disabled. When a value is selected from the first and then from the second JList, the button is enabled an the required selection listener is added to it.

The problem is that every time the button is clicked the number of output messages is equal to the time a value of the second JList is selected. For example if I select a value from the second JList, then I change my mind and select another value, the click of the button will output the message two times.
Does anyone know a method to prevent such a thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

謸气贵蔟 2024-10-08 09:15:35

您的 ListSelectionListener 应检查 (e.getValueIsAdjusting() == false) 否则您将响应所有用户的选择,而不仅仅是最终的选择。

Your ListSelectionListener should check for (e.getValueIsAdjusting() == false) otherwise you'll respond to all of user's selections and not just the final one.

无风消散 2024-10-08 09:15:35

是的:不缓存选择,只需处理第二个列表中的实际选择。

如果可能,请发布按下按钮后执行的代码。我想,您有某种集合(列表或队列)来存储您在对话框中所做的所有选择,当您按下按钮时,每个存储的选择都会被处理。

这看起来像是有意为之的行为,因为您通常不会意外地编写此代码;)

如果这是有意为之并且您只是想消除重复项,请考虑使用 Set 而不是列表,作为 < code>Set 将仅包含唯一值。

Yes: don't cache the selections, just process the actual selection in your second list.

If possible, post the code that is executed once your button is pressed. I guess, you have some sort of collection (a list or queue) that stores all the selections you do on the dialog and when you press the button, each stored selection is processed.

This looks like an intended behaviour, because you usually don't code this by accident ;)

If it is intended and you just want to eliminate duplicates, consider using a Set instead of a list, as a Set will only contain unique values.

随风而去 2024-10-08 09:15:35

它完美地完成了它应该做的事情。

它触发两个事件,

1>;选择已从第一项中删除。

2>选择第二项。

正如 fbcocq 所说,您应该检查 getValueIsAdjusting()。检查这个,它会有所帮助。

It does perfectly what it should do.

It fires two events,

1> Selection is removed from first item.

2> Selection is done to second item.

So as fbcocq said, you should check for getValueIsAdjusting(). Check this out, it'll help.

南街女流氓 2024-10-08 09:15:35

每次启用按钮时是否都会向按钮添加一个 ActionListener?

Are you adding an ActionListener to the button every time you enable it?

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