Java GUI repintar problema ()?
-
21-08-2019 - |
Pregunta
Tengo un JFrame. Este JFrame contiene un JButton. Hacer clic en el JButton y 10 JTextFields se crean.
el problema: No puedo verlos hasta que "fuerzo un repintado ()" cambiando el tamaño de la ventana. Sólo entonces veo los JTextFields creadas.
CÓDIGO:
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();
GRACIAS - después de que el bucle for, acabo de llamar a points.validate () y funcionó ...
Solución
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