hello please help me i have picture then i want to put on my JButton but the problem is that my picture is too big its jpg file how to make this bmp file so that it fit to my Jbutton..hoping for your positive responds

The JButton takes as argument an Icon.
If you look at the class ImageIcon which extends Icon you will find 2 things:
The constructor of ImageIcon takes as argument an Image object.
There is a method: getImage() that "Returns this icon's Image."

The Image class has the method that you need to accomplish what you want.

Use that method to get the new Image, create a new ImageIcon and pass that as parameter to the JButton contructor

oki thanks for the reply i will try this..more power to you...i will write again if i have problem on this

Create the image object from the file, then create an ImageIcon frmo it and the set the button's icon with that icon.

Image myImage = getToolkit().createImage("myImage.jpg");
ImageIcon myIcon = new ImageIcon(myImage);
myButton.setIcon(myIcon);//here you set the icon for your button

Create the image object from the file, then create an ImageIcon frmo it and the set the button's icon with that icon.

Image myImage = getToolkit().createImage("myImage.jpg");
ImageIcon myIcon = new ImageIcon(myImage);
myButton.setIcon(myIcon);//here you set the icon for your button

If you had read carefully his question, jemz has asked how to make the image smaller in order to fit, not how to put an image in a button

ahaa... sorry.
You can get it as a BufferedImage and resize it (but I think you'll lose lot of quality). See if this works:

//'myAppName' is your main class 
BufferedImage img  = ImageIO.read(myAppName.class.getResource("myImage.jpg"));

int width = myButton.getWidth();
int height = myButton.getHeight();

BufferedImage scaledImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            
            Graphics2D graphics2D = scaledImage.createGraphics();
            graphics2D.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                                                           RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);
            graphics2D.drawImage(img, 0, 0, width, height, null);
            graphics2D.dispose();

ImageIcon myIcon = new ImageIcon(scaledImage);
myButton.setIcon(myIcon);

Hope this helps.

oki thanks for helping it works..more power to you guyz

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.