SwingWorker calls a JFrame Class ..window shows nothing

131 views Asked by At
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);}
}}

The Wait Window class follows

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


        }

What am I doing wrong here? I want the wait Window to shown until server's RequestForEntry method return 0 this might take some time. Also there is no error with RMI connection.

1

There are 1 answers

1
MadProgrammer On

You're blocking the Event Dispathing Thread, with the call to RequestForEntry, which should be within the doInBackground method of the SwingWorker, for example

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 is a single threaded framework and isn't thread safe. This means that anything the blocks the Event Dispatching Thread will prevent it from processing new events, including paint requests.

Swing components should also only be updated from within the context of the EDT, which is where SwingWorker comes in.

See Concurrency in Swing and Worker Threads and SwingWorker for more details