What I want is to be able to change this picture in response to a button click.
I currently have a working solution where the picture changes via this action listener by setting the label's image icon to another image using the setIcon method.
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
I think this approach requires me to redraw the screen by overriding the paint(Graphics g) method but doing so and then calling the repaint method when my button is pressed, which in turn calls paint(), does not have any effect.
I have not posted any code because I just want to know if it can be done this way although I have a solution already by just changing the image with the setIcon method.
You could subclass the class and override the paintComponent method, but in the end you'll be doing a lot of hard work that has already been done for you. You can even specify where your icon should be placed relatively to the text - left, right, bottom or top. Ok, maybe it's the other way around (you set the text position relatively to the icon), but the result is the same.
> 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
label = new JLabel(new ImageIcon("..."));
panel.add(label);//with conditions - see below
basically, any time you add or remove a component from a visible container, you need to:
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
(Is a removal required???
try it with, then try it without
Joined: Apr 04, 2009
Yes the other method works(i.e. using the revalidate()... I do have to remove the component before adding it again though.
subject: How to change image on label without using seticon method