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.
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?
There are no new questions, but there may be new answers.
Joined: Feb 22, 2010
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.
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.
Joined: Feb 22, 2010
OK, In understand now, thanks. rtis 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?
subject: Unable to implement detection of click on a JButton using Swing and AWT