Results 1 to 5 of 5
- 05-20-2012, 02:21 AM #1
Member
- Join Date
- May 2012
- Posts
- 15
- Rep Power
- 0
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
Any help would be appreciated, also if I need to post any more info to be helped just let me know.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()); }Last edited by DarrylBurke; 05-20-2012 at 06:17 AM.
- 05-20-2012, 02:51 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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.
- 05-20-2012, 03:04 AM #3
Member
- Join Date
- May 2012
- Posts
- 15
- Rep Power
- 0
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
- 05-20-2012, 06:18 AM #4
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-20-2012, 06:28 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Need help changing image of JPanel
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.I would still love to know what was going on previously
Similar Threads
-
error during changing the color of paintComponent of my JPanel
By noobplus in forum AWT / SwingReplies: 8Last Post: 04-07-2012, 11:38 PM -
Changing from JDesktop to JPanel contentPane
By h4xnoodle in forum AWT / SwingReplies: 2Last Post: 06-21-2011, 05:51 AM -
Changing an Applet to a JPanel
By xSkittlesx in forum Java AppletsReplies: 2Last Post: 05-31-2010, 03:41 PM -
Dynamically changing JPanel in a JScrollPane - incorrect sizing
By david.stefka in forum AWT / SwingReplies: 3Last Post: 02-06-2010, 03:28 PM -
Changing JPanel sizes
By Basit in forum Java AppletsReplies: 2Last Post: 08-12-2009, 06:48 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks