jQuery in Action, 2nd edition*
The moose likes Swing / AWT / SWT and the fly likes How to change image on label without using seticon method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to change image on label without using seticon method" Watch "How to change image on label without using seticon method" New topic
Author

How to change image on label without using seticon method

Colm Dickson
Ranch Hand

Joined: Apr 04, 2009
Posts: 89
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

Joined: Jul 26, 2007
Posts: 10170
    
    8

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.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Colm Dickson
Ranch Hand

Joined: Apr 04, 2009
Posts: 89
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

Joined: Oct 27, 2005
Posts: 19653
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Apr 04, 2009
Posts: 89
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: How to change image on label without using seticon method
 
Similar Threads
Insert Pictures with Swing
Dice Program
Displaying an image in a subpanel
2d graphics context from component to image
Adding an Image to JPanel using NetBeans IDE