File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How to stretch icon to fit on JToggleButton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to stretch icon to fit on JToggleButton" Watch "How to stretch icon to fit on JToggleButton" New topic
Author

How to stretch icon to fit on JToggleButton

Rabbia Najam
Greenhorn

Joined: Sep 08, 2008
Posts: 2
Hi,
i want to stretch my button's icon to fit it on the whole button when i increase the size of button through coding.
e.g if i have a button size 51,51 and an icon size 50,50.....it is exactly fit on the button.....but what if i change the size of the button 100,100 and wants that the image 50,50 stretch according to the new size of the button and exactly fit on it.
Plzzz help me
Thanks
Bye
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

Image has a method called getScaledInstance. You can use that to scale your image. Resizing can slow your application though, so do it as sparsely as possible.

You can use a ComponentListener to find out when the button is resized.


Moving to Swing.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rabbia Najam
Greenhorn

Joined: Sep 08, 2008
Posts: 2
Thanks
but can you please show me a small example to scale an icon of a button.according to button size.
bye
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
it would be far more beneficial to you, for you to have a shot at it yourself.

google the keyword
look for code snippets
try to understand what the snippets are doing
read the apidocs/tutorials for explanations

and if you can't get it working, post your attempt here and we'll try to
point you to the fix
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

If you want to work with Icons, then ImageIcon is your friend. And guess what - you can create them with Images!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
Start looking here.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Originally posted by Rob Prime:
Image has a method called getScaledInstance. You can use that to scale your image. Resizing can slow your application though, so do it as sparsely as possible.


Performance wise, how does this compare to over riding the Button's paintComponents(Graphics g) and drawing the Image?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

To be honest, I have no idea. You would have to check that out with some tests.

Still, my guess is that stretching it through paintComponent will not be slower. It can sometimes use the graphics card for that, and even if it can't, it should still be just as fast / slow as scaling manually.

And of course, after you've scaled it, it needs to be drawn again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to stretch icon to fit on JToggleButton