File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes removing one component and replacing it with another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "removing one component and replacing it with another" Watch "removing one component and replacing it with another" New topic

removing one component and replacing it with another

J Chandler
Ranch Hand

Joined: May 17, 2003
Posts: 32
Good morning

I have a lil' problem, hope someone can help out....

I have a JPanel (tablePanel) with 4 JLabels placed north, south, east and west (JLabels create a border).

In the centre of tablePanel, I have added JScrollPane (tableScroll) - this holds a JTable.

All good so far! Display shows a splendid scrollable JTable with a smart black border.

My "more details" JButton requires that tableScroll be removed from the centre, taking the JTable with it, and is replaced by a JPanel component.
To accomplish this task, I used :

When "more details" button is clicked, nothing happens until I either maximize or minimize the frame, whereupon the table dissappears and the panel is displayed as planned.

When I want to return to the JTable view, another button event causes only the header to display until the frame is maximised or minimized, then the whole JTable appears.

Does anyone know why this happens? - I am puzzled.

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

tablePanel.repaint() should do the trick after you have removed and added your components.

GenRocket - Experts at Building Test Data
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
See this post
Cathy Song
Ranch Hand

Joined: Jul 01, 2003
Posts: 92

If tablePanel.repaint() does not work...trying doing the following:


Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Whenever you add/remove components you may have to ask swing to do a new layout. You do this with either the JComponent method revalidate or the Container method validate. Sometimes you may have to follow this with a call to repaint. You just have to play with this until you get it to work with the minimum number of calls.

The main clue for the need to validate the layout is that nothing appears until you resize the container.
J Chandler
Ranch Hand

Joined: May 17, 2003
Posts: 32
Gregg, David, Cathy and Craig,

thank you all for your comments.

I have altered my code and now everything is working beautifully -


Happy J
Haroldo Level

Joined: Feb 17, 2005
Posts: 6
Hi guys, I have a similar problem. I'm trying to remove the whole content of a JPanel that I have polacved inside an applet. I tried everything, even what you posted and when I get to the part when I modify the panel the whole applet just crashes. I hope you hasve a solution for this. thanks in advance
I agree. Here's the link:
subject: removing one component and replacing it with another
It's not a secret anymore!