JTable错误获得价值

时间:2015-07-15 14:16:08

标签: java jtable

我无法解决此错误,它总是在网上说“无法访问的声明”

 int row = e.getFirstRow()

它建议,拆分声明,但是,即使在那之后,错误在"e.getFirstRow()"行上保持不变:

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class View extends javax.swing.JFrame implements TableModelListener{
    int row;
    DefaultTableModel model;

    public View() {
        initComponents();

        model = (DefaultTableModel) jTable2.getModel();
        model.addRow(new Object[]{"abc","abc","abc","abc"});
        model.addRow(new Object[]{"abc","abc","abc","abc"});
        model.addRow(new Object[]{"abc","abc","abc","abc"});
        model.addRow(new Object[]{"abc","abc","abc","abc"});

        model.addTableModelListener(this);
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "A", "B", "C", "D"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTable2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 546, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>                        


    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new View().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable2;
    // End of variables declaration                   

    @Override
    public void tableChanged(TableModelEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); 

        row = e.getFirstRow();
        int column = e.getColumn();  //<<<--------------- This LINE
        TableModel model = (TableModel)e.getSource();
        String columnName = model.getColumnName(column);
        Object data = model.getValueAt(row, column);
    }
}

1 个答案:

答案 0 :(得分:1)

问题是这一行(在无法到达之前):

throw new UnsupportedOperationException("Not supported yet."); 

你的程序永远不会执行该语句下面的内容,因为它总是会抛出Exception。编译器会告诉你永远不会执行“无法访问的语句”。

删除throw ...声明,您就可以了!