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

  • Home
  • SEARCH
  • 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 88099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:30:23+00:00 2026-05-10T22:30:23+00:00

I have a number of icons used throughout an application – let’s take ok/cancel

  • 0

I have a number of icons used throughout an application – let’s take ok/cancel icons as an example. At the moment they might be a tick and a cross (tick.png, cross.png) but I may want to replace them in future. Also, I would like to keep the resource path in one place.

Is this ok:

public class Icons {     public static Icon OK = new ImageIcon(Icons.class.getResource('/icons/tick.png');     public static Icon CANCEL = new ImageIcon(Icons.class.getResource('/icons/cross.png'); } 

Or should I be doing this a different way? I don’t mind relying on the existence of the image files at runtime since they’re in the .jar

Solution

I’ve used Bent’s idea for initialisation, and I’ve made the constants final:

public final class Icons {     private static final Logger logger = Logger.getLogger(Icons.class);      public static final Icon OK = icon('/icons/add.png');     public static final Icon CANCEL = icon('/icons/cancel.png');      private static Icon icon(String path) {         URL resource = Icons.class.getResource(path);         if(resource==null) {             logger.error('Resource '+path+' does not exist');             return new ImageIcon();         }         return new ImageIcon(resource);     } } 
  • 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. 2026-05-10T22:30:24+00:00Added an answer on May 10, 2026 at 10:30 pm

    If you want to keep you icons as static constants, I would extract the instantiation of the ImageIcon objects into a static method;

    public static final Icon ok = icon('ok.png');   private static Icon icon(String path) {      URL resource = Icons.class.getResource('/icons/' + path);     if (resource == null) {         // Log something...         return null;     }     return new ImageIcon(resource); } 

    This way you have control whenever something fail, and you don’t have to repeat yourself in the instantiation.

    Also, I would make the constants final.

    A more general approach could be to use reflection to inspect your Icons-class and load resources for each public static Icon field in the class. This way, you would only have to declare a new Icon constant, and the corresponding resource would be loaded automatically based on the name of the constant. Leave a comment if you want more tips for doing this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 70k
  • Answers 70k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You might try keeping a reference to your drawingPanel and… May 11, 2026 at 12:49 pm
  • added an answer You're right, in this instance using an ORM to load,… May 11, 2026 at 12:49 pm
  • added an answer No, you can't use a desktop assembly under the CF… May 11, 2026 at 12:49 pm

Related Questions

I have a number of icons used throughout an application - let's take ok/cancel
I have a number of macros written for Visual Studio 2005, but they have
I have a number of tracks recorded by a GPS, which more formally can
I have a number of application settings (in user scope) for my custom grid
i have a number of jsp files under web-inf folder. Inside my web.xml i
I have an existing PDF and I can use FdFWriter to input to text
I am working on a project that requires a number of icons generated from
i have a winforms application. i have a user control with a lot of
How can I reduce resources inside my application? I have tried a number of

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.