1

I have a problem with FEST:

I have four JtextFields in a JDialog. How can I obtain a certain JTextField if I have four JTextFields, where attributes name, text, and visibility are undefined or null.

public class Form1 {
    public static void main(String[] args) {
        JTextField tf1 = new JTextField();
        JTextField tf2 = new JTextField();
        JTextField tf3 = new JTextField();
        JTextField tf4 = new JTextField();

        tf1.setPreferredSize(tf1.getPreferredSize());
        tf1.setText("");
        tf2.setPreferredSize(tf2.getPreferredSize());
        tf2.setText("");
        tf3.setPreferredSize(tf3.getPreferredSize());
        tf3.setText("");
        tf4.setPreferredSize(tf4.getPreferredSize());
        tf4.setText("");

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(1, 1));
        frame.add(tf1);
        frame.add(tf2);
        frame.add(tf3);
        frame.add(tf4);
        frame.setSize(300, 85);
        frame.setVisible(true);
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Guru_1010
  • 574
  • 7
  • 22
  • afair, you have to set the name property so that the component can be found – kleopatra Mar 01 '12 at 13:19
  • I haven't access to source code – Guru_1010 Mar 01 '12 at 13:43
  • 1
    hmm ... then you'll probably need to implement a custom Matcher (don't nail me on the name, it's been a while and writing this is from the top of my head ;-) You might consider asking your question on the FEST mailinglist, the lurkers there tend to respond rather quickly – kleopatra Mar 01 '12 at 13:47

1 Answers1

1

Here is a method to run setName for all JComponent instances contained inside an object using Reflection. It will show stack overflow error if there are circular referencing inside the object.

public static void assignComponentNames(Object obj) {

    try {

        Method getComponentsMethod = obj.getClass().getMethod("getComponents", new Class[]{});

        if (null != getComponentsMethod){
            try {
                for (Component component : (Component[])(getComponentsMethod.invoke(obj))){
                    assignComponentNames(component);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (InvocationTargetException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
        else {
            System.out.println(obj.toString());
        }
    } catch (NoSuchMethodException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }

    for (Field field : obj.getClass().getDeclaredFields()) {
        field.setAccessible(true);

        String fieldName = field.getName(); //this is a different name, the reflection level name
        Object fieldValue = null;

        try {
            fieldValue = field.get(obj);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        if (null == fieldValue) {
            continue;
        }

        if (fieldValue instanceof JComponent) {

            String currentComponentNameForFieldValue = ((JComponent) fieldValue).getName();

            if (null == currentComponentNameForFieldValue){
                System.out.println("null component name");
                ((JComponent) fieldValue).setName(fieldName);  //this sets the name specially for JComponent
            }
        }
        else if(fieldValue instanceof Collection){

            for (Object subObject : ((Collection)fieldValue).toArray()) {

                assignComponentNames(subObject);
            }
        }

    }
}
Intel
  • 141
  • 1
  • 9