Java Utililty Methods Swing Mouse

List of utility methods to do Swing Mouse

Description

The list of methods to do Swing Mouse are organized into topic(s).

Method

JButtoncreateButton(JComponent parent, String description, MouseListener listener)
Creates a button with the given description text.
JButton button = new JButton(description);
button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
button.addMouseListener(listener);
parent.add(button);
return button;
MouseListenerdecorateComponentWithMouseOverCursorChange(final JComponent toDecorate, final int mouseEntered, final int mouseExited)
Adds a mouse listener to the given component, so that the cursor will change on mouse entered/exited.
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;
voiddelegateEventToParent(Component component, MouseEvent e)
Sends event to parent in it's coordinate space.
delegateEventToParent(component, e, false);
voiddisableDoubleMouseClick(Component c)
disable Double Mouse Click
if (c instanceof JList) {
    java.util.EventListener[] listeners = c.getListeners(java.awt.event.MouseListener.class);
    for (int i = 0; i < listeners.length; i++) {
        if (listeners[i].toString().indexOf("SingleClickListener") != -1) {
            c.removeMouseListener((java.awt.event.MouseListener) listeners[i]);
    return;
...
voidforwardMouseWheelEvent(JScrollPane target, MouseWheelEvent evt)
forward Mouse Wheel Event
Toolkit.getDefaultToolkit().getSystemEventQueue()
        .postEvent(new MouseWheelEvent(target, evt.getID(), evt.getWhen(), evt.getModifiers(), evt.getX(),
                evt.getY(), evt.getClickCount(), evt.isPopupTrigger(), evt.getScrollType(),
                evt.getScrollAmount(), evt.getWheelRotation()));
JComponentgetDeepestComponentAt(final MouseEvent aEvent)
Returns the deepest visible descendent Component of parent that contains the location x, y.
return getDeepestComponentAt(aEvent.getComponent(), aEvent.getX(), aEvent.getY());
booleaninInsets(MouseEvent me)
in Insets
JComponent comp = (JComponent) me.getComponent();
Insets insets = comp.getInsets();
if (me.getX() < insets.left || me.getX() > comp.getSize().width - insets.right || me.getY() < insets.top
        || me.getY() > comp.getSize().height - insets.bottom) {
    return true;
return false;
voidinstallMouseListener(Component cmp, MouseListener mouseListener, boolean removePrevious)
Installs the mouse listener in the tree of components where the argument component is included, starting in the first parent JFrame or JDialog parent.
Component parent = getFirstParentFrameOrDialog(cmp);
if (parent == null) {
    parent = cmp;
List<Component> components = getAllComponents(parent);
for (Component component : components) {
    if (removePrevious) {
        removeMouseListeners(component);
...
booleanisDoubleClick(MouseEvent evt)
Checks if MouseEvent is a double-click.
return (SwingUtilities.isLeftMouseButton(evt) && evt.getClickCount() == 2);
booleanisLeftButtonAndNoModifiers(final MouseEvent event)
is Left Button And No Modifiers
final boolean leftMouseButton = SwingUtilities.isLeftMouseButton(event);
if (leftMouseButton) {
    final int modifiers = event.getModifiers();
    if (InputEvent.BUTTON1_MASK == modifiers) {
        return true;
return false;
...