jtable实现回车新增一行

阅读更多
// 在进入编辑状态下才自动加行,未进入编辑直接输出修改不换行
jTable1.addKeyListener(new KeyAdapter() {
    public void keyReleased(KeyEvent e) {
        if (e.getKeyChar() == '\n') {
            if (jTable1.getSelectedRow() >= jTable1.getRowCount() - 1) {
           
            if(jTable1.getValueAt(jTable1.getRowCount() - 1, 0)!=null&&!jTable1.getValueAt(jTable1.getRowCount() - 1, 0).equals("")){
           
            jTable1.editCellAt(jTable1.getSelectedRow(), jTable1.getSelectedColumn());
                ((DefaultTableModel) jTable1.getModel()).addRow(new Object[jTable1.getColumnCount()]);
                jTable1.editCellAt(jTable1.getRowCount() - 1, 0);
           
            }

            }
        }
    }
});

你可能感兴趣的:(jtable,j2se)