Good (not!), bad, ugly?

WaitCursor.java

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import sun.security.krb5.internal.crypto.e;

public class WaitCursor {
    public static void main(String[] args) {
        final JFrame    frame = new JFrame("Wait Cursor");

        JMenuBar        menuBar               = new JMenuBar();
        JMenu           fileMenu              = new JMenu("File");
        JMenuItem       launchLongRunningTask = new JMenuItem("Launch Long Running Task");

        launchLongRunningTask.addActionListener(new ActionListener() {
                public void actionPerformed(final ActionEvent e) {
                    // At least show wait cursor
                    SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                            }
                        });
                    SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                try {
                                    // simulated long running operation
                                    Thread.currentThread().sleep(10000);
                                } catch (InterruptedException e) {
                                    System.err.println(e.getMessage());
                                } finally {
                                    // reset the cursor to default
                                    frame.setCursor(Cursor.getDefaultCursor());
                                }
                            }
                        });
                }
            });
        
        fileMenu.add(launchLongRunningTask);
        menuBar.add(fileMenu);
        frame.setJMenuBar(menuBar);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(400, 400, 200, 100);
        frame.setVisible(true);
    }
}

Comments?

Comments:

Post a Comment:
Comments are closed for this entry.
About

sandipchitale

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today