Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.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失去焦点事件处理程序在执行另一个事件处理程序之前完成其工作_Java_Swing - Fatal编程技术网

如何确保java失去焦点事件处理程序在执行另一个事件处理程序之前完成其工作

如何确保java失去焦点事件处理程序在执行另一个事件处理程序之前完成其工作,java,swing,Java,Swing,我正在将语音识别组件与现有应用程序集成。在我的应用程序中,我有一个JTree和JTextPane。我已经为jtextpane编写了失焦事件处理程序,为JTree编写了选择事件处理程序。当焦点在jtextpane中,然后在jtree中选择一个节点时,我可以看到在焦点丢失事件处理程序之前,选择事件处理程序被执行。我在focus lost事件处理程序中编写了ThreadPoolExecutor,并向ThreadPoolExecutor添加了三个作业,我必须确保在执行选择处理程序之前,我应该完成Thra

我正在将语音识别组件与现有应用程序集成。在我的应用程序中,我有一个JTree和JTextPane。我已经为jtextpane编写了失焦事件处理程序,为JTree编写了选择事件处理程序。当焦点在jtextpane中,然后在jtree中选择一个节点时,我可以看到在焦点丢失事件处理程序之前,选择事件处理程序被执行。我在focus lost事件处理程序中编写了ThreadPoolExecutor,并向ThreadPoolExecutor添加了三个作业,我必须确保在执行选择处理程序之前,我应该完成ThradPoolExecutor中的所有作业;为此,我使用了倒计时锁。我读过许多类似的文章,但我找不到如何确保在执行选择事件处理程序之前完成焦点丢失。一些帖子要求在invokeLater方法中运行选择事件处理程序代码。但这并不能解决我的问题,因为ThreadPoolExecutor在另一个线程中执行其代码。下面我记下了我想要执行代码的顺序

  • 将作业添加到焦点丢失事件处理程序中的ThreadPoolExecutor
  • 在失去焦点事件中,阻止UI线程并完成向ThreadPoolExecutor添加的所有作业
  • 执行选择事件处理程序
  • 下面是我的焦点处理程序的编写代码

    public void focusLost(FocusEvent e)
      {
    
        if (ecaControl.isInitialized())
        {
    
          ecaControl.addStopRecordingJobToExecutor();
    
          ecaControl.addSaveJobToExecutor(this.currentMetaFileName != null ? this.currentMetaFileName : this.getName());
    
          ecaControl.addCloseJobToExecutor();
    
          co = new CountDownLatch(ecaControl.getExecutor().counter);
          ecaControl.getExecutor().setCountdownla(co);
    
          try
          {
            System.out.println("------- count before" + ecaControl.getExecutor().counter);
            co.await();
    
            ecaControl.getExecutor().setCountdownla(null);
          }
          catch (InterruptedException e1)
          {
            // TODO Auto-generated catch block
            e1.printStackTrace();
          }
          System.out.println("--------count after" + ecaControl.getExecutor().counter);
          try
          {
            System.out.println("The text is "
                + this.getStyledDocument().getText(0, getStyledDocument().getLength()));
          }
          catch (BadLocationException e1)
          {
            // TODO Auto-generated catch block
            e1.printStackTrace();
          }
    
        }
    
      }
    

    如果选择事件仅与
    focusLost()
    事件同时发生,则应调用
    ThreadPoolExecutor
    ,然后在选择事件中调用
    invokeLater()
    方法。如果不是,那么最好的解决方案可能是事件调度器。如果将所有事件发送给事件调度器,它可以协调
    JTree
    是否需要等待,执行任何必要的线程,然后在适当的时间将事件重新分派给适当的组件