aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Unable to implement detection of click on a JButton using Swing and AWT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Unable to implement detection of click on a JButton using Swing and AWT" Watch "Unable to implement detection of click on a JButton using Swing and AWT" New topic
Author

Unable to implement detection of click on a JButton using Swing and AWT

Nate Lockwood
Ranch Hand

Joined: Feb 22, 2010
Posts: 83
Because of my inexperience with AWT and SWING I've adapted some code that allows me to display a propitiatory image. I'm trying to extend this so that the user may retrieve a list of images and move through them with buttons such as "Next", "Last, etc. I've been able to show the location and pixel value at the mouse tip but can't get the mouse click to work here.
Here's the class:

This is an abbreviated example of the setup code:

This is supposed to detect an action on the button and no matter what I do elsewhere it is never launched.


Nothing I've found by using Google to search has returned anything useful so far.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

The keyword this inside the anonymous inner class that implements Runnable refers to that inner class instance -- and that anonymous inner class does not implement ActionListener.

I would recommend adding an anonymous ActionListener to the button. As it stands, you can add the otherwise unused rt as the ActionListener.

Why does ImageViewer3 extend JPanel when you don't use it as one?


luck, db
There are no new questions, but there may be new answers.
Nate Lockwood
Ranch Hand

Joined: Feb 22, 2010
Posts: 83
Thanks, I'll give that a try.

> Why does ImageViewer3 extend JPanel when you don't use it as one?

The short answer is: that's what the author of the example I used did!
I don't understand all that he did but here's some additional code that doesn't work without it. I suspect there's a more straightforward and cleaner way of implementing this. I'm still learning Java, too.
This is his constructor.


Later on
Bill Johnston
Ranch Hand

Joined: Nov 17, 2005
Posts: 201
Nate Lockwood wrote:


Based on this code above, I would say, don't create jpn, and don't add it to frame. Instead add rt as the ActionListener to jbFirst, add jbFirst to rt, and add rt to frame.


~Bill
Nate Lockwood
Ranch Hand

Joined: Feb 22, 2010
Posts: 83
Bill Johnston wrote:
Nate Lockwood wrote:


Based on this code above, I would say, don't create jpn, and don't add it to frame. Instead add rt as the ActionListener to jbFirst, add jbFirst to rt, and add rt to frame.


Thanks. Just to be clear jpn is a panel that contains 4 buttons so far. I've already implemented Darryl's suggestions (and took care of a couple of other problems as well. All is working as I wish.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Nate Lockwood wrote:Just to be clear jpn is a panel that contains 4 buttons so far.

And rt is a panel to which nothing is added, and which is never shown. Bill was recommending that you use that panel instead of creating another.
Nate Lockwood
Ranch Hand

Joined: Feb 22, 2010
Posts: 83
OK, In understand now, thanks. rt is used - the image is painted on it. Here's the code that paints the image and I assume that the coordinate 0, 0 used to place the image on the frame on component at 0,.0 as specified by GridLayout.
Thanks to Bill and for your reply or I wouldn't have figured out that little bit of information!!




This is the way I'm associating an action Listener for each button:


Is it considered better to extend a JPanel and implement the action listener there?

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Unable to implement detection of click on a JButton using Swing and AWT