在不触发侦听器的情况下清除JList

在不触发侦听器的情况下清除JList

本文介绍了Java Swing:在不触发侦听器的情况下清除JList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况如下:我有一个JList,只要在列表中进行选择,它就会触发搜索(使用ListSelectionListener).我正在尝试使用list.clearSelection()重设列表上的选择.问题是使用clearSelection()会触发侦听器,该侦听器将调用搜索:不好.

My situation is as follows: I've got a JList which triggers a search (using a ListSelectionListener) whenever a selection is made in the list. I'm trying to reset the selection on the list, using list.clearSelection(). The problem with this is that using clearSelection() triggers the listener, which calls the search: not good.

是否可以在不触发侦听器的情况下清除列表中的选择?这是否暗示我没有正确使用侦听器?但总的来说,您可以在Swing UI中更改某些内容而不触发与该元素相关的事件吗?

Is there a way of clearing the selection on the list without having the listener triggered? Could this be a hint that I'm not using the listener properly? In general though, can you change something in a Swing UI without triggering events associated with that element?

谢谢.

推荐答案

阅读如何为有效的演示编写列表选择监听器.

通常会生成两个事件,一个事件用于取消选择,另一个事件用于选择.被选中的东西时,你只应做搜索.您还应该能够检查选定的索引,该索引应该不是-1.

Normally two events are generated, one for the deselection and one for the selection. You should only be doing your search when something is selected. You should also be able to check selected index which should be something other than -1 I believe.

这篇关于Java Swing:在不触发侦听器的情况下清除JList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 15:12