package wait;
/*
* WaitCursorDemo.java
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.SwingWorker;
import test.*;
public class WaitCursorDemo extends JFrame {
private SwingWorker worker ;
private JButton btStart;
private JLabel lb;
public WaitCursorDemo() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(400,300);
setLocationRelativeTo(null);
btStart = new JButton("Start");
add(btStart, BorderLayout.NORTH);
lb = new JLabel();
add(lb);
btStart.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
start();
}
});
}
private void start(){
worker = new SwingWorker() {
protected Object doInBackground() throws Exception {
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
lb.setText("Please wait...");
try{Thread.sleep(3000);}catch(InterruptedException ex){}
setCursor(Cursor.getDefaultCursor());
lb.setText("Done");
return null;
}
};
worker.execute();
}
public static void main(final String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new WaitCursorDemo().setVisible(true);
}
});
}
/*
If you don't have JDK6, you need to download and compile the "SwingWorker" source code
to be able to use it. Here it is:
http://java.sun.com/products/jfc/tsc/articles/threads/src/SwingWorker.java
*
The API is a little different:
*
worker = new SwingWorker() {
public Object construct() {
setCursor(new Cursor(Cursor.WAIT_CURSOR));
lb.setText("Please wait...");
try{Thread.sleep(3000);}catch(InterruptedException ex){}
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
lb.setText("Done");
return null;
}
};
worker.start();
*/
}
Message was edited by:
Andre_Uhres
Message was edited by:
Andre_Uhres