3 回答
TA贡献1842条经验 获得超21个赞
我已经这样做了并且它的工作......希望我的回答可以帮助任何人......
private void datatypetableKeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
int col = datatypetable.getSelectedColumn();
int row = datatypetable.getSelectedRow();
int colCount = datatypetable.getColumnCount();
int rowCount = datatypetable.getRowCount();
col++;
if (col >= colCount) {
col = 0;
row++;
}
if (row >= rowCount) {
row = 0;
}
// datatypetable.getSelectionModel().setSelectionInterval(row, row);
// datatypetable.getColumnModel().getSelectionModel().setSelectionInterval(col, col);
// datatypetable.editCellAt(row, col);
if (row == rowCount - 1) {
addRow();
datatypetable.scrollRectToVisible(datatypetable.getCellRect(rowCount, 0, true));
}
}
if (evt.getKeyCode() == KeyEvent.VK_TAB) {
int col = datatypetable.getSelectedColumn();
int row = datatypetable.getSelectedRow();
int colCount = datatypetable.getColumnCount();
int rowCount = datatypetable.getRowCount();
col++;
if (col >= colCount) {
col = 0;
row++;
}
if (row >= rowCount) {
row = 0;
}
// datatypetable.getSelectionModel().setSelectionInterval(row, row);
// datatypetable.getColumnModel().getSelectionModel().setSelectionInterval(col, col);
// datatypetable.editCellAt(row, col);
if (col == colCount - 1) {
addColumn();
int selectedRow = 0;
datatypetable.editCellAt(selectedRow, 0);
datatypetable.setSurrendersFocusOnKeystroke(true);
datatypetable.getEditorComponent().requestFocus();
datatypetable.scrollRectToVisible(datatypetable.getCellRect(colCount, 0, true));
}
}
}
private void addRow() {
DefaultTableModel model = (DefaultTableModel) datatypetable.getModel();
if (model != null) {
Vector v = new Vector(1);
// for (int j = 0; j <= datatypetable.getColumnCount(); j++) {
// v.add("");
// }
v.add("");
model.addRow(v);
}
}
private void addColumn() {
DefaultTableModel model = (DefaultTableModel) datatypetable.getModel();
if (model != null) {
Vector v = new Vector(1);
// for (int j = 0; j < datatypetable.getRowCount(); j++) {
// v.add("");
// }
v.add("");
model.addColumn(Character.toString((char) ('A' + datatypetable.getColumnCount())), v);
}
}
TA贡献1833条经验 获得超4个赞
您可以像这样添加列。
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
//add you dynamic column name logic. ex, I am adding with i loop variable.
for( int i=1; i<5;i++){
model.addColumn("Col"+i);
}
希望你有办法添加列。
添加回答
举报