Question

I wrote a program using WindowBuilderPro and it works fine. However, I now want to reuse the 2 classes in a new project, but when I copy the first into the project, I get 2 errors from Eclipse. Here's an SSCCE demonstrating the problem:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Font;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;

public class ExampleFrame extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ExampleFrame frame = new ExampleFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ExampleFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        textField = new JTextField();
        textField.setBounds(106, 32, 74, 28);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel lblNewLabel = new JLabel("Box 1");
        lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
        lblNewLabel.setBounds(31, 39, 46, 14);
        contentPane.add(lblNewLabel);

        JLabel lblBox = new JLabel("Box 2");
        lblBox.setFont(new Font("Tahoma", Font.PLAIN, 18));
        lblBox.setBounds(31, 101, 46, 14);
        contentPane.add(lblBox);

        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(106, 94, 74, 28);
        contentPane.add(textField_1);
        contentPane.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{lblNewLabel, textField, lblBox, textField_1}));
    }
}

The 2 errors I get are as follows:

The import org.eclipse cannot be resolved

on the line

import org.eclipse.wb.swing.FocusTraversalOnArray;

and

FocusTraversalOnArray cannot be resolved to a type

at the line

contentPane.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{lblNewLabel, textField, lblBox, textField_1}));

These lines appear when one sets the tab order. Does anyone know how to overcome this please? It seems like quite an error considering the whole point of Java is to be able to reuse components.

Was it helpful?

Solution

I needed to add all the libraries I used in the old project to the build path of the new project.

OTHER TIPS

I had the same issue after copying a class which had been created by Window Builder. I found that I had been missing the following file:

\src\org\eclipse\wb\swinger\FocusTraversalOnArray.java

this seems to be the only file which Window Builder adds externally. I'll post the code here in case anybody needs it.

/*******************************************************************************
 * Copyright (c) 2011 Google, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.wb.swing;

import java.awt.Component;
import java.awt.Container;
import java.awt.FocusTraversalPolicy;

/**
 * Cyclic focus traversal policy based on array of components.
 * <p>
 * This class may be freely distributed as part of any application or plugin.
 * 
 * @author scheglov_ke
 */
public class FocusTraversalOnArray extends FocusTraversalPolicy {
    private final Component m_Components[];
    ////////////////////////////////////////////////////////////////////////////
    //
    // Constructor
    //
    ////////////////////////////////////////////////////////////////////////////
    public FocusTraversalOnArray(Component components[]) {
        m_Components = components;
    }
    ////////////////////////////////////////////////////////////////////////////
    //
    // Utilities
    //
    ////////////////////////////////////////////////////////////////////////////
    private int indexCycle(int index, int delta) {
        int size = m_Components.length;
        int next = (index + delta + size) % size;
        return next;
    }
    private Component cycle(Component currentComponent, int delta) {
        int index = -1;
        loop : for (int i = 0; i < m_Components.length; i++) {
            Component component = m_Components[i];
            for (Component c = currentComponent; c != null; c = c.getParent()) {
                if (component == c) {
                    index = i;
                    break loop;
                }
            }
        }
        // try to find enabled component in "delta" direction
        int initialIndex = index;
        while (true) {
            int newIndex = indexCycle(index, delta);
            if (newIndex == initialIndex) {
                break;
            }
            index = newIndex;
            //
            Component component = m_Components[newIndex];
            if (component.isEnabled() && component.isVisible() && component.isFocusable()) {
                return component;
            }
        }
        // not found
        return currentComponent;
    }
    ////////////////////////////////////////////////////////////////////////////
    //
    // FocusTraversalPolicy
    //
    ////////////////////////////////////////////////////////////////////////////
    public Component getComponentAfter(Container container, Component component) {
        return cycle(component, 1);
    }
    public Component getComponentBefore(Container container, Component component) {
        return cycle(component, -1);
    }
    public Component getFirstComponent(Container container) {
        return m_Components[0];
    }
    public Component getLastComponent(Container container) {
        return m_Components[m_Components.length - 1];
    }
    public Component getDefaultComponent(Container container) {
        return getFirstComponent(container);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top