Java Swing Mouse decorateComponentWithMouseOverCursorChange(final JComponent toDecorate, final int mouseEntered, final int mouseExited)

Here you can find the source of decorateComponentWithMouseOverCursorChange(final JComponent toDecorate, final int mouseEntered, final int mouseExited)

Description

Adds a mouse listener to the given component, so that the cursor will change on mouse entered/exited.

License

LGPL

Parameter

Parameter Description
toDecorate DOCUMENT ME!
mouseEntered DOCUMENT ME!
mouseExited DOCUMENT ME!

Return

the listener that was added

Declaration

public static MouseListener decorateComponentWithMouseOverCursorChange(final JComponent toDecorate,
        final int mouseEntered, final int mouseExited) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.awt.Cursor;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JComponent;

public class Main {
    /**/* w w w. ja v a2 s .  c o  m*/
     * Adds a mouse listener to the given component, so that the cursor will change on mouse entered/exited.
     *
     * <p>Hint: Uses the awt.Cursor.XXX constants!</p>
     *
     * @param   toDecorate    DOCUMENT ME!
     * @param   mouseEntered  DOCUMENT ME!
     * @param   mouseExited   DOCUMENT ME!
     *
     * @return  the listener that was added
     */
    public static MouseListener decorateComponentWithMouseOverCursorChange(final JComponent toDecorate,
            final int mouseEntered, final int mouseExited) {
        final MouseListener toAdd = new MouseAdapter() {

            private final Cursor entered = new Cursor(mouseEntered);
            private final Cursor exited = new Cursor(mouseExited);

            @Override
            public void mouseEntered(final MouseEvent e) {
                if (toDecorate.isEnabled()) {
                    toDecorate.setCursor(entered);
                }
            }

            @Override
            public void mouseExited(final MouseEvent e) {
                if (toDecorate.isEnabled()) {
                    toDecorate.setCursor(exited);
                }
            }
        };
        toDecorate.addMouseListener(toAdd);
        return toAdd;
    }
}

Related

  1. convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)
  2. convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)
  3. convertMouseEvent(java.awt.Component source, java.awt.event.MouseEvent sourceEvent, java.awt.Component destination)
  4. convertPoint(MouseEvent evt, Component target)
  5. createButton(JComponent parent, String description, MouseListener listener)
  6. delegateEventToParent(Component component, MouseEvent e)
  7. disableDoubleMouseClick(Component c)
  8. forwardMouseWheelEvent(JScrollPane target, MouseWheelEvent evt)
  9. getDeepestComponentAt(final MouseEvent aEvent)