Image on Button..

How will I add an Image to a Button?
[36 byte] By [srini_raghav] at [2007-11-15 19:14:32]
# 1 Re: Image on Button..
You have to use Java Swing for this.
varbsjava at 2007-11-10 3:01:29 >
# 2 Re: Image on Button..
Try something like this:
URL url = getClass().getResource(iconPath);
ImageIcon image = new ImageIcon(url);
JButton button= new JButton(image);

EK
YePP at 2007-11-10 3:02:26 >
# 3 Re: Image on Button..
ImageIcon icon=new ImageIcon("myfile.gif");
JButton mybutton=new JButton("TheButtonText",icon);
at 2007-11-10 3:03:24 >
# 4 Re: Image on Button..
In java 1.1.x, you can create a class which extends the Button's class and use the method paint() in it to display your image's file.
Here is an example :

import java.awt.*;
import java.awt.event.*;

public class BoutonImage extends Button implements ActionListener
{
Image i;

public BoutonImage()
{
i = Toolkit.getDefaultToolkit().getImage("Hlpcd.gif");
this.addActionListener(this);
this.validate();
}

public void paint(Graphics g)
{
g.drawImage(i, 5, 5, this.getSize().width+5, this.getSize().height+5, this);
this.setSize(60, 30);
}


public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}

This code is not perfect because you need to refresh manually the frame to see the button behind the image, but it works !!

So long,
at 2007-11-10 3:04:30 >