public class Util {
public static void controlEnterTable(JTable tb, final Action act) {
InputMap im = tb.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
KeyStroke tab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
im.put(enter, im.get(tab));
final Action oldTabAction = tb.getActionMap().get(im.get(tab));
final Action tabAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
JTable table = (JTable) e.getSource();
int row = table.getSelectedRow();
int originalRow = row;
int column = table.getSelectedColumn();
int originalColumn = column;
if ((row + 1 == table.getRowCount()) && (column + 1 == table.getColumnCount())) {
oldTabAction.actionPerformed(e);
if (act != null) {
act.actionPerformed(e);
}
table.changeSelection(row + 1, 0, false, false);
return;
}
oldTabAction.actionPerformed(e);
while (!table.isCellEditable(row, column)) {
oldTabAction.actionPerformed(e);
row = table.getSelectedRow();
column = table.getSelectedColumn();
if (row == originalRow && column == originalColumn) {
break;
}
}
}
};
tb.getActionMap().put(im.get(tab), tabAction);
}
}
Exemplo de uso:
Util.controlEnterTable(myTableInstance, new AbstractAction() {
public void actionPerformed(ActionEvent e) {
addItemTable();
}
}
É isso aí, esta é uma forma simples de implementar este recurso.
Nenhum comentário:
Postar um comentário