aspose file tools
The moose likes Swing / AWT / SWT and the fly likes How to get an image into an app? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "How to get an image into an app?" Watch "How to get an image into an app?" New topic
Author

How to get an image into an app?

Matthew Bailey
Greenhorn

Joined: Jan 29, 2009
Posts: 6
Sorry to bother everyone yet again but I have a problem that is really annoying me. Our teacher wants us to make a calculator, which I have done, and wants us to put as many different features into it as possible. As a suggestion he said we should have an "About" thing with stuff like Version Number, Creator, etc and a picture. Now I am trying to use a JLabel as an icon, which apparently you can do with its constructor "JLabel(Icon image)", I have looked all over for how you acomplish this but I have had no luck.

Short version: How do you put an image into your java app?

My code if it will help:

package mycalculator;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AboutForm extends JFrame implements ActionListener{


public AboutForm(){
super("About");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(300, 300);
Font f = new Font("Trebuchet MS", Font.PLAIN, 13);
Font f2 = new Font("Trebuchet MS", Font.BOLD, 16);


this.setFont(f);
BorderLayout b1 = new BorderLayout();
GridLayout g = new GridLayout(4, 1);
JButton btn1 = new JButton("CLICK HERE");
btn1.setFont(f);

JButton btn2 = new JButton("CLICK HERE ALSO");
JPanel panel = new JPanel();
JLabel lbl = new JLabel();
JLabel lbl2 = new JLabel();

ImageIcon icon = new ImageIcon("images/b.gif");
JLabel lblImg = new JLabel(icon);

panel.setSize(100,100);
panel.setLayout(g);
panel.add(lblImg);
panel.add(lbl2);
this.getContentPane().add(panel, BorderLayout.CENTER);


}


I have my image in a folder in NetBeansProjects\myCalculator\src\mycalculator\images, is this the right place to put it?

Any help is greatly appreciated.

Pawel Makara
Greenhorn

Joined: Apr 10, 2009
Posts: 1
File location is incorrect, move it to: NetBeansProjects\myCalculator\images
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

Please Use Code Tags.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32830
    
    4
Pawel Makara, Welcome to JavaRanch

Apart from the code tags, it would have been better to post on the Swing forum. I shall move you there.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if you want to include your image in your submitted work, you may have to .jar it
and load the image via a getResource()

http://java.sun.com/docs/books/tutorial/deployment/jar/index.html
Matthew Bailey
Greenhorn

Joined: Jan 29, 2009
Posts: 6
Thank you! It was just the location of the image, now it works.

Thanks also for the other advice, I'll be sure to use the tags in any future posts and I'll put any other swing-related things in the swing forum.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How to get an image into an app?
 
Similar Threads
scrollPane
Image is not been added to a label
Selecting applications using Icons in Swing
jpeg in a Jpanel
How to add panel in from other class