Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9143181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:57:35+00:00 2026-06-17T09:57:35+00:00

Consider the MouseListener below. My question is this: is having the extra features that

  • 0

Consider the MouseListener below. My question is this: is having the extra features that this listener provides, some of which you won’t need, worth the memory and processing overhead that comes with having these features? Or should “verbose” implementations like this be avoided?

import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Timer;

/**
 * This is an overkill class that is useful for distinguishing between buttons and includes functions for hold and double-click events.
 *
 * @author Paranoid Android
 */
public class ParanoidMouseListener extends MouseAdapter {

    public static final int LEFT = MouseEvent.BUTTON1;
    public static final int MIDDLE = MouseEvent.BUTTON2;
    public static final int RIGHT = MouseEvent.BUTTON3;

    private DoubleClickTimer leftDouble = new DoubleClickTimer();
    private DoubleClickTimer middleDouble = new DoubleClickTimer();
    private DoubleClickTimer rightDouble = new DoubleClickTimer();

    private MouseEvent event;
    private int pressedButton;
    private Component pressed;
    private boolean dragging;


    /**
     * This method allows methods to ignore the MouseEvent when not needed.
     *
     * @return the latest mouse event.
     */
    public MouseEvent getEvent() {
        return event;
    }

    private HoldTimer leftHold = new HoldTimer() {

        @Override
        public void perform() {
            onLeftHold();
        }
    };
    private HoldTimer middleHold = new HoldTimer() {

        @Override
        public void perform() {
            onMiddleHold();
        }
    };
    private HoldTimer rightHold = new HoldTimer() {

        @Override
        public void perform() {
            onRightHold();
        }
    };

    @Override
    public final void mouseClicked(MouseEvent event) {
        this.event = event;
        switch (event.getButton()) {
            case LEFT:
                if (leftDouble.isRunning()) {
                    leftDouble.stop();
                    onLeftDoubleClick();
                } else {
                    leftDouble.start();
                    onPureLeftClick();
                }
                break;
            case MIDDLE:
                if (middleDouble.isRunning()) {
                    middleDouble.stop();
                    onMiddleDoubleClick();
                } else {
                    middleDouble.start();
                    onPureMiddleClick();
                }
                break;
            case RIGHT:
                if (rightDouble.isRunning()) {
                    rightDouble.stop();
                    onRightDoubleClick();
                } else {
                    rightDouble.start();
                    onPureRightClick();
                }
                break;
        }
    }

    @Override
    public final void mousePressed(MouseEvent event) {
        this.event = event;
        pressedButton = event.getButton();
        pressed = event.getComponent();
        switch (event.getButton()) {
            case LEFT:
                leftHold.start();
                onLeftPress();
                break;
            case MIDDLE:
                middleHold.start();
                onMiddlePress();
                break;
            case RIGHT:
                rightHold.start();
                onRightPress();
                break;
        }
    }

    @Override
    public final void mouseReleased(MouseEvent event) {
        this.event = event;
        pressedButton = -1;
        Component src = event.getComponent();
        boolean contains = src.contains(event.getPoint());
        switch (event.getButton()) {
            case LEFT:
                leftHold.stop();
                onLeftRelease();
                if (!dragging && src == pressed && contains) onLeftClick();
                break;
            case MIDDLE:
                middleHold.stop();
                onMiddleRelease();
                if (!dragging && src == pressed && contains) onMiddleClick();
                break;
            case RIGHT:
                rightHold.stop();
                onRightRelease();
                if (!dragging && src == pressed && contains) onRightClick();
                break;
        }
        dragging = false;
    }

    @Override
    public final void mouseMoved(MouseEvent event) {
        this.event = event;
        moved();
    }

    @Override
    public final void mouseDragged(MouseEvent event) {
        this.event = event;
        dragging = true;
        switch (pressedButton) {
            case LEFT:
                onLeftDrag();
                break;
            case MIDDLE:
                onMiddleDrag();
                break;
            case RIGHT:
                onRightDrag();
                break;
        }
    }

