views:

195

answers:

2

Hi all, I have a client-server application and i am using swing in the client side. My swing client has one main window (jframe) and lots of panels, toolbars and menubar in it. I want to remove all client action/mouse events (or simply grab and do nothing) while client is waiting response from server by means of glasssPane. Here is the code i wrote:

private final static MouseAdapter mouseAdapter = new MouseAdapter() 

{ public void mouseClicked(MouseEvent e) { System.out.println("MouseClicked..!"); } }; private static Cursor WAIT_CURSOR = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR); private static Cursor DEFAULT_CURSOR = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);

and

public static void startWaitCursor(JComponent comp)

{ MainWindow root = ((MainWindow) comp.getTopLevelAncestor());

root.getGlassPane().setCursor(WAIT_CURSOR); root.getGlassPane().addMouseListener(mouseAdapter); root.getGlassPane().setVisible(true); }

public static void stopWaitCursor(JComponent comp) {
MainWindow root = ((MainWindow) comp.getTopLevelAncestor());

root.getGlassPane().setCursor(DEFAULT_CURSOR); root.getGlassPane().setVisible(false); }

but i am not able to manage the grab mouse events. Changing cursors at the glassPane is working fine but either i am not able to add mouseAdapter or am not able to make glasssPane become to the top level component.

Any idea?

Thanks.

A: 

I realized that my code is working but my problem is threading related. My code was something like:

startWaitCursor(); 
work(); // server request that takes time 
stopWaitCursor();

and changed it to:

startWaitCursor(); 
SwingUtilities.invokeLater(new Runnable() {
poblic void run() { 
try 
{ 
work(); // server request 
} 
finally 
{ 
stopWaitCursor(); 
}

by doing this modification i could see the settings i made in the startWaitCursor() method while client is waiting response from the server.

But stil there is a small problem. In startWaitCursor() method i desabled key, mouse and focus events for the glass pane but events are still captured by main frame even glassPane is displayed.

addMouseListener(new MouseAdapter() {});
addMouseMotionListener(new MouseMotionAdapter() {});
addKeyListener(this);
setFocusTraversalKeysEnabled(false);

After server response reached to client and stopWaitCursor() method is invoked the events handled in the main frame.

If i disable the main frame of my application while client is waiting than cursor is not being changed to wait_cursor, if i am not disable the main frame then cursor is being changed but the events are queued.

cheers...

Deger
A: 

After digging swing threads issues couple of days, i finally found the real answer: SwingWorker

Now my final code is something like,

startWaitCursor();
SwingWorker worker = new SwingWorker() {
   public Object doInBackground() 
   {
      doWork(); // time consuming server request
      return null;
   }
   public void done() 
   {
      stopWaitCursor();
   }
};
worker.execute();

In startWaitCursor() method i set the glasspane visible (with alpha valued background), display a message to warn the user time consuming job is doing, set the cursor to wait_cursor (hourglass) and consume all the key, mouse events. That is it.

And by using SwingWorker my client is actually responsive (it is working as if no server request is made) but since i display the glasspane and consume all key and mouse events it feels like irresponsive.

What a relief.. SwingWorker rocks...

cheers..

Deger