Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resizing a ImageIcon in a JButton

I am creating a JButton which includes a specific ImageIcon. The main issue is that the original icon size is much bigger than the button size. As result when the button is displayed, only part of the icon can be seen. What is the method that "resize" an ImageIcon i n order to make it fit inside a JButton?

like image 872
Anto Avatar asked May 18 '10 10:05

Anto


People also ask

How do I make a JButton smaller?

Simply delete the frame. setPreferredSize() and the buttons will be small (the frame size will be determined by the preferred sizes of inner components by calling frame.

How do I fit an image into a JButton?

Now, to use this method in your example code: JFrame frame2 = new JFrame("Tauler Joc"); JPanel panell = new JPanel(); ImageIcon icon = new ImageIcon("king. jpg"); JButton jb= new JButton(); jb. setBounds(200,200,700,700); panell.

How do I change font size in JButton?

To change the font in java swing: name_Button. setFont(new FontUIResource(String name, int style, int size)); name the font name.


1 Answers

   Image img = icon.getImage() ;      Image newimg = img.getScaledInstance( NEW_WIDTH, NEW_HEIGHT,  java.awt.Image.SCALE_SMOOTH ) ;      icon = new ImageIcon( newimg ); 

from http://www.coderanch.com/t/331731/GUI/java/Resize-ImageIcon

like image 180
tim_yates Avatar answered Sep 22 '22 05:09

tim_yates