Forums Register Login

Image on a JButton

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
try this.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ).
Don't touch me. And dont' touch this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 3098 times.
Similar Threads
Math code
JLabel1.setText error
requestFocus
Problem with JToolBar
Errors in Frame
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 08:51:48.