• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problems with ImageIcon in JButton

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't seem to get an image to show up on a JButton...Here is my code, what's wrong with it?
/*********Code**********/
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);
//System.out.println(FILEGIF);
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
/*********Code**********/
The constants are filenames (relative addresses)
Thanks,
Kevin
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code which displays button with icon. Check you code with this.
class gifButton extends JFrame{
gifButton(){
// 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");
file.setIcon(fileg);
setup.setIcon(setupg); */
getContentPane().setLayout(new GridLayout(3,2));
getContentPane().add(file);
getContentPane().add(setup);
pack();
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String arg[]){
new gifButton().show();
}
}
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic