This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How to get an image into an app?  RSS feed

 
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.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File location is incorrect, move it to: NetBeansProjects\myCalculator\images
 
Sheriff
Posts: 21603
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Use Code Tags.
 
Marshal
Posts: 62865
203
  • 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.
 
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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!