Results 1 to 11 of 11
  1. #1
    javawriter is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default JMenuItem - image won't show

    Hi everyone,

    I'm relativley new to java programming, and I was having some problems displaying an image on a JMenuItem. When I run my program, I can open the file menu, however there is no image beside my close button. I included my image "exit.png" in the same directory as my class file and I can't seem to figure out what my problem is. Thanks in advance for any help. My code is as follows:

    public class Menu extends JFrame{
    public Menu(){
    setTitle("JMenuBar");

    JMenuBar menubar = new JMenuBar();

    ImageIcon icon =new ImageIcon(getClass().getResource("exit.png") );


    JMenu file = new JMenu("File");
    file.setMnemonic(KeyEvent.VK_F);

    JMenuItem fileClose = new JMenuItem("Close",icon);
    fileClose.setVisible(true);
    fileClose.setMnemonic(KeyEvent.VK_C);
    fileClose.setToolTipText("Exit Application");
    fileClose.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
    System.exit(0);
    }
    });

    file.add(fileClose);

    menubar.add(file);

    setJMenuBar(menubar);

    setSize(250,200);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    }

    public static void main(String[] args){
    new Menu();
    }
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Shouldn't that be
    Java Code:
    ImageIcon icon =new ImageIcon(getClass().getResource("/exit.png") );
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    javawriter is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    Thanks for the response.

    I tried changing the name to that but still no luck. I know it finds the picture in the directory because otherwise I get an error when running the program (when my image isn't present). I just have no clue why the picture isn't visible in the program itself.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default

    Your code works for me with my image:

    ImageIcon icon =new ImageIcon(getClass().getResource("images/search.jpg") );

  5. #5
    javawriter is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    Could there be something wrong with where my files are located. I have my class file and images folder in a package named 'src'. My picture 'exit.jpg' is saved in the images folder. I changed my icon code to what follows:

    Java Code:
     ImageIcon icon =new ImageIcon(getClass().getResource("images/exit.jpg") );
    Thanks for your reply

  6. #6
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    Quote Originally Posted by javawriter View Post
    Could there be something wrong with where my files are located. I have my class file and images folder in a package named 'src'. My picture 'exit.jpg' is saved in the images folder. I changed my icon code to what follows:

    Java Code:
     ImageIcon icon =new ImageIcon(getClass().getResource("images/exit.jpg") );
    Thanks for your reply
    No, to ref an image like that the image must be not in src files folder but in class files folder :)
    You see that...
    getClass().getResource("images/exit.jpg")
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  7. #7
    javawriter is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    i've been reading web references for the past 3 hours, and from what I understand, I believe I have the corrent "file set-up". I'm still not able to see my icon however, so I think I misunderstood something. My folder named 'src' contains my class file, and another folder named Menu. Inside of the Menu folder is my 'exit.png file'.

    My code is as follows:

    Java Code:
     
    
    package src;
    
    //import statements
    
    public class Menu extends JFrame{
    public Menu(){
    setTitle("JMenuBar");
    
    JMenuBar menubar = new JMenuBar();
    
    ImageIcon icon =new ImageIcon(getClass().getResource("/exit.png") );
    
    
    JMenu file = new JMenu("File");
    file.setMnemonic(KeyEvent.VK_F);
    
    JMenuItem fileClose = new JMenuItem("Close",icon);
    fileClose.setVisible(true);
    fileClose.setMnemonic(KeyEvent.VK_C);
    fileClose.setToolTipText("Exit Application");
    fileClose.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
    System.exit(0);
    }
    });
    
    file.add(fileClose);
    
    menubar.add(file);
    
    setJMenuBar(menubar);
    
    setSize(250,200);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    }
    
    public static void main(String[] args){
    new Menu();
    }
    }

  8. #8
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    have you tried,...
    Java Code:
    ImageIcon icon =new ImageIcon(getClass().getResource("Menu/exit.png") );
    or variations on that?

    Also: please post indented code. All left-justifed code is hard to read. Thanks.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default

    Recheck that the exit.png file is really an image.
    If the file is not found by the getResource() method there is an NullPointerException.
    I just tried:
    Java Code:
    ImageIcon icon =new ImageIcon(getClass().getResource(" images/junk.txt")
    NOTE: Without the leading space. The forum changes this if I post it without a space?)
    It executes without a NPE and no image shows!!
    Last edited by Norm; 07-13-2010 at 05:20 PM.

  10. #10
    javawriter is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    Thanks so much everyone, I just got it working! I deleted the image file and "re-saved" it. I think there may have been an issue with my exit.png file.

  11. #11
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Dear lord, that's a new one :) Glad you got it sorted.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. show image that I refresh
    By erwillie in forum New To Java
    Replies: 1
    Last Post: 04-29-2010, 03:23 PM
  2. Replies: 0
    Last Post: 03-19-2010, 11:26 PM
  3. Show image on glasspane while mouse button is down
    By zweibieren in forum AWT / Swing
    Replies: 10
    Last Post: 09-12-2009, 06:29 AM
  4. How to obtain the name of the JMenuItem?
    By Azuxard in forum AWT / Swing
    Replies: 1
    Last Post: 03-23-2009, 03:33 AM
  5. Replies: 1
    Last Post: 08-25-2008, 08:48 PM

Posting Permissions

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