0

I am using Swing n Netbeans IDE stuck at a point. I want to add the address of the text file in one of the cell of JTable. Further If I click that field It should open the same file.

jInternalFrame3.setVisible(true);

    jTable3.setBackground(new java.awt.Color(0, 0, 0));
    jTable3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    jTable3.setForeground(new java.awt.Color(255, 255, 255));
    jTable3.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {

        },
        new String [] {
            "Sr. No", "EXCHANGE", "INSTRUMENT", "SYMBOL", "EXPIRY DATE", "B/S", "LOT/QTY", "PRICE", "STOP LOSS", "TRIGGER PRICE", "FILE NAME", "TIME CREATED"
        }
    ) {
        boolean[] canEdit = new boolean [] {
            false, false, false, false, false, false, false, false, false, false, false, false
        };

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
  • 1
    Also see [How to show URL as a click-able URL in Table and allow them to open in default browser?](http://stackoverflow.com/q/9029514/1048330). – tenorsax Feb 13 '13 at 18:46

1 Answers1

0

Use following code to open the file on click of the file cell.

    table.addMouseListener( new MouseAdapter() {

        public void mouseClicked(MouseEvent e) {
            int col = table.columnAtPoint( e.getPoint() );
            int row = table.columnAtPoint( e.getPoint() );
            int fileColumn = 10;

                if( col != fileColumn )
                return;

            String file = ( String ) table.getValueAt(row, col);
            BufferedReader reader = new BufferedReader(new FileReader( file ) );
            StringBuffer buffer = new StringBuffer();
            String line;
            while( ( line = reader.readLine() ) != null ) {
            if( buffer.length() > 0 )
                buffer.append( "\n" );
                buffer.append( line );
            }

                    JDialog dialog = new JDialog();
            dialog.add( new JTextArea(  buffer.toString(), 140, 100 ) );
            dialog.pack();
            dialog.setVisible(true);
        }
   });
mKorbel
  • 109,525
  • 20
  • 134
  • 319