Adding JTextField to a JPanel and showing them

Posted by Davide Gualano on Stack Overflow See other posts from Stack Overflow or by Davide Gualano
Published on 2009-02-13T14:34:17Z Indexed on 2010/06/12 4:12 UTC
Read the original article Hit count: 317

Filed under:
|
|
|

I'm building a little app using Java and Swing in NetBeans. Using NetBeans design window, I created a JFrame with a JPanel inside.

Now I want to dynamically add some jTextFields to the JPanel. I wrote something like that:

Vector textFieldsVector = new Vector();
JTextField tf;
int i = 0;
while (i < 3) {
    tf = new JTextField();
    textFieldVector.add(tf);
    myPanel.add(tf); //myPanel is the JPanel where I want to put the JTextFields
    i++;
}
myPanel.validate();
myPanel.repaint();

But nothing happens: when I run the app, the JFrame shows with the JPanel inside, but the JTextFields don't.

I'm a total newbie in writing graphical Java apps, so I'm surely missing something very simple, but I can't see what.

© Stack Overflow or respective owner

Related posts about java

Related posts about swing