• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Fandy Akhmad
Ranch Hand
Posts: 32
Java Linux MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10976
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Camick
Ranch Hand
Posts: 2476
8
  • 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
Java Linux MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic