1,105,197 Community Members

Adding a link to JTable cell

Member Avatar
LianaN
Posting Whiz in Training
285 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi! I would like to add a link to JTable cell. When a user will click on this cell, then a file pointed in the cell will be opened. Could someone tell me some hints? Thanks!

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 
Member Avatar
adams161
Posting Whiz in Training
289 posts since May 2008
Reputation Points: 21 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,

Maybe one approach is to split this into two parts. First detecting clicks, then if you identify they click on a url, ( you will know what they click on and maybe you look if it starts with www. or http://www.), open the url in your listener that heard the click.

I use some code like this to detect clicks:

MouseListener mouseListenerEvents = new MouseAdapter() {
     public void mouseClicked(MouseEvent e) {
         if (e.getClickCount() == 2) { // double click detection

             JTable target = (JTable)e.getSource();
      int row = target.getSelectedRow(); // i just care about rows but i imagine you can get collumns
      row = sorter.convertRowIndexToModel(row); // sorter is my row sorter. need to map the sort model to the real. if you dont use a row sorter this step is not needed
      /*int index = gametable.rowAtPoint(e.getPoint());*/
			String gameIndex = (String)gametable.getModel().getValueAt(row,0);

and later i do

gametable.addMouseListener(mouseListenerEvents);
Member Avatar
adams161
Posting Whiz in Training
289 posts since May 2008
Reputation Points: 21 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 0 [?]
 
0
 

this code placed in the listener when you determine they clicked on a url can open it in windows,

String[] cmd = new String[4];
	              cmd[0] = "cmd.exe";
	              cmd[1] = "/C";
	              cmd[2] = "start";
	              cmd[3] = myurl;

	              rt.exec(cmd);
Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

this code placed in the listener when you determine they clicked on a url can open it in windows,

There is a problem with putting too much code in a listener. Listeners are on Swing's EDT/GUI thread and you don't want to use that for too long. Do the minimal in the listener.
You will want to start another thread to execute the OS's cmd.exe.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article