【问题标题】:Add a File Address in JTable, in java swing using Net Beans在 JTable 中添加文件地址,在 java swing 中使用 Netbeans
【发布时间】:2013-02-14 04:06:43
【问题描述】:

我使用 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];

【问题讨论】:

标签: java swing jtable netbeans-7


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2014-01-23
    • 2023-03-30
    • 2015-09-11
    • 2010-12-01
    • 2013-08-18
    • 2014-06-18
    • 2020-09-29
    • 2011-08-19
    相关资源
    最近更新 更多