JTable
中的默认行为似乎是,如果我到达列的最后一行并点击返回,我将被带到下一列的第一行。有办法避免这种情况吗?请建议一种方法,我可以留在同一列的最后一行。我还想避免一种情况,我被带到下一列,然后检测到并返回到前一列,因为我有一些与之关联的监听器。
任何帮助表示赞赏。
答案 0 :(得分:4)
要更改任何导航行为,请使用您自己的导航操作替换默认导航操作。最好通过包装默认值:有条件地执行默认或自定义的东西。像
这样的东西 Object key = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
.get(KeyStroke.getKeyStroke("ENTER"));
final Action action = table.getActionMap().get(key);
Action custom = new AbstractAction("wrap") {
@Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectionModel().getLeadSelectionIndex();
if (row == table.getRowCount() - 1) {
// do custom stuff
// return if default shouldn't happen or call default after
return;
}
action.actionPerformed(e);
}
};
table.getActionMap().put(key, custom);