Results 1 to 5 of 5
  1. #1
    goatjugsoup is offline Member
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Need help changing image of JPanel

    So basically I have an app I am building that when it loads up it displays an image in a jpanel, when buttons are pressed the image is supposed to change. Something weird is happening when the change is made though, I remember seeing something like this before but I can't remember where.

    This is what it looks like when it loads up


    After a button is pressed to change the picture, this is what happens


    The Code I think may be responsible is

    Java Code:
    	private void setImage(String picPath)
    	{
    		jPanelImages = new DisplayImage(new BorderLayout(), picPath);
    		getContentPane().remove(jPanelImages);
    		this.getContentPane().add(jPanelImages, new Constraints(new Leading(182, 579, 10, 10), new Leading(12, 325, 10, 10)));
    		this.getContentPane().doLayout();
    		this.update(getGraphics());	
    	}
    Any help would be appreciated, also if I need to post any more info to be helped just let me know.
    Last edited by DarrylBurke; 05-20-2012 at 06:17 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Need help changing image of JPanel

    What do you intend the remove() call to do? Note that the value of jPanelImages is a reference to a newly created DisplayImage instance. Because it is newly hatched it certainly will *not* be contained in the content pane, so the remove() method will - I would guess - do nothing.

    Then you add() the newly created DisplayImage instance resulting in the appearance you observe: two houses.

    -----

    An aside on the blurred buttons...

    We are seeing quite a lot of this as students post code while (quite reasonably) not wanting their question to be associated with some particular home work problem. A better approach, I think, is to construct a SSCCE which illustrates the problem. The problem does not really involve the house image (or the particular properties of the DisplayImage class), any old JLabel would have done just as well. A SSCCE in this case would be complete code (compilable and runnable) without the buttons and with JLabel instances standing in for the DisplayImage instances.

    Such a SSCCE could be thoroughly and precisely discussed without reference to your specific homework problem. (Thereby stopping anyone from stealing your work, and also keeping you within any reasonable "own work" requirements). Also while separating a problem from the obscuring detail of its context is *not* easy, it does shine light on the problem from which understanding is more likely to arise.

  3. #3
    goatjugsoup is offline Member
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Re: Need help changing image of JPanel

    Line 3 should really be below line 4. I changed the order of the lines in my code and that didn't fix the problem. jPanelImages is the panel that in the first picture contains the house with the blue roof and I intended for the setImage function I created to be able to change its image when a button is pressed. Plus, your explanation doesn't help me to understand why the menu was duplicated. Thanks for the reply though

    *edit*

    Hmm, I still have no idea what was causing that issue BUT I figured an alternate way to get what I wanted done, don't know why it took this long but I stopped trying to change the image in the form class and did it in the DisplayImage class and it worked fine. I would still love to know what was going on previously but for now the problem is solved.

    Sidenote: what do I do to a topic when the problem inside is solved? I know some forums tell you to add something to the title.
    Last edited by goatjugsoup; 05-20-2012 at 03:13 AM. Reason: avoid double posting

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: Need help changing image of JPanel

    You edit the first post, click 'Go Advanced' and select the prefix [SOLVED] then Save Changes.

    I've done it for you this time.

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

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Need help changing image of JPanel

    I would still love to know what was going on previously
    My (in the absence of code) guess would be that if you ended up with two houses then, whereever you were calling remove(), you were removing something that was not *directly* contained within the content pane. This can be checked by looping through the contained components.

Similar Threads

  1. Replies: 8
    Last Post: 04-07-2012, 11:38 PM
  2. Changing from JDesktop to JPanel contentPane
    By h4xnoodle in forum AWT / Swing
    Replies: 2
    Last Post: 06-21-2011, 05:51 AM
  3. Changing an Applet to a JPanel
    By xSkittlesx in forum Java Applets
    Replies: 2
    Last Post: 05-31-2010, 03:41 PM
  4. Replies: 3
    Last Post: 02-06-2010, 03:28 PM
  5. Changing JPanel sizes
    By Basit in forum Java Applets
    Replies: 2
    Last Post: 08-12-2009, 06:48 PM

Posting Permissions

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