Justin Lee <[EMAIL PROTECTED]> writes:

> We are trying to set the mouse cursor to the wait cursor whenever we
> open a dialog box.  We have method that takes in a java.awt.Component
> and grabs all of its contained Components and sets the cursor on it.
> *

A better way to do this is to set the cursor for the actual Frame.

Enclosed are two handy (and classical) methods to set the cursor for a
Frame, and a test program. The first method sets the cursor, the
second finds the first frame in the component hierarchy, searching
upwards from the component. The second method is also handy whenever
one have to produce a frame reference. For example in Swing's
JOptionPane various Dialog methods.

  /**
   * Set mouse cursor for first frame in the component hierarchy.
   *
   * @param comp - Component, usualy this
   * @param type - Cursor type (e.g. Cursor.WAIT_CURSOR)
   */
  public static void setCursor(Component comp, int type) 
  {
    ((Component)getFrame(comp)).
        setCursor(Cursor.getPredefinedCursor(type));
  } 

  /**
   * Get the first frame in the component hierarchy, searching
   * upwards from comp.
   *
   * @param comp - A component
   * @return First frame in the object hierarchy from comp
   */
  public static Frame getFrame(Component comp)
  {
    Component component= comp;
    while (component != null && !(component instanceof Frame)) 
        component= component.getParent();
    return ((Frame)(component));
  }

-----------------------------------------------------------------------

/*
 * Mouse Cursor Test
 */

import jam.util.JaMUtil; // My placeholder for utilitys e.g. setCursor
import java.awt.*;
import java.awt.event.*;

class t 
{
  public static void main(String[] args){ new t(); }

  t() 
  {
    final Frame f=  new Frame("Cursor test");
    final Button n= new Button("Normal Cursor");
    final Button w= new Button("Wait Cursor");
    
    f.add("North",n);
    f.add("South",w);
    
    f.pack();
    f.setVisible(true);

    // ------------ add listeners ------------ //

    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        f.dispose();
        System.exit(0);
      }
    });

    n.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        JaMUtil.setCursor(w, Cursor.DEFAULT_CURSOR);
      }
    });

    w.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        JaMUtil.setCursor(n, Cursor.WAIT_CURSOR);
      }
    });
   
  }
}

-- 
Jan-Henrik Haukeland

Reply via email to