Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在mouseExit事件上的特定毫秒数后取消选择jList中的项目_Java_Swing_Timer_Jlist_Listselectionlistener - Fatal编程技术网

Java 如何在mouseExit事件上的特定毫秒数后取消选择jList中的项目

Java 如何在mouseExit事件上的特定毫秒数后取消选择jList中的项目,java,swing,timer,jlist,listselectionlistener,Java,Swing,Timer,Jlist,Listselectionlistener,我有一个叫todoList的jList 当用户单击列表中的项目时,它将保持选中状态。但我希望列表中当前选定的项目在鼠标退出jList 400毫秒后自行取消选择 仅当列表中已选择某些内容时,此操作才能运行 我正在使用Netbeans IDE,这是迄今为止我们尝试过的: private void todoListMouseExited(java.awt.event.MouseEvent evt) { if (!todo

我有一个叫todoList的jList

当用户单击列表中的项目时,它将保持选中状态。但我希望列表中当前选定的项目在鼠标退出jList 400毫秒后自行取消选择

仅当列表中已选择某些内容时,此操作才能运行

我正在使用Netbeans IDE,这是迄今为止我们尝试过的:

private void todoListMouseExited(java.awt.event.MouseEvent evt) {                                     
    if (!todoList.isSelectionEmpty()) {
        Thread thread = new Thread();
        try {
            thread.wait(400L);
            todoList.clearSelection();
        } catch (InterruptedException ex) {
            System.out.println(ex);
        }
    }
}

这两者都会让一切停止运转

我的想法是,我需要创建一个等待400毫秒的新线程,然后运行jList的clearSelection方法。每次鼠标退出列表并仅在列表中有已选中的内容时运行时,都会发生这种情况

我希望我能充分地解释我的问题。

问题是Objectwait正在等待而不是Sleep得到通知,但这并没有发生。相反,超时会导致InterruptedException绕过对clearSelection的调用

不要在Swing应用程序中使用原始线程。取而代之的是使用一个设计用于与Swing组件交互的Swing计时器

if (!todoList.isSelectionEmpty()) {
   Timer timer = new Timer(400, new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
         todoList.clearSelection();
      }
   });
   timer.setRepeats(false);
   timer.start();
}
问题是Objectwait正在等待而不是休眠以获得通知,但这并没有发生。相反,超时会导致InterruptedException绕过对clearSelection的调用

不要在Swing应用程序中使用原始线程。取而代之的是使用一个设计用于与Swing组件交互的Swing计时器

if (!todoList.isSelectionEmpty()) {
   Timer timer = new Timer(400, new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
         todoList.clearSelection();
      }
   });
   timer.setRepeats(false);
   timer.start();
}

问题是您正在阻止AWT事件线程

解决方案是使用:


问题是您正在阻止AWT事件线程

解决方案是使用:


注意,从我这里复制的代码不起作用,我忘了添加.start和其他一些东西;非常感谢。修复编译错误,请参阅修订历史记录,以确保它是正确的+1编辑后。我用过这个,它可以工作,但并不完美。当我单击列表中的项目并且鼠标退出列表时,该项目会按其应该的方式取消选择,但当我再次尝试单击该项目时,它甚至不会选择。我认为这是因为计时器在400毫秒后没有停止,所以ActionListener会出错。我使用了System.out.printlne;在public void中,actionPerformed和输出是java.awt.event.ActionEvent[unknown type,cmd=null,when=1365513320800,modifiers=]on javax.swing。Timer@aba647. 你能再帮我一次吗?我不能投票,因为我需要15分:否则我就得@莱默斯,对不起,我只有最后一个问题。当鼠标进入列表时,有没有办法停止计时器?如果鼠标在列表中,我不想取消选择项目。请注意,从我复制的代码不起作用,我忘记添加。开始和其他一些东西;非常感谢。修复编译错误,请参阅修订历史记录,以确保它是正确的+1编辑后。我用过这个,它可以工作,但并不完美。当我单击列表中的项目并且鼠标退出列表时,该项目会按其应该的方式取消选择,但当我再次尝试单击该项目时,它甚至不会选择。我认为这是因为计时器在400毫秒后没有停止,所以ActionListener会出错。我使用了System.out.printlne;在public void中,actionPerformed和输出是java.awt.event.ActionEvent[unknown type,cmd=null,when=1365513320800,modifiers=]on javax.swing。Timer@aba647. 你能再帮我一次吗?我不能投票,因为我需要15分:否则我就得@莱默斯,对不起,我只有最后一个问题。当鼠标进入列表时,有没有办法停止计时器?如果鼠标在列表中,我不想取消选择该项。
private void todoListMouseExited(java.awt.event.MouseEvent evt) 
{
   if (!todoList.isSelectionEmpty()) {
        new Timer(400, new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                  todoList.clearSelection();
              }
        }).start();
    }
}