Sets the cursor to hourglass. - Java Swing

Java examples for Swing:Cursor

Description

Sets the cursor to hourglass.

Demo Code

/*L/*  www.j ava2  s  .c  o  m*/
 * Copyright SAIC, SAIC-Frederick.
 *
 * Distributed under the OSI-approved BSD 3-Clause License.
 * See http://ncip.github.com/caadapter/LICENSE.txt for details.
 */
//package com.java2s;
import javax.swing.*;
import java.awt.*;

public class Main {
    /**
     * Sets the cursor to hourglass.  Should be called BEFORE time-intensive task has started.
     * Disables the mouse from clicking on anything in the current window.
     *
     * @param cont the current Window, Panel, Dialog, or other JComponent;
     *             just pass 'this', as long as it's a Container
     */
    public static void setCursorWaiting(Container cont) {
        if (cont == null) {
            System.err
                    .println("setCursorWaiting(Container cont): Container argument cannot be null. Will return and do nothing.");
            return;
        }

        Component glasspane = null;

        if (cont instanceof RootPaneContainer) {
            glasspane = ((RootPaneContainer) cont).getGlassPane();
        } else if (cont instanceof JComponent) {
            glasspane = ((JComponent) cont).getRootPane().getGlassPane();
        }
        glasspane.setVisible(true);
        Window window = SwingUtilities.windowForComponent(glasspane);
        window.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    }
}

Related Tutorials