• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to change image on label without using seticon method

 
Colm Dickson
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.



I have a panel that holds a JLabel component which displays an image using new JLabel(new ImageIcon("aPicture.gif");


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.

Hope you can understand my aim.

Thanks,

Colm
 
Maneesh Godbole
Saloon Keeper
Posts: 11064
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Why do you want to change something which is already working? That is the standard way of changing images.
 
Colm Dickson
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying.


Well I don't need to change it but I was just wondering as I'm trying to expand my Swing knowledge.

Thanks.

Colm


 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
 
Colm Dickson
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all.



Yes the other method works(i.e. using the revalidate()... I do have to remove the component before adding it again though.

Thanks again.

Colm
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic