开发者

JTable - Should not move to next column after hitting return on end of a column

开发者 https://www.devze.com 2023-04-12 18:05 出处:网络
The default behaviour in a JTable seems to be that if I reach the last row of a column and hit return, I am taken to the first row of the next column. Is there a way to avoid this? Please suggest a wa

The default behaviour in a JTable seems to be that if I reach the last row of a column and hit return, I am taken to the first row of the next column. Is there a way to avoid this? Please suggest a way that I could stay at the last开发者_JAVA百科 row of the same column. I also want to avoid a situation where I am taken to the next column and then detect that and go back to the previous one, because I have some listeners associated with it. Any help is appreciated.


to change any of the navigational behaviour, replace the default navigational actions with your own. Best by wrapping the defaults: conditionally either do the default or your custom stuff. Something like

    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);
0

精彩评论

暂无评论...
验证码 换一张
取 消