2017-02-09 85 views
0

我有一個JTabbedPane,它有4個選項卡。每個選項卡都有一個JTable。我試圖爲JTabbedPane設計一個自定義的FocusTraversalPolicy,這樣我可以在我關注表格的最後一個單元格時按下標籤頁將我帶到下一個選項卡。我搜查了很多,但沒有找到可以與之合作的具體內容。JTabbedPane FocusTraversalPolicy

tabbedPane.setFocusCycleRoot(true); 
tabbedPane.setFocusTraversalPolicyProvider(true); 
tabbedPane.setFocusTraversalPolicy(new MyPanelFocusTraversalPolicy(tabbedTables)); 

以下是我的自定義焦點遍歷策略。

private class MyPanelFocusTraversalPolicy extends FocusTraversalPolicy{ 
     private Vector<Component> order; 
     public StateWithholdingPanelFocusTraversalPolicy(List<CopyWorkerSingleTablePanel> table){ 
      this.order = new Vector<Component>(table.size()); 
      this.order.addAll(table); 
     } 
     @Override 
     public Component getComponentAfter(Container aContainer, Component aComponent) { 
      return order.get(order.indexOf(aComponent)+1); 
     } 

     @Override 
     public Component getComponentBefore(Container aContainer, Component aComponent) { 
      return order.get(order.indexOf(aComponent)-1); 
     } 

     @Override 
     public Component getFirstComponent(Container aContainer) { 
      return order.get(1); 
     } 

     @Override 
     public Component getLastComponent(Container aContainer) { 
      return order.lastElement(); 
     } 

     @Override 
     public Component getDefaultComponent(Container aContainer) { 
      return order.get(0); 
     } 
    } 

而且,這些JTable中被實例化別的地方,我已經覆蓋Tab鍵綁定關注下一個組件上最後一個單元格時。我已經做到了,以便在其他屏幕上也可以使用。

KeyStroke keyStroke = KeyStroke.getKeyStroke("TAB"); 
      Object actionKey = copyWorkerTable.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).get(keyStroke); 
      final Action action= table.getActionMap().get(actionKey); 
      Action wrapper = new AbstractAction() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        if(table.getSelectedRow() >= table.getRowCount()-1 && table.getSelectedColumn() >= table.getColumnCount()-1) {      
         if(!table.isCellEditable(table.getSelectedRow(),table.getSelectedColumn())){ 
          table.transferFocus(); 
         }else{ 
          table.getCellEditor(table.getSelectedRow(),table.getSelectedColumn()).stopCellEditing(); 
          table.transferFocus(); 
         } 

        }else{ 
         action.actionPerformed(e); 
        } 
       } 
      }; 

回答

0

如果我明白你爲什麼不在你的動作處理程序中使用類似這樣的內容在標籤之間導航?

if (tabbedPane.getSelectedIndex() == (tabbedPane.getComponentCount() - 1)) 
    { 

     tabbedPane.setSelectedIndex(0); 

    } else 
    { 

     tabbedPane.setSelectedIndex(tabbedPane.getSelectedIndex() + 1); 

    }