سؤال

I have a JTable in which some columns are uneditable. I do that by overriding the isCellEditable method. I now want to make the cells in these columns un-selectable. if the user is using the tab key to go through the cells, I want to focus to "skip" these uneditable cells. Can you please tell me how this is done? Thanks.

هل كانت مفيدة؟

المحلول

All navigation behaviour is controlled by actions registered in the table's actionMap. So the way to go is to hook into the action that is bound to the tab key, implement a wrapper that invokes that action as often as needed and replace the original action with the wrapper.

A raw code snippet for skipping not-editable cells:

Object actionKey = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
    .get(KeyStroke.getKeyStroke("TAB")); 
final Action traverseAction = table.getActionMap().get(actionKey);
Action wrapper = new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
        traverseAction.actionPerformed(e);
        while(shouldRepeat((JTable) e.getSource())) {
            traverseAction.actionPerformed(e);
        }
    }

    private boolean shouldRepeat(JTable source) {
        int leadRow = source.getSelectionModel().getLeadSelectionIndex();
        int leadColumn = source.getColumnModel().getSelectionModel().getLeadSelectionIndex();
        return !source.isCellEditable(leadRow, leadColumn);
    }
};
table.getActionMap().put(actionKey, wrapper);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top