焦点遍历以跳过不可聚焦的字段

时间:2013-09-17 15:51:07

标签: java netbeans focus

我在屏幕上有很多文本字段(超过代码显示)。

private Component[] focusList;
focusList = new Component[]{
        txtArcustNo,
        txtBillTo,
        txtAcctNo,
        txtName,
        txtAddress,
        txtAddress2,
        txtAddress3,
        txtAddress4,
        txtContact,
        txtContact2,
        txtEmail,
        txtWebsite,
        txtPhone,
        txtPhone1Ext,
        txtPhone2,
        txtPhone2Ext,
        txtFax1,
        txtFax1Ext,
        txtFax2,
        txtFax2Ext,
    };
    focusTraversal = new WWFocusTraversalPolicy(focusList);
    pnlBase.setFocusTraversalPolicy(focusTraversal);

在某个条件下(即用户在屏幕上标记了一个复选框)我想从焦点中删除txtPhone2和txtPhone2Ext。但是,如果我在onClick事件中明确说明将这两个字段设置为Focusable false,那么当我在字段中进行选项卡时,我的光标只会停留在txtPhone1Ext并且不会转到Fax1字段,跳过我设置的字段不可聚焦。

想知道我是否遗漏了什么。任何提示/建议表示赞赏。如果重要的话,我正在使用NetBeans IDE。

1 个答案:

答案 0 :(得分:4)

阅读How to Use the Focus Subsystem上的Swing教程。它提供了如何创建自定义焦点遍历策略的示例。不幸的是,这个例子不是很完整,因为它也有你的代码所遇到的同样问题,因为它总是假设一个组件能够有焦点。

我修改了示例中的类以确保组件是可聚焦的:

public static class MyOwnFocusTraversalPolicy extends FocusTraversalPolicy
{
    Vector<Component> order;

    public MyOwnFocusTraversalPolicy(Vector<Component> order)
    {
        this.order = new Vector<Component>(order.size());
        this.order.addAll(order);
    }

    public Component getComponentAfter(Container focusCycleRoot,
                                       Component aComponent)
    {
//      int idx = (order.indexOf(aComponent) + 1) % order.size();
//      return order.get(idx);

        int idx = order.indexOf(aComponent);

        for (int i = 0; i < order.size(); i++)
        {
            idx = (idx + 1) % order.size();
            Component next = order.get(idx);

            if (canBeFocusOwner(next)) return next;
        }

        return null;
    }

    public Component getComponentBefore(Container focusCycleRoot,
                                        Component aComponent)
    {
/*
        int idx = order.indexOf(aComponent) - 1;
        if (idx < 0) {
            idx = order.size() - 1;
        }
        return order.get(idx);
*/
        int idx = order.indexOf(aComponent);

        for (int i = 0; i < order.size(); i++)
        {
            idx = (idx - 1);

            if (idx < 0)
            {
                idx = order.size() - 1;
            }

            Component previous = order.get(idx);

            if (canBeFocusOwner(previous)) return previous;
        }

        return null;
    }

    public Component getDefaultComponent(Container focusCycleRoot) {
//      return order.get(0);
        return getFirstComponent( focusCycleRoot );
    }

    public Component getLastComponent(Container focusCycleRoot) {
//      return order.lastElement();

        Component c = order.lastElement();

        if (canBeFocusOwner(c))
            return c;
        else
            return getComponentBefore(focusCycleRoot, c);
    }

    public Component getFirstComponent(Container focusCycleRoot)
    {
//      return order.get(0);

        Component c = order.get(0);

        if (canBeFocusOwner(c))
            return c;
        else
            return getComponentAfter(focusCycleRoot, c);
    }

    private boolean canBeFocusOwner(Component c)
    {
        if (c.isEnabled() && c.isDisplayable() && c.isVisible() && c.isFocusable())
        {
            return true;
        }

        return false;
    }

}