0

I have three panels inside one panel(which is inside JFrame).In the middle panel there are some 30 JButtons.Now I want to add JButton dynamically inside the middle panel on click of a leftmost panel JButton.I am doing following

imagePanel.add(newBtn);
imagePanel.revalidate();
outermostPanel.revalidate();

But,on doing this nothing happens.I don't want to use repaint as I will re-draw the complete middle panel and I am only left the new JButton,the previously added 30 JButtons are gone.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Raj Trivedi
  • 557
  • 7
  • 18
  • 1
    See [this answer](http://stackoverflow.com/a/5630271/418556) for tips on adding components dynamically. Clicking the `Add Another Label` button will add labels on demand. – Andrew Thompson Sep 15 '15 at 14:07
  • Has imagePanel been added to the component tree? – Dávid Horváth Sep 15 '15 at 14:18
  • [basics stuff](http://stackoverflow.com/a/6989230/714968) – mKorbel Sep 15 '15 at 14:21
  • @David Horvath-imagePanel is inside a JScrollPane – Raj Trivedi Sep 15 '15 at 14:22
  • It worked after setting setButtonBounds(oldbeacons); imagePanel.add(newBtn); imagePanel.revalidate(); imagePanel.repaint(); But,why do we need to re-draw the previously created components? – Raj Trivedi Sep 15 '15 at 14:35
  • Because the position of the components may be adjusted by the layout manager after the component has been added. For example if you are using a FlowLayout and the components are "centered" on the panel, adding one more component will cause all components to shift. – camickr Sep 15 '15 at 14:56

0 Answers0