2013-07-10 60 views

回答

6

答案很簡單:找一個綁定到標籤的動作,把它包裝成一個自定義操作委託給只如果不是在最後一個單元格,並替換爲您定製的實行原來的動作原件。

在代碼:

KeyStroke keyStroke = KeyStroke.getKeyStroke("TAB"); 
Object actionKey = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) 
     .get(keyStroke); 
final Action action = table.getActionMap().get(actionKey); 
Action wrapper = new AbstractAction() { 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     JTable table = (JTable) e.getSource(); 
     int lastRow = table.getRowCount() - 1; 
     int lastColumn = table.getColumnCount() -1; 
     if (table.getSelectionModel().getLeadSelectionIndex() == lastRow 
       && table.getColumnModel().getSelectionModel() 
         .getLeadSelectionIndex() == lastColumn) { 
       return; 
     } 
     action.actionPerformed(e); 
    } 

}; 
table.getActionMap().put(actionKey, wrapper); 
相關問題