    @Override
    public final void mouseEntered(MouseEvent event) {
        this.event = event;
        entered();
    }

    @Override
    public final void mouseExited(MouseEvent event) {
        this.event = event;
        exited();
    }

    private int getDoubleClickInterval() {
        String property = "awt.multiClickInterval";
        return (int) Toolkit.getDefaultToolkit().getDesktopProperty(property);
    }

    private class DoubleClickTimer extends Timer {

        public DoubleClickTimer() {
            super(getDoubleClickInterval(), null);
            this.setRepeats(false);
        }
    }

    public int getHoldInitialDelay() {
        return 300;
    }

    public int getHoldQueueDelay() {
        return 60;
    }

    private class HoldTimer extends Timer {

        public HoldTimer() {
            super(getHoldQueueDelay(), null);

            this.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    perform();
                }
            });
            this.setInitialDelay(getHoldInitialDelay());
        }

        public void perform() {
        }
    }

    public void moved() {
    }

    public void entered() {
    }

    public void exited() {
    }

    public void onLeftHold() {
    }

    public void onMiddleHold() {
    }

    public void onRightHold() {
    }

    public void onLeftClick() {
    }

    public void onMiddleClick() {
    }

    public void onRightClick() {
    }

    public void onPureLeftClick() {
    }

    public void onPureMiddleClick() {
    }

    public void onPureRightClick() {
    }

    public void onLeftDoubleClick() {
    }

    public void onMiddleDoubleClick() {
    }

    public void onRightDoubleClick() {
    }

    public void onLeftPress() {
    }

    public void onMiddlePress() {
    }

    public void onRightPress() {
    }

    public void onLeftRelease() {
    }

    public void onMiddleRelease() {
    }

    public void onRightRelease() {
    }

    public void onLeftDrag() {
    }

    public void onMiddleDrag() {
    }

    public void onRightDrag() {
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:57:36+00:00Added an answer on June 17, 2026 at 9:57 am

    As Hovercraft Full Of Eels points out in the comments, this is a classic case of You Aren’t Gonna Need It. Implementing functionality before you have a clear notion of who will use it and when is generally a no-no. In this situation, considering the use cases you outlined in the comments, you have several options:

    • Use this class everywhere and accept the marginally higher overhead. Odds are you don’t much care about the performance implications, which are likely to be very small. However, this does introduce a greater dependency on this class throughout the rest of your code, meaning if you introduce a regression at a later date, you’re in risk of breaking a large number of related systems.

    • Allow consumers of the class to indicate which features they will use (e.g., double clicking) and disable features that the consumer does not want. This introduces complexity into your class and makes it more likely to be buggy, as well as making testing more difficult (though hardly impossible). If consistency is badly needed between classes, this may be an option.

    • Use this class when the added functionality is needed, and use an ordinary MouseAdapter elsewhere. This is probably your best option, especially if certain behavior cases are not well-defined in your custom class. This reduces dependency on your class and simplifies the class internally as well. The trade-off is less consistency in how mouse interaction is handled between consumer classes, and slightly more code in order to implement a MouseAdapter for consumers – generally a worthwhile trade-off.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this condition that exists in a template that is called recursively: <xsl:if test=$i
Consider i have a dataGrid that is created dynamically. Now i need to add
Consider this example - I have a class called Report that has a field
Consider the console application below, featuring a method with a generic catch handler that
Consider a standard use of the CRTP , for some expression template mechanism, which
Consider this scenario. We have an internal Rails 2 app that connects to a
Consider having the following header file (c++): myclass.hpp #ifndef MYCLASSHPP_ #define MYCLASSHPP_ namespace A
Consider this code: enum { ERR_START, ERR_CANNOTOPENFILE, ERR_CANNOTCONNECT, ERR_CANNOTCONNECTWITH, ERR_CANNOTGETHOSTNAME, ERR_CANNOTSEND, }; char* ERR_MESSAGE[]
Consider a markup such as <select id=blah> <option value=3>Some text</option> <option value=4>Some text</option> <option
Consider the following line of Lisp code: (some-function 7 8 | 9) ;; some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.