Results 1 to 10 of 10
  1. #1
    fanjavaid is offline Member
    Join Date
    Dec 2012
    Location
    Jakarta, Indonesia
    Posts
    23
    Rep Power
    0

    Default How to remove/replace Swing component object which the new one

    Hello :)
    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 :
    Java Code:
    public void getStreamData(PanelEntry view, String path) {
            //String path = "data/images/";
            String files = null;
            
            int j = view.getTbPatientData().getSelectedRow();
            String noMr = (String) view.getTbPatientData().getValueAt(j, 0);
            
            File folder = new File(path);
            File [] listOfFiles = folder.listFiles();
            
            System.out.println(path+" = "+listOfFiles.length);
            
            int maxFiles = listOfFiles.length;
            int maxView  = 15;
            
            for (int i = 0; i < listOfFiles.length; i++) {
                if(listOfFiles[i].isFile()) {
                    files = listOfFiles[i].getName();
                    
                    if(files.endsWith(".jpg") || files.endsWith(".JPG")) {
                        final String newPath = path+files;
    
                        try {
                            File showFile = new File(newPath);
                            Image image = ImageIO.read(showFile);
    
                            JPanel panel = new BackgroundImageRounded(showFile);
                            panel.setLayout(null);
    
                            view.getPanelStreamData().add(panel); //This will add dynamic JPanel that have been created
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    Please help me, thank you :)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,001
    Rep Power
    10

    Default Re: How to remove/replace Swing component object which the new one

    You should look into CardLayout.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,122
    Rep Power
    20

    Default Re: How to remove/replace Swing component object which the new one

    Stick to the one JPanel and simply change its contents.
    Don't create a new one each time.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    fanjavaid is offline Member
    Join Date
    Dec 2012
    Location
    Jakarta, Indonesia
    Posts
    23
    Rep Power
    0

    Default Re: How to remove/replace Swing component object which the new one

    Quote Originally Posted by KevinWorkman View Post
    You should look into CardLayout.
    My Data will increase anytime. And it will make card layout, i think it not efficient.
    Thanks for your idea ^^

  5. #5
    fanjavaid is offline Member
    Join Date
    Dec 2012
    Location
    Jakarta, Indonesia
    Posts
    23
    Rep Power
    0

    Default Re: How to remove/replace Swing component object which the new one

    Quote Originally Posted by Tolls View Post
    Stick to the one JPanel and simply change its contents.
    Don't create a new one each time.
    I create JPanel object for my each image, so i have 5 images and to place them i must create 5 JPanels to.
    Any idea?
    Thanks :)

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,122
    Rep Power
    20

    Default Re: How to remove/replace Swing component object which the new one

    You need to clear it then.
    You should have a Panel that displays whatever based on the selected row.
    It seems each row has several things it should display.
    So that Panel should display those things (presumably these BackgroundImageRounded panels).
    Before you add your BackgroundImageRounded panes to it, remove all (there's even a method called that) the components in the panel, to ensure all the previous images are removed.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    fanjavaid is offline Member
    Join Date
    Dec 2012
    Location
    Jakarta, Indonesia
    Posts
    23
    Rep Power
    0

    Default Re: How to remove/replace Swing component object which the new one

    Quote Originally Posted by Tolls View Post
    You need to clear it then.
    You should have a Panel that displays whatever based on the selected row.
    It seems each row has several things it should display.
    So that Panel should display those things (presumably these BackgroundImageRounded panels).
    Before you add your BackgroundImageRounded panes to it, remove all (there's even a method called that) the components in the panel, to ensure all the previous images are removed.
    Yes that exactly i want, but how i can do this? I don't know how to clear previoues Component objects.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: How to remove/replace Swing component object which the new one

    Quote Originally Posted by fanjavaid View Post
    I don't know how to clear previoues Component objects.
    Tolls just told you how.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    fanjavaid is offline Member
    Join Date
    Dec 2012
    Location
    Jakarta, Indonesia
    Posts
    23
    Rep Power
    0

    Default Re: How to remove/replace Swing component object which the new one

    Quote Originally Posted by DarrylBurke View Post
    Tolls just told you how.

    db
    Sorry, i mean what the Java method to remove Component object,
    Thank you

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,122
    Rep Power
    20

    Default Re: How to remove/replace Swing component object which the new one

    To qute myself:
    "...remove all (there's even a method called that) the components in the panel..."

    And if that doesn't help then there's the API, which you should be looking at anyway.
    It's inherited from Container.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replace/remove character in a String
    By earlyMorningJAVAtime in forum New To Java
    Replies: 1
    Last Post: 02-17-2012, 04:34 AM
  2. Replies: 3
    Last Post: 12-10-2009, 04:07 PM
  3. how to remove component from panel
    By itaipee in forum AWT / Swing
    Replies: 3
    Last Post: 04-28-2009, 01:50 PM
  4. how to remove all component in container(Jpanel)
    By SaYuNaRa in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 05:22 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •