Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ActionListener vs. MouseListener

570 views
Skip to first unread message

Chris Bizon

unread,
Jan 23, 2001, 1:39:00 AM1/23/01
to
Hi all,

I'm having what I guess is a common problem with MouseListeners vs.
ActionListeners for JButtons. Just for a point of reference, consider
SwingApplication.java from the Swing tutorial at:
http://web2.java.sun.com/docs/books/tutorial/uiswing/start/example-swing/

It's just a JButton with an ActionListener. When the button is clicked,
a counter increments. The definition of click is pretty broad: you can press
the button, move the mouse all around, and as long as you release the mouse
inside the button, an ActionEvent is fired, and the counter increments.

Now, for fun, we change the ActionListener to a MouseListener and
instead of actionPerformed, we look for mouseClicked. The basic operation
is the same, but the definition of clicked is a bit narrower: if you
slide the mouse around after the button is pressed but before released,
no mouseClicked event is fired, and the counter doesn't count. (This is using
1.3 for linux, at least).

So suppose I want the ActionListener behavior. After all, people tend to be
messy about their clicks, moving the mouse around and so on, and it'll be
an annoying application to use if the user has to be incredibly careful so
as not to move the mouse a single pixel while clicking a button. Fine, so
just use the ActionListener, you say. But what if I also want to be able
to differentiate left-clicks from right-clicks on a button? As far as I can
tell, there's no way to do that using an ActionEvent, and there's no (simple?)
way to get the desired clicking behavior using a MouseEvent.

So what do folks do in this situation? Capture both kinds of events? More
complicated MouseListeners?

Thanks for any input,
Chris

Arra Avakian

unread,
Jan 23, 2001, 9:42:21 AM1/23/01
to
Chris,

The "problem" is due to this code in javax.swing.plaf.basic.BasicButtonListener,
which is used by javax.swing.plaf.basic.BasicButtonUI:

public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e) ) {
AbstractButton b = (AbstractButton) e.getSource();
if(b.contains(e.getX(), e.getY())) {
ButtonModel model = b.getModel();
if (!model.isEnabled()) {
// Disabled buttons ignore all input...
return;
}
if (!model.isArmed()) {
// button not armed, should be
model.setArmed(true);
}
model.setPressed(true);
if(!b.hasFocus()) {
b.requestFocus();
}
}
}
};

Perhaps the solution is to change the UI implementation to look also
for right clicks? What will you do differently from a left click?

Arra

Chris Bizon

unread,
Jan 23, 2001, 9:45:45 PM1/23/01
to
On Tue, 23 Jan 2001 09:42:21 -0500, Arra Avakian <ar...@writeme.com> wrote:
>Chris,
>
>The "problem" is due to this code in javax.swing.plaf.basic.BasicButtonListener,
>which is used by javax.swing.plaf.basic.BasicButtonUI:
>
> public void mousePressed(MouseEvent e) {
[chop]

> };
>
>Perhaps the solution is to change the UI implementation to look also
>for right clicks? What will you do differently from a left click?

I really want to do something entirely different given the left or
right clicks. The SwingApplication from sun doesn't really make this point
very well, but I thought it was easier than posting a bunch of code
from my application. Imagine that SwingApplication had two counters,
one for left clicks and the other for right clicks.

I'm afraid I'm not entirely following your explaination. The code you posted
goes into BasicButtonUI. OK. So, is this explaining why mouseClicked performs
badly, or why ActionEvents only are fired with left clicks? If the latter
(which I think is what you're saying), then that doesn't really help: even if I
could get an ActionEvent to fire, it doesn't have a mask to determine whether
it was fired by right or left mouse clicks.

I'm sorry if I misunderstood what you're saying.

C'mon folks, this must be a common question, right? Nobody ever wanted to
have a robust button that could distinguish left from right clicks?

Chris

Claire Sylvestre

unread,
Jan 25, 2001, 9:19:23 PM1/25/01
to
Why not simply listen to mouseReleased events?

Something like this:

public void mouseReleased( MouseEvent e )
{
Point p = e.getPoint();
if ( myButton.contains( p ) )
{
if ( e.isMetaDown() )
{
//right button was clicked
}
else
{
//left button was clicked

Chris Bizon

unread,
Jan 27, 2001, 12:12:38 AM1/27/01
to
On Thu, 25 Jan 2001 21:19:23 -0500,
Claire Sylvestre <claire.s...@videotron.ca> wrote:
>Why not simply listen to mouseReleased events?

Thanks for the response. This was what I ended up doing. For some reason,
I was laboring under a delusion about how mouseReleased worked. Specifically,
I thought that mouseReleased would get called anytime the mouse was released
within the boundaries of a button, regardless of where the mouse was
originally pressed.

Thanks,
Chris

0 new messages