> Please tell me how to solve this since it is a major usability issue.
Here it is:
import javax.swing.*;
import java.awt.* ;
import java.awt.event.*;
class example extends JFrame
{
example ()
{
super ( "Wait Cursor Problem" ) ;
final JButton btnShowDialog = new JButton ( "Show Dialog" ) ;
btnShowDialog.addActionListener ( new ActionListener ()
{
public void actionPerformed ( ActionEvent e )
{
new exampleDialog ( example.this ) ;
}
}
);
getContentPane().setLayout ( new FlowLayout () );
getContentPane( ).add ( btnShowDialog ) ;
setBounds ( 0 , 0 , 600 , 400 ) ;
setDefaultCloseOperation ( WindowConstants.EXIT_ON_CLOSE );
setVisible ( true ) ;
}
public void showWaitCursor ( boolean bShow )
{
this.getGlassPane().addMouseListener( new MouseAdapter() {});
this.getGlassPane().addKeyListener ( new KeyAdapter() {});
this.getGlassPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
//this.setCursor ( Cursor.getPredefinedCursor ( Cursor.WAIT_CURSOR )
);
this.getGlassPane().setVisible ( bShow ) ;
}
public static void main ( String[] args )
{
new example () ;
}
}
final class exampleDialog extends JDialog
{
private example mainframe = null ;
exampleDialog ( example parent )
{
super ( ( Frame ) parent , "Long-running Task" , true ) ;
this.mainframe = parent ;
final JButton btnStartTask = new JButton ( "Start Task" ) ;
btnStartTask.addActionListener ( new ActionListener ()
{
public void actionPerformed ( ActionEvent e )
{
longTask ( ) ;
}
}
);
getContentPane().setLayout ( new FlowLayout () );
getContentPane().add ( btnStartTask ) ;
pack ( ) ;
setSize ( 300 , 200 ) ;
setDefaultCloseOperation ( WindowConstants.DISPOSE_ON_CLOSE ) ;
setVisible ( true ) ;
}
private void longTask ()
{
mainframe.showWaitCursor ( true ) ;
try
{
Thread.sleep ( 5000 ) ;
}
catch ( InterruptedException iexp )
{
}
mainframe.showWaitCursor ( false ) ;
}
}
Hope this helps.
Best regards.
> > Please tell me how to solve this since it is a major usability issue.
>
> Please post a runnable example of the problem.
John McGrath - 30 Mar 2005 06:33 GMT
> final JButton btnStartTask = new JButton ( "Start Task" ) ;
> btnStartTask.addActionListener ( new ActionListener ()
[quoted text clipped - 5 lines]
> }
> );
You are performing the long task in the event thread, which is a problem.
You need to run it in a separate thread. Here is some reading that should
help: http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html

Signature
Regards,
John McGrath