Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / August 2005

Tip: Looking for answers? Try searching our database.

How to consume mousePressed for JButton?

Thread view: 
Rainer Schwarze - 22 Aug 2005 15:15 GMT
Hi,

at the end of my post is an example where I try to consume() the mouse
event, when the click in a button is in the right part of it. When the
click is in the left part, nothing is done and the button shall get pressed.

However, it does not work. Can anybody give me some advice about it?
What I want to do is this: For a button I want to have a region which
defines the valid "active area". When I click inside that area, the
button is pressed, otherwise not. (I am kicking that problem around for
a few days now and it looks like I just miss the relevant information
while searching...)

Thanks a lot in advance.
Code follows:

import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ConsumeTest extends javax.swing.JFrame {
  public class JButtonX extends JButton {
    public void turnOnRegionCheck() {
      addMouseListener(new java.awt.event.MouseAdapter() {
        public void mousePressed(java.awt.event.MouseEvent e) {
          if (e.getX()>getWidth()/2) {
            e.consume();
            System.out.println("don't press button");
          } else {
            System.out.println("do press button");
          }
        }
      });
    }
  }
  public static void main(String[] args) {
    ConsumeTest ct = new ConsumeTest();
    ct.setVisible(true);
  }

  public ConsumeTest() {
    super();
    setSize(200, 100);
    JButtonX btn = new JButtonX();
    this.getContentPane().add(btn, BorderLayout.NORTH);
    btn.setText("Left: shall work, right: consume");
    btn.turnOnRegionCheck();
    this.addWindowListener( new WindowAdapter() {
        public void windowClosing( WindowEvent e ) {
          System.exit(0);
        }
    });
  }
}

Signature

Rainer Schwarze (Mr.) -- remove .nospam for email

Thomas Hawtin - 22 Aug 2005 15:47 GMT
> at the end of my post is an example where I try to consume() the mouse
> event, when the click in a button is in the right part of it. When the
> click is in the left part, nothing is done and the button shall get
> pressed.

You example code does not compile. Please make sure it does before posting.

However, the problem is that consume is relevant only before the
listeners are fired. Even if it did succeed, would you be sure that your
listener was called before the UI delegate's listener?

Also mouse pressed, IIRC, is used to arm the button. Mouse clicked is
used to fire the action event (assuming already armed).

The obvious thing to do is override JComponent.contains.

    JButton button = new JButton("Left half only") {
        @Override
        public boolean contains(int x, int y) {
            return x <= getWidth()/2;
        }
    };

That will also stop rollovers and anything else occurring for the right
hand side.

Alternatively, you could say override the ButtonModel so that it does
not fire action events if the event queue's current event was over the
right hand side.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Rainer Schwarze - 22 Aug 2005 17:12 GMT
>> at the end of my post is an example where I try to consume() the mouse
>> event, when the click in a button is in the right part of it. When the
>> click is in the left part, nothing is done and the button shall get
>> pressed.
>
> You example code does not compile. Please make sure it does before posting.

(Tried it :-) Compiles on my machine right now... What went wrong on yours?)

> However, the problem is that consume is relevant only before the
> listeners are fired. Even if it did succeed, would you be sure that your
> listener was called before the UI delegate's listener?
>
> Also mouse pressed, IIRC, is used to arm the button. Mouse clicked is
> used to fire the action event (assuming already armed).

Those questions bounced through my head, because listeners just smell
like that...

> The obvious thing to do is override JComponent.contains.

Well, indeed. My brain... I came across that a few weeks ago.

Thanks a lot for your quick reply.

Best wishes,
Rainer
Signature

Rainer Schwarze (Mr.) -- remove .nospam for email

Thomas Hawtin - 22 Aug 2005 18:41 GMT
>> You example code does not compile. Please make sure it does before
>> posting.
>
> (Tried it :-) Compiles on my machine right now... What went wrong on
> yours?)

Mea culpa. I should follow my own advice... I read it and decided that
JButtonX (not a good name for a class btw) didn't exist. I didn't expect
some code that had absolutely no reason to extend a class to be in an
inner class.

>> However, the problem is that consume is relevant only before the
>> listeners are fired. Even if it did succeed, would you be sure that
[quoted text clipped - 5 lines]
> Those questions bounced through my head, because listeners just smell
> like that...

The action event from a JButton is fired in response to input listeners,
for instance MouseListener. Even if you manage to stop an event firing
mid way through (for instance by throwing an exception), you don't
necessarily know the order in which the listeners are firing. So whether
the action event has already fired or not is uncertain.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.