JTable table = new JTable(2, 2){
private final KeyStroke tabKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);
public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
AWTEvent currentEvent = EventQueue.getCurrentEvent();
if(currentEvent instanceof KeyEvent){
KeyEvent ke = (KeyEvent)currentEvent;
if(ke.getSource()!=this)
return;
// focus change with keyboard
if(rowIndex==0 && columnIndex==0
&& KeyStroke.getKeyStrokeForEvent(ke).equals(tabKeyStroke)){
((DefaultTableModel)getModel()).addRow(new Object[getColumnCount()]);
rowIndex = getRowCount()-1;
}
}
super.changeSelection(rowIndex, columnIndex, toggle, extend);
}
};
Monday, August 4, 2008
Add rows on tabbing to a cell beyond the last cell
JTable by defaults selects the next cell when TAB is pressed. When the current selection is last cell, pressing tab selects the first cell. But here we want a new row to be inserted and selected the next newly created cell.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment