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

Tip: Looking for answers? Try searching our database.

Hyperlink inJtable cells

Thread view: 
Austin - 21 Jan 2005 06:00 GMT
I would like to render text inside JTable column cells as Hyperlinks
(so the hand-cursor appears when the mouse hovers over the cells, and a
single click event on the link can be trapped.

I tried using JEditorPane as the cellrenderer for the JTable column
with a HyperLinkListener, but it doesn't seem to work - although the
text appears as a hyperlink, mouse hover over the cell does not change
the cursor. Only after clicking on the cell does the hyperlink behavior
get exposed (The JTable seems to be consuming all mouse events and not
the JEditorPane)

Has anybody tried anything similar? And succeeded??
John McGrath - 21 Jan 2005 18:32 GMT
> I would like to render text inside JTable column cells as Hyperlinks
> (so the hand-cursor appears when the mouse hovers over the cells, and a
[quoted text clipped - 4 lines]
> text appears as a hyperlink, mouse hover over the cell does not change
> the cursor.

That is because the CellRenderer is not there when your mouse hovers over
the cell.  A renderer is not a realized component.  It is (sort of) placed
over the cell when the cell needs painting, and then is immediately
whisked away.  Renderers cannot respond to events on the cell.

If you want to do any event handling, without a TableCellEditor being
active, you will need to listen for the events on the Table itself, and
respond to them appropriately.  So to handle hyperlinks, you would need to
use a MouseMotionListener on the JTable, detect if you are over a link,
change the cursor yourself, etc.

Are you sure you want to use a JTable for this?

Signature

Regards,

John McGrath

Kari Ikonen - 22 Jan 2005 10:32 GMT
> I would like to render text inside JTable column cells as Hyperlinks
> (so the hand-cursor appears when the mouse hovers over the cells, and a
> single click event on the link can be trapped.

Sure, it is easy. And most likely you don't want to use JEditorPane.

You need:

- LinkListener extends MouseAdapter
- LinkRenderer extends DefaultTableCellRenderer

LinkRenderer {
       setValue(Object pValue) {
               boolean isLinkValue = ...
               if (isLinkValue) {
                       String link = "<html>"
                               + "<a href=\"x\">" + pValue.toString() + "</a>"
                               + "</html>";
                       setText(link);
                       putClientProperty(KEY_LINK, pValue);
               } else {
                       ...
               }
       }
}

LinkListener {
       mouseOver() {
               renderer = ...
               LinkRenderer c = renderer.getComponent(...);
               Object value = c.getClientProperty(KEY_LINK);
               if (value!=null) {
                       setMouseCursor(CURSOR_HAND);
               } else {
                       setMouseCursor(CURSOR_DEFAULT);
               }
       }

       mouseClicked() {
               renderer = ...
               LinkRenderer c = renderer.getComponent(...);
               Object value = c.getClientProperty(KEY_LINK);
               if (value!=null) {
                       activateLink(..., value);
               }
       }
}

Doing thing so that link is rendered only when mouse is hovering over label
is not much more difficult. Also handling multiple links from single cell
is also feasible. Some minor things are needed to adjust HTML linkstyle to
match normal cell renderer font style (for JDK 5.0).
Andrew McDonagh - 22 Jan 2005 11:09 GMT
>>I would like to render text inside JTable column cells as Hyperlinks
>>(so the hand-cursor appears when the mouse hovers over the cells, and a
[quoted text clipped - 48 lines]
> is also feasible. Some minor things are needed to adjust HTML linkstyle to
> match normal cell renderer font style (for JDK 5.0).

Are you sure this will work?  I thought that mouse/keyboard events were
not passed to the rendered components, only too edited components.
Kari Ikonen - 22 Jan 2005 17:02 GMT
> Are you sure this will work?  I thought that mouse/keyboard events were
> not passed to the rendered components, only too edited components.

Of course it works.

LinkListener is attached to JTable, there is not any kind of editor
component involved here. Trick is to find out renderer for cell, let it
render value and then just check this rendered value.

you need to:
- resolve (column, row) from mouse coordinates
- get renderer for (column, row)
(- and not mix up table vs. model column indexes)
Andrew McDonagh - 22 Jan 2005 18:07 GMT
>>Are you sure this will work?  I thought that mouse/keyboard events were
>>not passed to the rendered components, only too edited components.
[quoted text clipped - 9 lines]
> - get renderer for (column, row)
> (- and not mix up table vs. model column indexes)

Oh I see, you are adding your mouse listener directly to the JTable...
I thought you were adding it to the component rendered by the renderer.

In that case, yes I agree, it would work as you say.
Austin - 27 Jan 2005 05:02 GMT
Thank you all very much, especially Kari!
mariag p - 25 Feb 2005 18:48 GMT
Hi,
I am new to this swing and jtable.
I don't know how to use the LinkListener.
Can you write the complete code?

Thanks,
mp


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.