为什么 JList 选择发生两次?

发布于 2024-07-15 06:07:04 字数 1286 浏览 4 评论 0原文

我有一个包含一些项目的 JList。 我添加了一个监听器,用于监听列表中的某个项目被选中的情况。 以下是选择列表中的项目时发生的情况的代码:

private void questionaireNamesListValueChanged(ListSelectionEvent evt) {
    try {
        inputPanel.setEnabled(false);
        inputPanel.setVisible(false);
        inputTextField.setText("");
        inputStatusLabel.setText("");
        int questionaireIndex = questionaireNamesList.getSelectedIndex();

        // Why will this be printed twice?
        System.out.println("Questionaire Index: " + questionaireIndex);

        if (remoteQuestionServer.getQuestionCount(questionaireIndex) == 5) {
            answerQuestionButton.setEnabled(true);
            addQuestionButton.setEnabled(false);
        } else {
            addQuestionButton.setEnabled(true);
            answerQuestionButton.setEnabled(false);
        }
    } catch (RemoteException ex) {
        ex.printStackTrace();
    }
} 

正如上面所示,我放入了一个 System.out.print 语句,每次单击列表中的某个项目时,我都会得到两个该项目的输出,例如。

Questionaire Index: 4
Questionaire Index: 4
Questionaire Index: 2
Questionaire Index: 2
Questionaire Index: 0
Questionaire Index: 0
Questionaire Index: 2
Questionaire Index: 2

知道为什么会发生这种情况吗?

谢谢, 帕特里克

I have a JList with some items. I've added a listener for when a item in the list is selected. Here is the code for what happens when an item in the list is selected:

private void questionaireNamesListValueChanged(ListSelectionEvent evt) {
    try {
        inputPanel.setEnabled(false);
        inputPanel.setVisible(false);
        inputTextField.setText("");
        inputStatusLabel.setText("");
        int questionaireIndex = questionaireNamesList.getSelectedIndex();

        // Why will this be printed twice?
        System.out.println("Questionaire Index: " + questionaireIndex);

        if (remoteQuestionServer.getQuestionCount(questionaireIndex) == 5) {
            answerQuestionButton.setEnabled(true);
            addQuestionButton.setEnabled(false);
        } else {
            addQuestionButton.setEnabled(true);
            answerQuestionButton.setEnabled(false);
        }
    } catch (RemoteException ex) {
        ex.printStackTrace();
    }
} 

As you can above I put a System.out.print statement in and every time I click on something in the list I get two ouputs for that item, eg.

Questionaire Index: 4
Questionaire Index: 4
Questionaire Index: 2
Questionaire Index: 2
Questionaire Index: 0
Questionaire Index: 0
Questionaire Index: 2
Questionaire Index: 2

Any idea why this is happening?

Thanks,
Patrick

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

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

发布评论

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

评论(2

动听の歌 2024-07-22 06:07:04

当您更改选择时,可能会发生一个或两个事件,具体取决于实施情况。 如果选择索引 #4 并单击第二个项目,则会发生以下情况:

  • 首先,索引 #4 未被选择。 根据模型的不同,questionaireNamesList.getSelectedIndex() 可以合法返回 2 或 -1。
  • 其次,选择索引#2。 此时,questionaireNamesList.getSelectedIndex() 肯定会返回 2。

因此,会触发两个事件。 这些事件如何生成的定义为不同的 JVM 实现提供了余地,但事情确实略有不同。

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

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:

  • 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.

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.

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.

凶凌 2024-07-22 06:07:04

进一步了解 Eddie 的答案,查看事件的 getValueIsAdjusting 方法。

Further to the answer by Eddie look at the getValueIsAdjusting method on the event.

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