Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By gimbal2

Thread: image icon is not working

  1. #1
    bhuwd is offline Member
    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    Default image icon is not working

    please tell me why image icon is not working to my menuitems....
    my code is
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class Tools extends JFrame
    {
    Toolkit t1;
    JLabel l1;
    JButton b1,b2;
    JMenuBar m1;
    public Tools()
    {
    t1=getToolkit();
    m1=new JMenuBar();

    l1=new JLabel("Toolkit example");
    b1=new JButton("Beep");


    ImageIcon iconExit=new ImageIcon("exit.png");
    ImageIcon iconNew=new ImageIcon("new.png");
    ImageIcon iconOpen=new ImageIcon("open.png");
    ImageIcon iconSave=new ImageIcon("save.png");

    JMenu f=new JMenu("File");
    JMenu e=new JMenu("Edit");
    JMenu fo=new JMenu("Format");
    JMenu v=new JMenu("View");

    JMenuItem ne=new JMenuItem("New", iconNew);

    JMenuItem ope=new JMenuItem("Open", iconOpen);
    JMenuItem sav=new JMenuItem("Save", iconSave);
    JMenuItem ex=new JMenuItem("Exit", iconExit);

    b1.setToolTipText("Press here for Sound");

    b2=new JButton("Close");
    f.setMnemonic(KeyEvent.VK_F);
    e.setMnemonic(KeyEvent.VK_E);
    fo.setMnemonic(KeyEvent.VK_R);
    v.setMnemonic(KeyEvent.VK_V);
    ex.setMnemonic(KeyEvent_VK_C);
    setLayout(null);
    b1.setBounds(60,100,100,20);
    b2.setBounds(180,100,100,20);
    l1.setBounds(60,50,100,20);

    b1.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e1)
    {
    t1.beep();
    }
    });

    f.add(ne); f.add(ope); f.add(sav); f.add(ex);
    m1.add(f); m1.add(e); m1.add(fo); m1.add(v);

    setJMenuBar(m1);
    add(l1);
    add(b1);
    add(b2);
    setSize(300,300);
    setVisible(true);
    setLocation(300,250);

    }
    public static void main(String h[])
    {
    new Tools();
    }
    }

    please check this ...please help me.

  2. #2
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: image icon is not working

    Hi,
    I know this.. resource file location
    If you run on eclipse, you should move the resources files (I mean exit.png, new.png ,..) to the project folder. Example, your project "project_name" contains the folder "src", "src" contains "package_name", "package_name" contains class "Tools", the resource files should be in "project_name" in eclipse.

    PS: line:43, error
    Java Code:
    (KeyEvent.VK_C);//yours was (KeyEvent_VK_C);
    regards
    dhilip
    Last edited by noobplus; 07-16-2013 at 09:20 AM.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: image icon is not working

    Quote Originally Posted by noobplus View Post
    the resource files should be in "project_name" in eclipse.
    That is bad advice, because this does not explain WHY this would work. And it would only work in one specific setup, when you run from Eclipse and you don't change the application's working directory.

    In stead, put the image files in for example a 'src\data' folder and load them as follows:

    Java Code:
    ImageIcon iconExit=new ImageIcon(getClass().getClassLoader().getResource("data/exit.png"));
    Putting the images in your project sources makes them available on the classpath. Using getResource() as I demonstrate will make ImageIcon attempt to load the image from the classpath. This way, your application works anywhere without any changes.
    noobplus likes this.

Similar Threads

  1. Adding an image/icon to Jpanel?
    By Jpyo in forum AWT / Swing
    Replies: 6
    Last Post: 04-27-2012, 07:47 PM
  2. Replies: 2
    Last Post: 11-24-2010, 02:38 PM
  3. Image icon in SWT
    By ashin in forum SWT / JFace
    Replies: 3
    Last Post: 07-11-2009, 02:21 AM
  4. [SOLVED] JFrame Image Icon
    By Ch4os in forum NetBeans
    Replies: 1
    Last Post: 11-24-2008, 10:53 AM
  5. how to remove an image icon
    By cecily in forum Advanced Java
    Replies: 1
    Last Post: 08-05-2007, 04:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •