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!

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);

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);

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.