posted 13 years ago
> However, I'd be interested in knowing if this can be done another way so that instead of using the setIcon method, that I can :
> 1. remove the existing image label, perhaps using the remove(Component) method (Is a removal required???
> 2. use the existing JLabel variable to create a new instance of a JLabel using the image I want to display
> 3. Add this to the original panel and display the NEW image in the same place as the first one
as you've discovered, setIcon(..) is all you need to do the job.
> Well I don't need to change it but I was just wondering as I'm trying to expand my Swing knowledge.
similarly, if you wanted to use a thumb tack, all you'd need is a thumb, but if you wanted to look at alternatives,
a sledge hammer would also work.
in relation to your swing setIcon(..) question, this will also work
panel.remove(label);
label = new JLabel(new ImageIcon("..."));
panel.add(label);//with conditions - see below
panel.revalidate();
panel.repaint();
basically, any time you add or remove a component from a visible container, you need to:
revalidate();// swing
validate();// awt
and most times (not always) this needs to be followed by a call to repaint() - so safest to always include it.
however, in adding it to a visble container, a lot depends on the layoutManager:
for BorderLayout, simple specify the area NORTH, SOUTH etc
for GridLayout, you'd need to specify an index
etc.
for the
(Is a removal required???
try it with, then try it without
i.e. experiment