自定义焦点遍历策略出错

时间:2012-08-22 10:02:08

标签: java focus

我正在创建java桌面应用程序。我想更改面板组件的Tab键顺序。我创建自己的焦点遍历政策。在面板中我使用两个日期选择器字段。其他组件准确获得焦点,但datechooser字段没有获得焦点。我试过像

  List<Component> order=new LinkedList<>();
    order.add(pidcmb);
    order.add(locationtxt);
    order.add(chemicalnametxt);
    order.add(fromtxt);//datechooser
    order.add(totxt);//datechooser
    order.add(generate);
    order.add(cancel);
    newPolicy = new MyOwnFocusTraversalPolicy(order);
    parameter.setFocusTraversalPolicy(newPolicy);
     parameter.setFocusCycleRoot(true);

并创建一个类

class MyOwnFocusTraversalPolicy extends FocusTraversalPolicy
{

   List<Component> order;

        public MyOwnFocusTraversalPolicy(List<Component> order) {
            this.order = new LinkedList<Component>() {};
            this.order.addAll(order);
        }
    @Override
        public Component getComponentAfter(Container focusCycleRoot,
                                           Component aComponent)
        {
            int idx = (order.indexOf(aComponent) + 1) % order.size();
            return order.get(idx);
        }

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

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

    @Override
        public Component getLastComponent(Container focusCycleRoot) {
            return order.get(order.size());
        }

    @Override
        public Component getFirstComponent(Container focusCycleRoot) {
            return order.get(0);
        }

}

0 个答案:

没有答案