wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes How to remove/replace Swing old component with new one. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to remove/replace Swing old component with new one." Watch "How to remove/replace Swing old component with new one." New topic
Author

How to remove/replace Swing old component with new one.

Fandy Akhmad
Ranch Hand

Joined: Nov 26, 2011
Posts: 32

Hello, master
I made a dynamic JPanel to hold the picture, where the picture came from the selected user ID in the JTable.

For example: When I select ID 1 in the JTable, it will come out images A, B and C. But when I select ID 2 in JTable, that comes out is a picture of D, E and F
I made ​​it using a loop to make the JPanels that contain images A, B, C and so on.

The problem is, when I select ID 1 it display A, B and C image and create a JPanel object A, B and C or whatever java sequence object name. When I chose ID 2 it shows D, E and F and JPanel objects D, E and F are created, I would like object A, B and C is removed / replaced / destroyed. So that the view in Application does not increase the number of JPanels but replacing the previous created JPanel.

This is the code that I created to display the dynamic JPanel :


Please help me, Thank you
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

To remove use Container#remove(Component child)
To add use Container#add(Component child)
You will need to also call revalidate as well as repaint to ensure the UI refreshes after any add/remove operation.

Instead of adding/removing panels, you might also want to considering refreshing the image or using a CardLayout


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2202
    
    7
Don't keep creating components. If you want to change the image displayed then just use a JLabel to display the image. Then you can use label.setIcon(...) when you want to change the displayed ImageIcon.
Fandy Akhmad
Ranch Hand

Joined: Nov 26, 2011
Posts: 32

Maneesh Godbole wrote:To remove use Container#remove(Component child)
To add use Container#add(Component child)
You will need to also call revalidate as well as repaint to ensure the UI refreshes after any add/remove operation.

Instead of adding/removing panels, you might also want to considering refreshing the image or using a CardLayout


Because my data will be large, it's a patient data in Hospital Information System. Impossible to use CardLayout i think.
 
Don't get me started about those stupid light bulbs.
 
subject: How to remove/replace Swing old component with new one.