第1个回答 2011-02-25
做一个类DataSet,继承TableModel接口的,然后覆盖isCellEditable方法,比如说第一行/第一列要编辑的,就这么写:
public boolean isCellEditable(int rowIndex, int columnIndex) {
return (rowIndex== 0 || columnIndex==0);
}
然后JTable.setModel(DataSet);
要退出编辑状态时候关闭编辑器:
int row=JTable.getSelectedRow();
int col=JTable.getSelectedColumn();
JTable.getCellEditor(row,col).stopCellEditing();
第4个回答 推荐于2018-04-13
table.setDefaultEditor(Object.class, new DefaultCellEditor(new JTextField()) {
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
final Component textField = super.getTableCellEditorComponent(table, value, isSelected, row, column);
if (textField instanceof JTextField) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
((JTextField) textField).selectAll();
}
});
}
return textField;
}
});
加个这个玩意就可以了。本回答被提问者和网友采纳