0

我正在使用 Swing n Netbeans IDE 卡在一个点上。我想在 JTable 的单元格之一中添加文本文件的地址。此外,如果我单击该字段它应该打开相同的文件。

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];
4

1 回答 1

0

使用以下代码在单击文件单元格时打开文件。

    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);
        }
   });
于 2013-02-13T19:37:54.357 回答