Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8706069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:31:24+00:00 2026-06-13T03:31:24+00:00

Dear Stackoverflow Developers i have to load image as an icon to jmenu from

  • 0

Dear Stackoverflow Developers i have to load image as an icon to jmenu from jar.
I am using the following code . It is throwing the null pointer exception but when i am setting the icon with imageicon class in jframe, it is showing the icon in frame

   try
   {

    JMenu menu= new JMenu("");
    Icon im1= new ImageIcon(getClass.getResource("path of the images");

   menu.setIcon(im1);
  }
  catch(Exception ex)
   {

   }

what is the wrong in this code plz help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:31:25+00:00Added an answer on June 13, 2026 at 3:31 am
    • common issue is that ImageIcon, Icon doesn’t returns any exception, test for Null value

    • Icon im1= new ImageIcon why???, those are different Java Object, have to test if Icon is instanceof ImageIcon

    • read turotial about Icon and JMenu / JMenuItem

    • for example

    enter image description here

    code

    import java.awt.BorderLayout;
    import java.awt.ComponentOrientation;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    
    import javax.swing.AbstractAction;
    import javax.swing.Box;
    import javax.swing.Icon;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JSeparator;
    import javax.swing.JTextField;
    import javax.swing.JTextPane;
    import javax.swing.UIManager;
    import javax.swing.border.BevelBorder;
    
    public class MenuExample extends JPanel {
    
        private static final long serialVersionUID = 1L;
        private JTextPane pane;
        private JMenuBar menuBar;
    
        public MenuExample() {
            menuBar = new JMenuBar();
            JMenu formatMenu = new JMenu("Justify");
            formatMenu.setMnemonic('J');
            formatMenu.setIcon(UIManager.getIcon("OptionPane.errorIcon"));
            MenuAction leftJustifyAction = new MenuAction("Left", UIManager.getIcon("OptionPane.errorIcon"));
            MenuAction rightJustifyAction = new MenuAction("Right", UIManager.getIcon("OptionPane.informationIcon"));
            MenuAction centerJustifyAction = new MenuAction("Center", UIManager.getIcon("OptionPane.warningIcon"));
            MenuAction fullJustifyAction = new MenuAction("Full", UIManager.getIcon("OptionPane.questionIcon"));
            JMenuItem item;
            item = formatMenu.add(leftJustifyAction);
            item.setMnemonic('L');
            item = formatMenu.add(rightJustifyAction);
            item.setMnemonic('R');
            item = formatMenu.add(centerJustifyAction);
            item.setMnemonic('C');
            item = formatMenu.add(fullJustifyAction);
            item.setMnemonic('F');
            menuBar.add(formatMenu);
            menuBar.add(createMenu("Menu 1"));
            menuBar.add(createMenu("Menu 2"));
            menuBar.add(createMenu("Menu 3"));
            menuBar.add(Box.createHorizontalGlue());
            menuBar.add(new JSeparator());
            menuBar.add(new JButton("   Seach ....  "));
            menuBar.add(new JLabel());
            menuBar.add(new JTextField("   Seach ....  "));
            menuBar.add(new JComboBox(new Object[]{"height", "length", "volume"}));
            menuBar.add(createMenu("About"));
            menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));
    
        }
    
        private JMenu createMenu(String title) {
            JMenu m = new JMenu(title);
            m.add("Menu item #1 in " + title);
            m.add("Menu item #2 in " + title);
            m.add("Menu item #3 in " + title);
            if (title.equals("About")) {
                m.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            }
            return m;
        }
    
        class MenuAction extends AbstractAction {
    
            public MenuAction(String text, Icon icon) {
                super(text, icon);
            }
    
            public void actionPerformed(ActionEvent e) {
                try {
                    pane.getStyledDocument().insertString(0,
                            "Action [" + e.getActionCommand() + "] performed!\n",
                            null);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    
        public static void main(String s[]) {
            MenuExample example = new MenuExample();
            example.pane = new JTextPane();
            example.pane.setPreferredSize(new Dimension(250, 250));
            example.pane.setBorder(new BevelBorder(BevelBorder.LOWERED));
            JFrame frame = new JFrame("Menu Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setJMenuBar(example.menuBar);
            frame.getContentPane().add(example.pane, BorderLayout.CENTER);
            frame.pack();
            frame.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Dear Folk's i'm using the following code in order to send the bytes of
Dear all hibernate developers, The following hibernate hbm2java class I am using in my
Dear StackOverflow community, I have a dataset from my university projects I am trying
Dear Friends from Stackoverflow, Please help me with a problem that i'm having when
Dear members of the Stackoverflow community, We are developing a web application using the
Dear fellow programmers I have come to you for asistance with the following: I
Dear friends,I want to extract text 平均3.6 星 from this code segment excerpted from
Dear stackoverflow members, I have a small problem, I want to replace some characters
Dear gods of Stackoverflow Let's say I have a MySQL query that selects a
Dear competent people. I'm having a problem with the following code, specifically that the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.