Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic