File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Problems with ImageIcon in JButton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problems with ImageIcon in JButton" Watch "Problems with ImageIcon in JButton" New topic

Problems with ImageIcon in JButton

Kevin Wright
Ranch Hand

Joined: Jul 10, 2001
Posts: 38
I can't seem to get an image to show up on a JButton...Here is my code, what's wrong with it?
private void initialize()
Icon fileg = new ImageIcon("images/mail11.gif");
Icon setupg = new ImageIcon(SETUPGIF);
Icon newmailg = new ImageIcon(NEWMAILGIF);
Icon deleteg = new ImageIcon(DELETEGIF);
Icon checkg = new ImageIcon(CHECKFORMAILGIF);
buttonPanel = new JPanel();
file = new JButton("File", fileg);
setup = new JButton("Setup", setupg);
newMail = new JButton("New Mail", newmailg);
delete = new JButton("Delete", deleteg);
checkForMail = new JButton("Check Mailbox", checkg);

messageViewer = new JPanel();
inbox = new JPanel();
}//end of initialize
The constants are filenames (relative addresses)
Ranch Hand

Joined: Jun 21, 2001
Posts: 57
Here is the code which displays button with icon. Check you code with this.
class gifButton extends JFrame{
// create image icon
Icon fileg = new ImageIcon("*.GIF");
Icon setupg = new ImageIcon("*.GIF");
// create button with icon
JButton file = new JButton("File", fileg);
JButton setup = new JButton("Setup", setupg);
/* // Alternate method - of creating a button with icon.
JButton file = new JButton("File");
JButton setup = new JButton("Setup");
setup.setIcon(setupg); */
getContentPane().setLayout(new GridLayout(3,2));
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
public static void main(String arg[]){
new gifButton().show();
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

I don't know what kind of a layout you are using, but sometimes the buttons don't appear big enough to show both the text and the icon. If you are using a null layout, try setting the bounds of your buttons to be a little larger button.
Otherwise, dismiss this trivial message

GenRocket - Experts at Building Test Data
I agree. Here's the link:
subject: Problems with ImageIcon in JButton
It's not a secret anymore!