2015-06-11 19 views
0
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);} 
}} 

等待窗口類如下的SwingWorker調用JFrame類..window顯示什麼

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連接也沒有錯誤。

+0

您阻止事件指派線程 – MadProgrammer

+0

可以看到Concurrency in SwingWorker Threads and SwingWorker你請,如果可以的話,更具體地解釋我? –

回答

1

您阻止事件Dispathing線程,與呼叫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是一個單線程的框架,而不是線程安全。這意味着阻塞Event Dispatching Thread的任何東西都將阻止它處理新的事件,包括繪畫請求。

Swing組件應該也只能從EDT,這就是SwingWorker進來的範圍內進行更新。

更多細節

+0

謝謝MadProgrammer確實有用! –