File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Adding and removing components in a JPanel. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply locked New topic
Author

Adding and removing components in a JPanel.

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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.
sasi kala
Ranch Hand

Joined: Dec 17, 2004
Posts: 68
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 ]
Ashish Chopra
Ranch Hand

Joined: Nov 30, 2004
Posts: 134
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 ]

Quis Custodiet Ipsos Custodes<br /> <br />My blog: <a href="http://www.coherentrambling.blogspot.com" target="_blank" rel="nofollow">http://www.coherentrambling.blogspot.com</a>
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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 ]
Christopher Thomas
Greenhorn

Joined: Aug 01, 2009
Posts: 1
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
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10170
    
    8

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


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
 
subject: Adding and removing components in a JPanel.
 
Similar Threads
Drop down menu
JMenuBar not using default alignment?
JMenu Construction problem
Multiple JMenuItem layout
Problem with JTree inside JScrollPane inside JPanel