Adding JTextField to a JPanel and showing them
- by Davide Gualano
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.