-3

You know how when you double click on a song in iTunes, a MP3 file plays.

Using a JTable for my user interface, how do I link a row to a text file in the application file system so that when I double click on a row, a txt file associated with that row opens?

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
user1062436
  • 43
  • 1
  • 2
  • 1
    No downvote. Have you tried anything..? – Mudassir Nov 29 '11 at 03:44
  • 2
    What are you having problems with? Do you know how to use a MouseListener for the double click? Do you know how to get the cell that was double clicked in the table so you can get the file name to open? Do you know how to read a file? Do you know how to load a text file into a JTextArea? – camickr Nov 29 '11 at 03:55

1 Answers1

7

On click on any row you need to have the file name with you in order to open it, in the below code I have stored the file name in the last column. And I am using the same on mouse click to open the file in notepad.

Code Snippet:

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.awt.Desktop;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

public class JTableTest {
JTable  myTable;

public JTableTest() {
    JFrame frame = new JFrame("Double Click on Table Test");
    final String[] columnNames = {"S.No. ", "File Name", "File Path", ""};
    final Object[][] tableData = {{"1", "test1.txt", "C://test1.txt", "C:/Test/test1.txt"},
            {"2", "test2.txt", "C://test2.txt", "C:/Test/test2.txt"}, {"3", "test2.txt", "C://test3.txt", "C:/Test/test3.txt"},};

    TableModel dataModel = new AbstractTableModel() {
        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return tableData.length;
        }

        public Object getValueAt(int row, int col) {
            return tableData[row][col];
        }

        public String getColumnName(int column) {
            return columnNames[column];
        }

        public Class getColumnClass(int col) {
            return getValueAt(0, col).getClass();
        }

        public void setValueAt(Object aValue, int row, int column) {
            tableData[row][column] = aValue;
        }
    };

    myTable = new JTable(dataModel);
    myTable.getColumnModel().getColumn(3).setMaxWidth(0);
    myTable.getColumnModel().getColumn(3).setMinWidth(0);
    myTable.getColumnModel().getColumn(3).setPreferredWidth(0);
    myTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    frame.getContentPane().add(myTable);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    myTable.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                int selectedRow = myTable.getSelectedRow();
                try {
                    Desktop.getDesktop().open(new File((String) myTable.getValueAt(selectedRow, 3)));
                } catch (IOException e1) {
                    e1.printStackTrace();
                    }
                }
            }
        });
    }

    public static void main(String[] args) {
        new JTableTest();
    }
}

Hope this gets you going.

mprabhat
  • 20,107
  • 7
  • 46
  • 63