free web page hit counter
Tuesday Feb 07, 2006

Preventing user interaction during lengthy operation in netbeans

If your module has to do some lengthy operation, then avoid doing it in the AWT thread. This causes ugly repaint problem even though user may not be able to interact with the UI, as it appears hanging with out repainting. The best practice may be to fire another thread to do your lengthy operation, thus freeing the AWT thread so repaint could occur properly (If your operation itself needs to do any GUI operation, then invoke those opreations using SwingInvokeLater). But the problem now is, user can interact with the UI and may create other unpleasant events. Here is a tip on how avoid this.

Before starting the lengthy operation, get the glasspane of the netbeans main window and set it's visibility true. Set the cursor of the glasspane as Cursor.WAIT_CURSOR. Now start your operation in another thread. When the thread finishes, set the visibility of glasspane false and set back the cursor to normal. Put the operation between try .. catch .. finally block to make sure the glasspane is set invisible even if some unexpected exception occurs during the lengthy operation.

Here is a sample code

       final JFrame mainWin = (JFrame) WindowManager.getDefault().getMainWindow();

        // set status text 
        StatusDisplayer.getDefault().setStatusText("");
        RepaintManager.currentManager(mainWin).paintDirtyRegions();

        // set wait cursor to glasspane after making it visible
        mainWin.getGlassPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        mainWin.getGlassPane().setVisible(true);

        // Start you operation here
        Runnable myOperation = new Runnable() {
          public void run() {
            try {
               // Your operation here 
            }catch (Exception ex) {
               // Report error
            }finally{
              // clear status text
              StatusDisplayer.getDefault().setStatusText(""); // NOI18N
              // clear wait cursor
              mainWin.getGlassPane().setCursor(Cursor.getDefaultCursor());
              mainWin.getGlassPane().setVisible(false);
            }  
          }
        };
        new Thread(myOperation ).start();

When you make the glass pane visible, it is like a sheet of glass over all the other parts of the main window. Since it is completely transparent, user can still see the parts of main windows, but can not interact with them, until it's visibility is set to false.

Comments:

Wow. Cool. I want to try this; I've thought about this exact problem before. Do you have a plug-in, as a context for what you're saying here? (These are really the kind of tips we need.)

Posted by Geertjan on February 07, 2006 at 01:12 PM PST #

http://www.batteryfast.co.uk/hp/nc8230.htm hp nc8230 battery,
http://www.batteryfast.co.uk/hp/nc8200.htm hp nc8200 battery,
http://www.batteryfast.co.uk/hp/nw8200.htm hp nw8200 battery,

Posted by laptop batteries on October 24, 2008 at 12:28 AM PDT #

If you want to know how to use laptop battery well, You can see it from http://www.adapterlist.com/hp/nc8230.htm hp nc8230 battery,it's very useful .

Posted by laptop battery on February 16, 2009 at 01:34 AM PST #

If you want to know more about laptop bettery,you can see it from http://www.batterygoshop.co.uk/hp/nc8200-battery.htm hp nc8200 battery .

Posted by laptop bettery on February 17, 2009 at 05:08 PM PST #

If you want to know something about laptop betteries,you can see it from http://www.adapterlist.com/hp/nc8230.htm hp nc8230 battery ,It,s very cool.

Posted by laptop bettery on February 25, 2009 at 10:27 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed