1

I have a JTable with one column displaying certain text in every row Like,

Example (www.example.com)
Test (www.Test.com)

Now, when mouse hovers on url i.e. text inside bracket cursor should change to HAND cursor and text inside bracket should be in different color and underlined to show that it looks like its an hyperlink Tried lot of things but couldn't achieve.

Tried TableCellRenderer, but its changes the whole row content

Tried Map map = new Hashtable();

map.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);

font = font.deriveFont(map);

but again it changes the content of whole row

Any solution would be great help

Object rows[][]={ };

Object headers[]={""};

TableModel model=new DefaultTableModel(rows,header);

String name= "Example";
String url="www.example.com";
String display = name + "(" + url +")" ;
(DefaultTableModel)model).addRow(new Object[]{display});

JTable table=Jtable(model);

2 Answers2

1

Implementing a TableCellRenderer is the way to go. getTableCellRendererComponent can return any Swing component. So your question boils down to "how to show a string with multiple formats" and "how to change the cursor".

  • To show a string with multiple formats, you can use html in a single JLabel, use multiple JLabels in a JPanel or use a JEditorPane.
  • Setting a Handcursor boils down to a call to setCursor, which is available on all JComponents.
BetaRide
  • 16,207
  • 29
  • 99
  • 177
1

You can try this.

And for adding multicoloured look for cell value in table use Label.

Have look over this answer.

Community
  • 1
  • 1
Gaurav
  • 11
  • 2