Domanda

Ho un JFrame. Questo JFrame contiene un JButton. Clicco il JButton e 10 JTextFields vengono creati.

il problema: Non riesco a vedere loro fino a quando "ho forzare un repaint ()" con il ridimensionamento della finestra. Solo allora vedo le JTextFields creati.

codice:

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();

GRAZIE - dopo il ciclo for, ho appena chiamato points.validate () e ha funzionato ...

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top