【问题标题】:Swing wait() and notify() on JDialog, the dialog does not show its components在 JDialog 上摆动 wait() 和 notify(),对话框不显示其组件
【发布时间】:2020-12-04 13:31:05
【问题描述】:

我有一个鼠标侦听器,当我在应用程序窗口中移动鼠标时会调用它。 在 MouseEntered 方法中,我这样调用 ThreadB:

@Override
   public synchronized void mouseEntered(MouseEvent e) {

     ..

     ThreadB b = new ThreadB();
     b.start();
     System.out.println("Before Sync!!!!!!!");
     synchronized(b) {
         try{
         System.out.println("Waiting for b to complete...");
         b.wait();
     }catch(InterruptedException ex){
         ex.printStackTrace();
     }       
    }
    System.out.println("After Sync!!!!!!!");
}

class ThreadB extends Thread {
        
        @Override
        public void run(){
            synchronized(this){

                int dialogResult = JOptionPane.showConfirmDialog(null, "Loading process is in progress. Do you want to stop the process?", "Loading Accounts", JOptionPane.YES_NO_OPTION);
                
              
            if (dialogResult == 0) {
                System.out.println("Yes is chosen!!!!");
            }else {
                System.out.println("No is chosen!!!!");
            }

                System.out.println("before NOTIFY!!!!!!!");
            
                notify();
         }
      }
   }

加载过程按预期停止,但对话框没有被绘制,这意味着它不显示其组件。

非常感谢您的帮助。

非常感谢

问候

【问题讨论】:

    标签: java swing user-interface components jdialog


    【解决方案1】:

    您不能在代码中的任何位置执行 GUI 操作,除非该代码在事件调度线程 (EDT) 中运行。您在自己创建的线程上运行它;不允许。使用例如SwingUtilities.invokeLater,或 SwingWorker 系统。

    【讨论】:

    • 非常感谢您的及时回复。当我使用 invokeLater 时,该过程不会停止,但对话框显示正常。我需要先捕获用户的输入,然后对其进行解析,然后根据用户的输入继续该过程。
    猜你喜欢
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多