• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

How to change image on label without using seticon method

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic