Swing中的wait()和notify()在JDialog上,对话框不显示其组件。

huangapple 未分类评论78阅读模式
英文:

Swing wait() and notify() on JDialog, the dialog does not show its components

问题

我有一个鼠标监听器,当我在应用程序窗口中移动鼠标时会被调用。
在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, "加载过程正在进行中。您想要停止该过程吗?", "加载账户", JOptionPane.YES_NO_OPTION);
        	
		  
      	if (dialogResult == 0) {
       		System.out.println("选择了是!!!");
       	}else {
       		System.out.println("选择了否!!!");
       	}

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

预期中加载过程已停止,但对话框未被绘制,意味着它未显示其组件。

非常感谢您的帮助。

谢谢
祝好

英文:

I have a mouse listener that gets invoked when I move the mouse in the application window.
In the MouseEntered method, I invoke a ThreadB as such:

@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();
         }
      }
   }

The loading process is stopped as expected, but the dialog does not gets painted meaning it does not show its components.

Your help is much appreciated.

Thanks a lot

Regards

答案1

得分: 0

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

英文:

You can't do GUI things anywhere in your code, except if that code runs in the Event Dispatch Thread (the EDT). You're running it on a thread of your own creation; not allowed. Use e.g. SwingUtilities.invokeLater, or the SwingWorker system.

huangapple
  • 本文由 发表于 2020年8月15日 00:21:44
  • 转载请务必保留本文链接:https://java.coder-hub.com/63416521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定