This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes clicking on icons does not work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "clicking on icons does not work" Watch "clicking on icons does not work" New topic
Author

clicking on icons does not work

Marc Beck
Ranch Hand

Joined: Sep 09, 2006
Posts: 36
I have a panel with ImageIcons. When I click on the panel, I get the coordinates where I clicked, but when I click directly on the icons, nothing happens. This is part of a larger program(>4000 lines of code). I want to get feedback when I clicked on the icons. I hope this problem is not too complex.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if you have anything to do with a mouselistener on the labels e.g. tooltips,
the mouseEvent is consumed and not passed to the panel.
Marc Beck
Ranch Hand

Joined: Sep 09, 2006
Posts: 36
Thanks for pointing that out. That must be the reason, because I just recently added tooltips on my ImageLabels, and I think everything worked alright before then. How can I get around the problem? I am sure there must be a way somehow.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
depending on how many labels you have, the easy way
might be to just add a mouseListener to the label,
then dispatch the event to the panel

Marc Beck
Ranch Hand

Joined: Sep 09, 2006
Posts: 36
I have this now:



and I get the following error message:
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
1)
if ImagePanel is the panel with the icons
ImagePanel.add(IconLabel);

then you need to dispatch the event to ImagePanel
//panel.dispatchEvent(me);
ImagePanel.dispatchEvent(me);

2)
"...is not abstract..."
any time you 'implement' an interface, you have to handle each method (can be empty)
easy fix is to, instead, use the built-in class that provides empty methods for the interface

//class IconMouseListener implements MouseListener{
class IconMouseListener extends MouseAdapter{

3)
whatever method you are listening to in your panel's mouseListener (to get the co-ords),
the method you dispatch from must be the same
mousePressed() --> mousePressed()
etc
Marc Beck
Ranch Hand

Joined: Sep 09, 2006
Posts: 36
I changed it to:



The error message is gone, but still nothing happens when I click on the icons.I am sure that ImagePanel is the correct panel, because that is the one that contains the icons and that has the main MouseListener. Thanks for your help and for being patient.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I just posted this at sun's forum (response to a different problem),
so it's an easy copy/paste

<copy/paste>
if this is the part not working
public void mouseClicked ( MouseEvent e ) {
be aware that mouseClicked does not always fire.

mousePressed event fires first, then mouseReleased, then
if the coords of pressed/released match, mouseClicked fires.
so, the mouse only has to move 1 pixel up/down/left or right
between press and release and mouseClicked won't fire.
</copy/paste>

make a small sample program - frame/panel/image with some listeners
to just print something to the console and see if it works OK for
mousePressed. if OK, you should be able to modify your actual code
to mousePressed

<EDIT>
to make it a bit easier, here's the dummy program to test
(just change the code or image file name to match)

then click anywhere on the panel or the image, and "OK" should print in the console

[ February 21, 2008: Message edited by: Michael Dunn ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: clicking on icons does not work
 
Similar Threads
Component.setEnabled()
Selecting JPanels
Multiple controls in a panel
Is there a way for updating JTree and JTable with a same model?
howto detect which button is click