Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 14-11-2009
Member
 
Join Date: Nov 2009
Posts: 359
Display image icon in java

Hi
I am trying to write a code in java, I should be able to create a new image icon using the object. When I try to run the program the image simple does not appear on the button. All the image files are located in the image folder inside the working directory. I am post my code please check it.
Code:
public class Jbt2s extends JFrame {
public static void main(String[] args) {
new Jbt2s();
}
public Jbt2s() {
super("Creating bt2s");
addWindowListener(new ExitListener());
Container cn = getcnPane();
cn.setBackground(Color.white);
cn.setLayout(new FlowLayout());
// This is the statement of interest
// the image can be anything in my
ImageIcon sp = new ImageIcon("images/grapes.jpg");
Jbt2 bt1 = new Jbt2("bt2 1");
cn.add(bt1);
//Jbt2 bt22 = new Jbt2("bt2 2 ",sp);
Jbt2 bt22 = new Jbt2("bt2 2");
cn.add(bt22);
//Jbt2 bt23 = new Jbt2("bt2 3 ", sp);
Jbt2 bt23 = new Jbt2("bt2 3");
bt23.setHorizontalTextPosition(SwingConstants.L EFT);
cn.add(bt23);
//Jbt2 bt24 = new Jbt2(sp);
Jbt2 bt24 = new Jbt2(sp);
cn.add(bt24);
pack();
setVisible(true);
}
}
Reply With Quote
  #2  
Old 14-11-2009
Member
 
Join Date: May 2008
Posts: 2,008
re: Display image icon in java

Hi,
I went through your code thoroughly, I think you should try this. this may be helpful to your program
Code:
ImageIcon sp = new ImageIcon(this.getClass().getResource("images/grapes.jpg"));
If it does not work then post back again and even if it works then post back with a positive reply.
Reply With Quote
  #3  
Old 14-11-2009
Member
 
Join Date: Nov 2009
Posts: 359
re: Display image icon in java

Thanks for the reply
I tried you code, and what I got was the following popup.
Code:
Cannot launch Java application
Uncaught exception in main method:
java.lang.NullPointerException
See if you can help me further to solve this. Thank again.
Reply With Quote
  #4  
Old 14-11-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
re: Display image icon in java

Hi
Since you are getting a NullPointerException
Code:
NullPointerException
The above is a subclass of
Code:
RuntimeException
I think it shouldn't cause a compile error. May be there is a bug in your IDE? Check if the IDE is working fine.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Display image icon in java"
Thread Thread Starter Forum Replies Last Post
Want to get icon which can displays the image of data which it contains with Windows 7 Nakul2 Windows Software 4 04-01-2011 08:30 PM
How to change JFrame image icon? Juany Software Development 4 08-02-2010 04:39 PM
Image Display In Java Amaresh Software Development 5 30-01-2010 09:20 AM
How to display an image icon file in subform of access 2003? Superstar9 Windows Software 3 03-09-2009 06:27 PM
Convert any image into icon ChrisUlrich Customize Desktop 2 30-01-2009 11:31 AM


All times are GMT +5.5. The time now is 04:22 PM.