Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Adding and removing components in a JPanel.

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi,

I've got a GUI (JFrame) in which I've added a JPanel to which I want to add and remove components mainly a JLabel, JTextField and JButton depending on a choice made by the User in a JMenu.

I have a listener that removes the old components and adds the new ones when the JMenu is selected. However, the components disappear, but the new ones aren't added.

What I found I had to was to call.


After adding the new components in order for the them to be shown. What I'm wondering is whether this is the correct way to get the components to be shown and if not what is?

Cheers,
Matt.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hi Matt,

i got the same problem in my project and now it is solved. Be sure about removing previous controls and refreshing the panel. (are you adding controls to panel or new panel to frame? )

see this snippet code


Hope This Helps U
All The Best
[ April 07, 2005: Message edited by: sasi kala ]
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
The repaint method is automatically called(by the repaint manager) on the screen only in case of resizing/relocating/changing the layout of a panel. If you add a component then it does not get displayed on its own. You'll have to explicitly call the repaint/revalidate method to ensure that the component appears on the screen. There is nothing wrong with this.

From my understanding, calling revalidate is a better option rather than calling repaint, although they both do the same job.
[ April 07, 2005: Message edited by: Ashish Chopra ]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I tried just calling revalidate() and there was a little bit of the GUI that didn't get repainted. It was an area not covered by the JMenuItem. Using both calls fixed this problem. If I only call repaint() then the new components don't appear. As its only a smallish GUI I'll leave both the calls in.

Thanks for your help with this.

Additional:
What makes it stranger is that the problem occurs as described above with the Metal Look & Feel, but if I use the Windows L&F then using just revalidate() works fine.
[ April 07, 2005: Message edited by: Matt Garner ]
 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I suggest that you do this
to remove:
jpanel.remove(component); //remove component from your jpanel in this case i used jpanel
jpanel.revalidate();
jframe.repaint();//repaint a JFrame jframe in this case

to add:
jpanel.add(component); //add component to jpanel in this case i used jpanel
jpanel.revalidate();
jframe.repaint();//repaint a JFrame jframe in this case

//my reson for diong this is because if you intend to remove then add again then remove this is best
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Christopher,

Welcome to the Ranch.
Did you notice the original thread is 4 years old? I doubt if the original poster is still waiting for the solution
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic