Question

J'ai un JFrame. Ce JFrame contient un JButton. Je clique sur le JButton et 10 JTextFields sont créés.

le problème: Je ne peux pas les voir jusqu'à ce que « je force un repeindre () » par le redimensionnement de la fenêtre. Alors seulement je vois les JTextFields créés.

CODE:

JPanel points = new JPanel();

//Creating the JTextFields:
for (int i=0; i<10; i++) {
    JTextField textField = new JTextField();
    points.add(textField);
}

repaint();
this.repaint();
super.repaint();
points.repaint();

MERCI - après la boucle, je viens d'appeler points.validate () et il a travaillé ...

Était-ce utile?

La solution

Container.add API docs sayeth:

Note: If a component has been added to a container that has been displayed, validate must be called on that container to display the new component. If multiple components are being added, you can improve efficiency by calling validate only once, after all the components have been added.

It's obscure and not very clever, but it's the rules. It may be better to call JComponent.revalidate

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top