问题描述
我有一个鼠标侦听器,当我在应用程序窗口中移动鼠标时会被调用。 在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();
}
}
}
加载过程已按预期停止,但是对话框未绘制,这意味着它没有显示其组件。
非常感谢您的帮助。
非常感谢
致谢
解决方法
您无法在代码中的任何地方执行GUI事务,除非该代码在事件调度线程(EDT)中运行。您正在自己创建的线程上运行它;不允许。使用例如SwingUtilities.invokeLater
或SwingWorker系统。