且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么JList选择发生两次?

更新时间:2023-02-05 19:22:14

更改选择时,根据实现的不同,可能会发生一个或两个事件.如果选择索引#4,然后单击第二项,则会发生以下情况:

When you change a selection, one or two events can occur, depending on the implementation. If index #4 is selected and you click on the second item, then the following occurs:

  • 首先,索引#4被取消选择.根据型号,questionaireNamesList.getSelectedIndex()可以合法地返回2或-1.
  • 秒,选择索引#2.此时,questionaireNamesList.getSelectedIndex()肯定会返回2.
  • First, index #4 is UNSELECTED. Depending on the model, questionaireNamesList.getSelectedIndex() can legally return either 2 or -1.
  • second, index #2 is SELECTED. At this point, questionaireNamesList.getSelectedIndex() will surely return 2.

因此,有两个事件被触发.有关如何生成这些事件的定义,为不同的JVM实现留出了余地,但情况确实有所不同.

Thus, there are two events fired. The definition of how these events are generated allows leeway for different JVM implementations do go things slightly differently.

注意:您可能应该检查 ListSelectionEvent#getValueIsAdjusting() 来查看您正在处理的事件是否是一系列事件中的一个.您可能需要忽略所有返回true的事件.

NOTE: You should probably check the value of ListSelectionEvent#getValueIsAdjusting() to see if the event you are processing is one in a series of events. You probably need to ignore all events where this returns true.