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 7003971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:08:18+00:00 2026-05-27T21:08:18+00:00

I have been studying swing for developing gui applications since a few days. The

  • 0

I have been studying swing for developing gui applications since a few days.

The following code aims to create a toolbar with an exit button (including the image “exit.png”). The problem is I am unable to see the image, though the toolbar shows up. The toolbar is positioned NORTH using the borderlayout manager in java.

    JMenuBar menubar = new JMenuBar();
    JMenu file = new JMenu("File");

    menubar.add(file);
    setJMenuBar(menubar);

    JToolBar toolbar = new JToolBar();
    toolbar.setFloatable(false);

    ImageIcon exit = new ImageIcon("exit.png");
    JButton bexit = new JButton(exit);
    bexit.setBorder(new EmptyBorder(0, 0, 0, 0));
    toolbar.add(bexit);
    //Default layout manager for JFrame is BorderLayout Manager
    add(toolbar, BorderLayout.NORTH);

The code is written inside a class constructor where the class extends the JFrame swing class.

please note that I have imported the required classes. There is no compile error either. the image is saved in the directory where the .class is created. Other elements of the gui are displayed without an error.

Please help me identify the problem. Thanks in advance.

  • 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-05-27T21:08:19+00:00Added an answer on May 27, 2026 at 9:08 pm

    First create your ImageIcon.

    ImageIcon myIcon = createImageIcon("exit.png", "");
    

    And the code for the createImageIcon() method as found on the Oracle website:

    private ImageIcon createImageIcon(String path, String description) {
            URL imgURL = getClass().getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL, description);
           } else {
              System.err.println("Couldn't find file: " + path);
              return null;
           }
        }
    

    And then set the icon of the button with:

    bexit.setIcon(myIcon);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been studying Ruby for the past few days and I have noticed
I have been studying unicode and its Python implementation now for two days, and
Over the last few weeks I have been studying the MVC design pattern for
I've recently been studying TDD, attended a conference and have dabbled in few tests
I have been studying this code to generate random text: from collections import defaultdict,
I have been studying .NET 4.0 Code Contracts and looking on stackoverflow as well
I've recently been studying Qt, and have the following questions: What is the difference
i have been studying unity3d for a few months and i have done some
i have been recently studying php and mysql and also is currently developing a
I have been studying Java for a few months and am now starting to

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.