JPanel trouble: replacing/ removing then adding

Im making a little game, so when you move, the character changes to move right. my problem is here, when the listener makes the call to my change() method, i didnt really know what to do and what i did do didnt work. first i just did "panel.removeAll()" then "panel.add(newImage)", but when i ran this, as soon as i hit the right arrow, the initial image does get removed but the new image does not appear. im not really sure why the JFrame can update a remove but not an add to the panel. if you want to look at the code i'll be happy to post it if you need it.

[570 byte] By [superwazna] at [2007-11-26 16:26:14]
# 1
Try panel.revalidate() (and maybe repaint too if revalidate wasnt enough).
abnormala at 2007-7-8 22:50:20 > top of Java-index,Desktop,Core GUI APIs...
# 2
great! it worked! but, why does it work? i'd rather not use something that works if i dont know how it works
superwazna at 2007-7-8 22:50:20 > top of Java-index,Desktop,Core GUI APIs...
# 3

> great! it worked! but, why does it work?

When you add components to a Container, the components need to be "layed out" by the LayoutManager. But what if you where adding 20 components at one time, it doesn't make sense to invoke the LayoutManager every time you add a component. So the revalidate() method basically says you are finished adding components and now its time to invoke the LayoutManager.

camickra at 2007-7-8 22:50:20 > top of Java-index,Desktop,Core GUI APIs...
# 4
thanks a ton!!!!!things are going really smoothly now and if i ever get a similar problem again i know why its happening and what to do!
superwazna at 2007-7-8 22:50:20 > top of Java-index,Desktop,Core GUI APIs...