this.tModel.insertRow(rowCount,new Object[] {"","","",""});
this.table.setRowSelectionAllowed(true);
this.table.changeSelection(0, 0, false, false);
this.table.addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER) {
rowCount = this.table.getSelectedRow() + 1;
tModel.insertRow(rowCount,new Object[]{"", "","",""});
}
}
});
I am trying to create a jtable adding rows at run time on mouse click. i alredy added a default row. but i cant get selection on that row. and i want to change the selection to newly added row when added on the key pressed action? please suggest an answer? thanks in advance
To select the last entry in a table, you could use a method like this:
or more general:
Hint: This does not take care of different sortings
Firstly, I would encourage you to use the key bindings API,
KeyListener
is a low level API and events can be consumed before you ever see them.To change/set the row selection in a
JTable
, you should use JTable#setRowSelectionInterval