Hyperlink in Jtable - Lots of hyperlinks!

[nobr]Hi.

I'm trying to get it, so that when someone clicks on an item in one of my columns it opens up a browser window, depending on the row that they have clicked on.

For example, in my table i may have

Name Number

Alex 1

David 2

Mike 3

When someone clicks on Alex, i want it to take them to www.site1.com, when someone clicks on David, it takes them to www.site2.com etc...

Now i've searched the forums, and read about listeners etc, however i'm not sure how to make it so that each row has a different link.

The table is current built using:

public GetFarms()throws Exception{

URL theUrl =new URL("http://www.allydm.co.uk/Covenant/farm_list.php");

BufferedReader in =new BufferedReader(

new InputStreamReader(

theUrl.openStream()));

String inputLine = in.readLine();

String[] lines = inputLine.split("<br>");

for (int i = 0; i < ROWS; i++){

String[] stuff = lines[i].split(" ");

for (int j = 0; j < COLUMNS; j++){

cells[i][j] = stuff[j];

}

}

in.close();

String[]columnNames ={"Username","DA","Sentry","Last Update"};

sampleJTable =new JTable(cells, columnNames);

JScrollPane tablePane =new JScrollPane(sampleJTable);

tablePane.setPreferredSize(new Dimension(420, 295));

add(tablePane, BorderLayout.CENTER);

}

Does anyone have any suggestions on how i could implement links?

Thankyou for any help.[/nobr]

[2307 byte] By [allydma] at [2007-11-26 21:48:51]
# 1

Which version of Java are you using? I am basing my response on 1.5.

There are two aspects to your problem:

1. making text in the table cells look like links

2. making the links actually work

1. You probably want to use a custom renderer. Create a class that implements TableCellRenderer, and then set that renderer for the desired column(s) in your table. (You can give each column its own renderer, you can use a separate renderer for headers, and each custom renderer you define can base its behavior on the contents of the cell it's displaying. I think just about any professional application involving a table would want to use custom renderers.) Here is some glorified pseudocode:

class MyRenderer extends JButton implementsTableCellRenderer{

// This is a very simple example. You might want your renderer to return a different

// component (perhaps a JPanel) containing the hyperlink and also other things.

public MyRenderer(...){

super();

setBorderPainted(false);

setContentAreaFilled(false);

}

// Note that the value, row and column are all parameters to this method.

public Component getTableCellRendererComponent(final JTable table, Object value, boolean isSelected,

boolean hasFocus, final int row, final int column){

final String valueString = (value == null) ? "" : value.toString();

// I'm just using HTML here to make it look like a hyperlink. I intentionally left the

// link location blank, since it would be ignored anyway.

setText("<html><a href=\"\">" + valueString + "</a></html>");

return this;

}

}

//...

sampleJTable = new JTable(cells, columnNames);

MyRenderer linkRenderer = new MyRenderer(...);

for (TableColumn c : sampleJTable.getColumnModel().getColumns()){

if (c should display hyperlinks){

c.setCellRenderer(linkRenderer);

}

}

2. What do you want to happen when the user clicks a hyperlink? You can use JEditorPane as an internal browser. For launching the user's default browser, I like JDIC: https://jdic.dev.java.net/nonav/documentation/javadoc/jdic/index.html

For example:

Desktop.browse(new URL("http://some.url"));

The code that responds to the click belongs in a listener. This is where things get complicated. The exact listener you need to use will depend on your project requirements and how you've implemented your renderer.

The easiest thing to do is to use a ListSelectionListener on the table's selection model:

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#selection

That will respond to a click anywhere in the cell. If you must respond only to clicks on the link and not to clicks elsewhere in the cell, you may need to use a MouseListener. (The button the renderer draws is not active, so you can't just add a listener to it.)

The.Joy.of.Javaa at 2007-7-10 3:39:54 > top of Java-index,Desktop,Core GUI APIs...
# 2
Add a MouseListener to the table. When you click on a cell you get the url from the cell and invoke the browser.
camickra at 2007-7-10 3:39:54 > top of Java-index,Desktop,Core GUI APIs...
# 3
http://forum.java.sun.com/thread.jspa?threadID=5147450
Rodney_McKaya at 2007-7-10 3:39:54 > top of Java-index,Desktop,Core GUI APIs...