Reputation: 1541
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!!!
Upvotes: 1
Views: 673
Reputation: 2029
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.
Upvotes: 0
Reputation: 2414
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
Upvotes: 3
Reputation: 4843
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().
Upvotes: 0