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 / March 2005

Tip: Looking for answers? Try searching our database.

How to detect a mouse click on an image icon in a JLabel in a JList.

Thread view: 
janderson - 21 Mar 2005 23:32 GMT
I am new to Java Swing.  I have a JList which displays a list of
JLabels.  The JLabels each contain text and an image icon.  Is there a
way to detect when a user clicks on the image icon vs. the text of the
JLabel?

Thanks
John McGrath - 22 Mar 2005 02:12 GMT
> I am new to Java Swing.  I have a JList which displays a list of
> JLabels.  The JLabels each contain text and an image icon.  Is there a
> way to detect when a user clicks on the image icon vs. the text of the
> JLabel?

A JList cannot really display a list of JLabels.  Rather, it displays its
cells using a ListCellRenderer, and that renderer can use a JLabel as the
renderer component.

Since there are really no JLabels in the UI, the JLabel cannot receive
mouse clicks - those go to the JList itself.  So if you want to determine
whether the mouse click is "on" the icon, you will have to know where the
icon is painted.  Obviously, in order to get that information, you want to
ask the object that decides where the icon will be painted.  But as far as
I know, JLabel does not expose that information.

You may be able to get that information using a JLabel subclass, but I
suspect that will not be easy.  You could also create your own component
using multiple JLabels that does expose it.  Finally, you could just make
some assumptions about how a JLabel paints itself.  Doing that is never
pretty, but sometimes it makes sense if you can write code that works, and
spend a lot less time writing it.

Although not documented, JLabel uses SwingUtilities.layoutCompoundLabel()
to decide where to place the text and the icon.  If you use this approach,
I would definitely recommend that you make sure you isolate the code that
makes this assumption, by creating your own renderer class that provides a
method to get the icon position.

Signature

Regards,

John McGrath



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.