I have an interface:
Java Code:
public interface MyComponent {
    public void addToPanel(JPanel panel);
    public String[] getUserInput();
}
And two classes that implement MyComponent:
Java Code:
public class TextBox implements MyComponent{
    
    private JTextField[] textbox;

    public TextBox() {
        textbox = new JTextField[10];
        for (int i=0;i<textbox.length;i++) {
            textbox[i] = new JTextField();
        }
    }

     public void addToPanel(JPanel panel) {
        for (int i=0;i<textbox.length;i++) {
            panel.add(textbox[i]);
        }
    }

    public String[] getUserInput() {
         //..... code to return user input from JTextFields....
         // ...   return string[];
    }


}

public class CheckBox implements MyComponent{

    private JCheckBox[] checkbox;

    public CheckBox(List list) {
       checkbox = new JCheckBox[list.size()];
       for(int i=0;i<checkbox.length;i++) {
            checkbox[i] = new JCheckBox((String)list.get(i));
        }
    }

    public void addToPanel(JPanel panel) {
        for (int i=0;i<checkbox.length;i++) {
            panel.add(checkbox[i]);
        }
    }

    public String[] getUserInput() {
         //..... code to return user checks from JCheckBox[]....
         // ...   return string[];
    }
}
OK, now for the parts that I get confused on. (PLEASE correct me if I'm wrong or you see a better way!! Thankyou!)

I have a class the has a MyComponents variable. I've had to set up an initialize method that instantiates the MyComponent variable based on a passed parameter, but after that the MyComponent variable calls the methods and does not care whether it is textbox or checkbox... and if I want to add another class that creates something different, I think I can. As long as it implements the MyComponent interface AND I add it to the initializeMyComonent method, it should work within this class.
Java Code:
public class SearchPanel extends JPanel {

    private MyComponent component;

    public SearchPanel(int type) {
       //code to initialize components such as JPanel panel;
        initializeMyComponent(type);
        component.addToPanel(panel);

    }

    initializeMyComponent(int type) {
         if (type == 0) {   //for ease of posting (have other class for this)
             component = new TextBox();
         }
          else if (type == 1) {
               component = new CheckBox(list); // just assume you have the list
         }
    }

    public String[] getInputs() {
          String[] result = component.getUserInputs();
          return result;
     }
}
My questions: Is this a correct way to implement an interface? Is it similar to the double-dispatch pattern (see post link below)?

Many thanks in advance for any/all responses/critiques/links/etc!!
Chris


Assign Different Objects to Array - Jos had responded to this original post with an example of a Visitor interface, and I still had questions about it. :)