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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:09:03+00:00 2026-05-24T01:09:03+00:00

I have tried several methods to add an Icon to a JFrame. Every method

  • 0

I have tried several methods to add an Icon to a JFrame. Every method work perfectly when I run it using the source code.

for example:

jframe.setIconImage(Toolkit.getDefaultToolkit().getImage("iconimages/icon.png"));

But none of them work when I run it using the jar file. I know the problem is with the path of the image file. How can I solve this?

Edit:

public Ui() { 
   initComponents(); 
   setLocationRelativeTo(null); 
   this.setIconImage(getImageIcon("icon.png").getImage());
} 

private ImageIcon getImageIcon(String fileName) {
   String imageDirectory = "iconimages/"; 
   imgURL = getClass().getResource(imageDirectory + fileName); 
   return new ImageIcon(imgURL); 
}

I tried this but now I get a null pointer exception.

--------------------------------------------------------------------------------

Edit [Solution] : I found the solution.

I added ../ to the path additionally and it works perfectly!!! 😀

 ImageIcon imageIcon = new ImageIcon("../imageicons/icon.png");
 this.setIconImage(imageIcon.getImage());

Thanks all for try to help me. 🙂

  • 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-24T01:09:04+00:00Added an answer on May 24, 2026 at 1:09 am

    You should use a URL. Like this:

     /**
      * Loads and returns an {@link Image} resource. 
      * @param fileName name of the image resource.
      * @return Image as resource.
      */
      public Image getResourceImage(String fileName) {
          String imageDirectory = "images/";
          URL imgURL = getClass().getResource(imageDirectory + fileName);
          Image image = null;
          try {
             image = ImageIO.read(imgURL);
           } catch (IOException e) {}
          return image;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried several methods but none is playing the file2.m4v using html5 see
I have tried several things so far, but I haven't had much luck yet.
I have tried several ways to login to a website through java. I have
Here is what I have. I have tried several things but I can't figure
I really need advice on how to do the following. I have tried several
I have tried this question on the site of Titanium and several tweets to
I'm building a much much larger program, and I've tried several methods (all of
Using this I tried to create a custom ListAdapter that would simply add all
So I have tried to follow the alfresco example several times and cant seem
Suppose I have my Android app's source code packaged this way: src/ my/ app/

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.