tags:

views:

101

answers:

3

I need to add dynamicaly Components to JPanel, but if i make just add(Component) then component doesn't appears, if i make then JPanel.revalidate(); then it appears, but JPanel blinks, can I make it more fine, without blinking?

Hm, i have found solution,just after add(component); i have write component.repaint(); and it works, but now there is another Problem with Window resizing, if i resize window then all my added components disapeard!!!

A: 

Might be a better idea to add the components on initialization and hide them, making them visible when needed.

Use the method Component.setVisible(boolean b) so show and hide components.

Edit:

I just tried a simple test class where I added random components to the main JFrame and it worked fine.

Try calling JFrame.pack() following JPanel.revalidate().

If this does not make a difference could you post some of your code where you add the dynamic components?

Another Edit:

Make your main component Implement the ComponentListener interface and implement the componentResized(ComponentEvent e) method to call JFrame.pack().

Gordon
No, because at moment of initialization i don't now which component will be added :(
Le_Coeur
A: 

if you add a new component you have to call revalidate.

Example:

panel.add(new JButton(...), ...); panel.revalidate();

Make sure you're calling this from within the EDT.

If it still flickers have a look at panel.setDoubleBuffered.

Hope that helps, even though example code from your side would be nice to actually see the effect you are describing.

Regards

stryba
+1  A: 

This is basic, but you should make sure each component is

1) added from the EDT (see SwingUtilities.invokeLater())

2) added only once per instance

louisgab