# setting the ImageIcon size



## Caliraj (Mar 22, 2006)

Hi,
Im getting stuck in the way Im trying to set the size of the ImageIcon. Im trying to set a jpeg image as the button. The program is below:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
*/
public class jbt extends JApplet implements ActionListener
{
Container c=getContentPane();
JLabel l=new JLabel("type");
JTextField t=new JTextField(10);
ImageIcon im1=new ImageIcon("yoga.jpeg");
JButton mb=new JButton(im1);
*public int getIconWidth()
{
return 200;
}
public int getIconHeight()
{
return 300;
}* public void init()
{
c.setLayout(new FlowLayout());
c.add(l);
c.add(t);
_mb.setIcon(im1);
mb.setSize(im1.getIconWidth(),im1.getIconHeight());_
mb.setRolloverIcon(im1);
mb.setRolloverEnabled(true);
c.add(mb);
mb.addActionListener(this);
}
public void actionPerformed(ActionEvent a)
{
l.setText(t.getText());
}
}
Im not quite sure if this is the right way to set the size of the ImageIcon, because even if I give values as high as 1000, I dont get to see the image on the button.
Thanks,
Cali


----------



## Chicon (Jul 29, 2004)

Hi caliraj,

First of all, resizing a component inside a *FlowLayout* is pratically impossible as the layout manager will put components sequentially in a row according their preferred size, either from left to right (default) or from top to bottom. In a FlowLayout, components are in a way interconnected.
Also, layout managers have their own set of rules that prevail on whatever method applied on a component. That means certain actions on the components can simply be ignored because they are violating one of the rules of the layout manager.

In your example, instead of implementing directly a variable size button, you may use a JPanel with a a size sufficiently large to contain a button. With the JPanel, you will have an independant container (nested container) in which you'll be able to resize your button according to the size of the image it will contains.

You should check the methods of the JButton : http://java.sun.com/javase/6/docs/api/javax/swing/JButton.html.


----------

