aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Image on a JButton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Image on a JButton" Watch "Image on a JButton" New topic

Image on a JButton

malcolm bailey

Joined: Aug 10, 2001
Posts: 22
Hello all,
Can anyone tell me why the image I have does not appear on the JButton? I can�t see why, I{ve tried using the absolute path, but to no avail. Here�s the code (I�ve started, and I�m just experimenting at the moment.. )
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import javax.swing.border.*;
public class MainFrame extends JFrame {
JToolBar toolbar = new JToolBar();
XYLayout xYLayout1 = new XYLayout();
JButton button;
JButton button2;
JButton button3;
JButton button4;
TitledBorder titledBorder1;

public MainFrame() {
try {
catch(Exception e) {
private void jbInit() throws Exception {
titledBorder1 = new TitledBorder("Ficha");
toolbar.setForeground(new java.awt.Color(77, 77, 108));
toolbar.setSize(600, 50);
button = new JButton(new ImageIcon("images/save.jpeg"));
button2 = new JButton("blah");
button3 = new JButton("blah");
button4 = new JButton("blah");
this.getContentPane().add(toolbar, new XYConstraints(0, 0, 600, 30));
this.setSize(600, 450);;
public static void main(String args[]) {
MainFrame mainf = new MainFrame();
Thanks very much...
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Malcom,
You must have a path problem. Your code works fine when the image can be found. I tested it with jpegs in the same directory as the class files and the button loads the image. When I put a non-existant file in the ImageIcon constructor it just makes an itsy-bitsy button with no image.
Hope this helps,
Michael Morris

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
malcolm bailey

Joined: Aug 10, 2001
Posts: 22
Hi Michael,
I think I may have missed something pretty basic. I moved my images folder to the same directory as my classes, but it still didn�t work. I created everything in JBuilder, which sometimes moves things about when you compile. I have a folder called "Malcolm/myprojecs/ficha", in which I have the .jpr file and Jbuilder created "Malcolm/myclasses/ficha", so I put "images" into the "ficha" folder, but no joy..
I must be organising things badly.. How should it all be arranged?
Garandi Garandi
Ranch Hand

Joined: Jan 07, 2003
Posts: 192
try this.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286

Are you 100% sure that your file extension is jpeg and not jpg? Most of the time the extension for a JPEG file will be jpg and not jpeg. ??
[ February 07, 2003: Message edited by: Gregg Bolinger ]

GenRocket - A Test Data Generation Platform
malcolm bailey

Joined: Aug 10, 2001
Posts: 22
Hi All,
Thanks for the contributions, I have a result, but only when I use the absolute path, which I should really avoid. I changed the file extension to .jpg - cheers, Greg...
JBuilder Properites:
My source path is:
My output path is:
My absolute path:
Any further pointers? (We�re almost there!!!)
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

put save.jpg in your output path (C:/Malcolm/myclasses), or load it as "ficha/save.jpg"...

You're running into classpath issues... remember, your classpath points to the directory above the beginning of your package directories...

So if you have a class called MainClass in the package ficha, the full name of that class is "ficha.MainClass". The class physically resides in the file "C:/Malcolm/myclasses/ficha/MainClass.class", so you would have to have the directory "C:/Malcolm/myclasses" in your classpath to load this class. This directory is also where your class would start searching for other files it needs ( like images ).

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: Image on a JButton
Similar Threads
Errors in Frame
JLabel1.setText error
Math code
Problem with JToolBar