java - SwingWorker 调用 JFrame 类 ..window 不显示任何内容

标签 java swing jframe swingworker event-dispatch-thread

class class1{
    public class1(){//here is my GUI commants}

@Override  
     public void actionPerformed(ActionEvent evt)   //this is my action performed from a jframe window      
 {  

    worker = new SwingWorker<Void, Void>(){//ia m creating a worker
    protected  WaitWindow waitWindow;
    @Override
    protected Void doInBackground() throws Exception {
         waitWindow= new  WaitWindow();//i call waitWindow class to pop up my new window with the progressBar
         return null;
    }
    @Override
    protected void done(){
        waitWindow.CloseWaitWindow();
    }
};
try{  
    String option = (String)serversList.getSelectedItem();

      if (evt.getSource().equals(Button1))//when client presses button1
       {                                                      
         if(option.equals("icsd Server"))
           {//here is my connection
            Registry registry = LocateRegistry.getRegistry("localhost",1080);
            icsdserver = (ICSDinterface)registry.lookup("RmiCheckICSD");
            worker.execute(); //i am calling execute until the server return 0 this might take a long time 
            if (icsdserver.RequestForEntry("icsd",0)==0)
                {
                 worker.cancel(true); //when server tell its all ok (with 0) i call cancel(true)
                 AddGrade d = new AddGrade(icsdserver,"icsd");
                }
            }
       }
    }
catch (RemoteException ex) {System.out.println(ex);}
catch (NotBoundException ex) {System.out.println(ex);}
}}

等待窗口类如下

class  WaitWindow extends JFrame //my WaitWindow Class
{

   private JProgressBar bar ;

   public WaitWindow(){

   super("Wait Until Connection Is ready");
   setSize(100,200);
   bar = new JProgressBar();
   bar.setIndeterminate(true);
   bar.setPreferredSize(new Dimension(300,330));
   add(bar);
   getContentPane();
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setVisible(true);
}


public void CloseWaitWindow()
            {
            removeNotify();
             }


        }

我在这里做错了什么?我希望显示等待窗口,直到服务器的 RequestForEntry 方法返回 0,这可能需要一些时间。 RMI 连接也没有错误。

最佳答案

您正在通过调用 RequestForEntry 来阻止事件调度线程,该调用应位于 SwingWorkerdoInBackground 方法内,例如

public void actionPerformed(ActionEvent ev) //this is my action performed from a jframe window      
{

    try {
        final String option = (String) serversList.getSelectedItem();

        if (evt.getSource().equals(Button1))//when client presses button1
        {
            final WaitWindow waitWindow = new WaitWindow();
            worker = new SwingWorker<Void, Void>() {//ia m creating a worker

                @Override
                protected Void doInBackground() throws Exception {
                    if (option.equals("icsd Server")) {//here is my connection
                        Registry registry = LocateRegistry.getRegistry("localhost", 1080);
                        icsdserver = (ICSDinterface) registry.lookup("RmiCheckICSD");
                        worker.execute(); //i am calling execute until the server return 0 this might take a long time 
                        if (icsdserver.RequestForEntry("icsd", 0) == 0) {
                            worker.cancel(true); //when server tell its all ok (with 0) i call cancel(true)
                            AddGrade d = new AddGrade(icsdserver, "icsd");
                        }
                    }
                    return null;
                }

                @Override
                protected void done() {
                    waitWindow.CloseWaitWindow();
                }
            };
        }
    } catch (RemoteException ex) {
        System.out.println(ex);
    } catch (NotBoundException ex) {
        System.out.println(ex);
    }
}

Swing 是一个单线程框架,并且不是线程安全的。这意味着任何阻塞事件调度线程的事情都会阻止它处理新事件,包括绘制请求。

Swing 组件也应该只在 EDT 上下文中更新,这就是 SwingWorker 的用武之地。

参见Concurrency in SwingWorker Threads and SwingWorker了解更多详情

关于java - SwingWorker 调用 JFrame 类 ..window 不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30779339/

相关文章:

java - 没有任务栏图标的 JFrame 弹出窗口

Java对象添加/更新/删除算法

java - QueueMessageHandler 的用途是什么

Java Swing 设计 : static vs instance

java - For循环导致JFrame(Eclipse)黑屏

java - 无法导入 JFrame 或 Dimension

java - 如何将 4GB 文件上传到 alfresco?

java - 用 javap 反汇编的枚举不显示构造函数参数

java - JFrame 找不到主类

java - 更改文本框颜色 - 应用程序。很冷