JTabbedPane FocusTraversalPolicy

时间:2017-02-09 15:43:33

标签: java swing jtable jtabbedpane

我有一个JTabbedPane有4个Tabs。每个Tab都有一个JTable。我正在尝试为JTabbedPane设计一个自定义的FocusTraversalPolicy,这样当我专注于桌子的最后一个单元格时,按Tab键会将我带到下一个标签。我经常搜索,但是找不到可以使用的特定内容。

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

1 个答案:

答案 0 :(得分:0)

如果我说对了你为什么不在你的动作处理程序中使用这样的东西来在标签之间导航?

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

        tabbedPane.setSelectedIndex(0);

    } else
    {

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

    }