aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to display HyperLink Data in JTable column Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to display HyperLink Data in JTable column" Watch "How to display HyperLink Data in JTable column" New topic
Author

How to display HyperLink Data in JTable column

vaibhav agarwal
Greenhorn

Joined: May 06, 2005
Posts: 7
Hi All

I have to display HyperLink Data in Jtable column... and that HyperLink is a URL after clicking that HyperLink data should be displayed..

still i couldn't get how can i display HyperLink in JTable column....

any one having any idea regarding this problem...

Thanks & Regards

Vaibhav
Scott Delap
author
Ranch Hand

Joined: Apr 05, 2005
Posts: 73
Sounds simple but it isn't. First a little JTable 101. Each cell in a JTable isn't a component. It is a block on the screen that is painted. Think of it more like an image. The JTable looks up a cell renderer based on the cell location, object type in the cell, etc and then uses it to paint the area. The default cell render is based off a JLabel. Since there is no component, you can't add a mouse listener or track click events to that label.

When you click on a cell the table goes into "edit" mode. A cell editor is created and displayed in the area the cell previous occupied. This is an acutal component that you can manipulate if you want.

Now back to your case. I'm guessing you want your links to appear as html style links. Since the table uses a render that extends JLabel (which supports html) you can feed it text like this:

<html><b><a href="">bold</a></b></html>

However, I wouldn't recommend altering the data in your model just to effect display. Instead I would create a custom cell renderer which accomplishes this effect and set it on the table. You could either wrap your text in the HTML or manually set font color and style properties on the renderer to mimic html.

Now even if you were to include the url in the html you still can't click on it. There is no component in the table. You don't really want to go into edit mode when clicking on a url. You just want to open that link. To accomplish this you would add a mouse listener to the table itself. When you receive a click event, you would then programatically determine which cell it was over, go back to your model and get the url, and finally use other Java API calls to open that url.

A quick Google search will reveal examples of creating a cell renderer, using a mouse listener, and opening a url if you need more information.

Scott Delap
Desktop Java Live
ClientJava.com
vaibhav agarwal
Greenhorn

Joined: May 06, 2005
Posts: 7
Thanks for Solution...

but still i could not exect... can you please send some code.....

>You just want to open that link...

Exactly I want only it bother about how so please give me some code related to this topic

Thanks & Regards
Vaibhav
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by vaibhav agarwal:
Exactly I want only it bother about how so please give me some code related to this topic

Thanks & Regards
Vaibhav


That's really not what we are here to do. Like Scott said, Google turns up a ton of code examples related to your issue. Not only that but you can search this very forum for code samples.

Here is a quick rundown of how javaranch works. You write some code, it doesn't work, we help you figure it out. We also realize that sometimes a simple code snippit sends you in the right direction, however, you haven't shown any willingness to figure any of this out on your own.

We love helping people, but we love helping people that try even more.

To get you started though Sun has a wonderful tutorial on JTables and it even shows you how to write your very own cell renderer.


GenRocket - Experts at Building Test Data
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to display HyperLink Data in JTable column