This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
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 Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Image on a JButton" Watch "Image on a JButton" New topic
Author

Image on a JButton

malcolm bailey
Greenhorn

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 {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
titledBorder1 = new TitledBorder("Ficha");
toolbar.setForeground(new java.awt.Color(77, 77, 108));
toolbar.setBorder(BorderFactory.createEtchedBorder());
toolbar.setSize(600, 50);
button = new JButton(new ImageIcon("images/save.jpeg"));
button2 = new JButton("blah");
button3 = new JButton("blah");
button4 = new JButton("blah");
toolbar.add(button);
toolbar.add(button2);
toolbar.add(button3);
toolbar.add(button4);
this.getContentPane().setLayout(xYLayout1);
this.getContentPane().add(toolbar, new XYConstraints(0, 0, 600, 30));
this.setSize(600, 450);
this.show();
}
public static void main(String args[]) {
MainFrame mainf = new MainFrame();
}
}
Thanks very much...
Malc
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
Greenhorn

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 MainFrame.java. 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?
Malc
Garandi Garandi
Ranch Hand

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

Joined: Jul 11, 2001
Posts: 15300
    
    6

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 - Experts at Building Test Data
malcolm bailey
Greenhorn

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:
C:/Malcolm/myprojects
My output path is:
C:/Malcolm/myclasses
My absolute path:
"C:/Malcolm/myclasses/ficha/save.jpg"
Any further pointers? (We�re almost there!!!)
Malc
Nathan Pruett
Bartender

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 ).


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Image on a JButton