Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get an image into an app?  RSS feed

 
Matthew Bailey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File location is incorrect, move it to: NetBeansProjects\myCalculator\images
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Use Code Tags.
 
Campbell Ritchie
Sheriff
Posts: 55315
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!