Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Image on a JButton

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
malcolm bailey
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
malcolm bailey
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic