jtable实现回车新增一行
程序员文章站
2022-04-03 10:05:12
...
// 在进入编辑状态下才自动加行,未进入编辑直接输出修改不换行
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);
}
}
}
}
});
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);
}
}
}
}
});