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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:21:18+00:00 2026-06-15T04:21:18+00:00

I have a package named images in my src directory. I have quite a

  • 0

I have a package named images in my src directory. I have quite a few images that I use in my project. I use a method in my only Swing class to get Icons.

 public Icon getIcon(String name) {
    Icon icon = null;
    URL url = null;
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    try {
       url = classLoader.getResource(name);
       icon = new ImageIcon(url);
    } 
    catch (Exception e) {
       System.out.println("Couldn't find " + getClass().getName() + "/" + name);
       e.printStackTrace();
    }
 return icon;
}

To get a Icon

getIcon("/images/pdfClose.png");

This works great for Icons, but in my SWT classes, I use Images.

Is there a way in SWT to copy what the getIcon() method is doing?
Is it possible to rewrite the method to get images?

public ImageIcon getImage(String name) {
   ImageIcon image = null;
   URL url = null;
   ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
   try {
     url = classLoader.getResource(name);
     image = new ImageIcon(url);
   } 
   catch (Exception e) {
      System.out.println("Couldn't find " + getClass().getName() + "/" + name);
      e.printStackTrace();
   }
 return image;
}

I know it throws the error
This instance method cannot override the static method from Dialog

But is there a work around?

  • 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-15T04:21:19+00:00Added an answer on June 15, 2026 at 4:21 am

    I don’t really see, where you are struggling. This is what I do to load images:

    public static Image loadImage(String path, boolean inJar)
    {
        Image newImage = null;
    
        try
        {
            if(inJar)
            {
                newImage = new Image(null, YOUR_MAIN_CLASS.class.getClassLoader().getResourceAsStream(path));
            }
            else
            {
                newImage = new Image(null, path);
            }
        }
        catch(SWTException ex)
        {
            System.out.println("Couldn't find " + path);
            e.printStackTrace();
        }
    
        return newImage;
    }
    

    Note that I don’t keep my images in the src folder, since that folder is for source files. I have a folder img in the root of the project and access the images via:

    Image image = Images.loadImage("img/myImage.png", true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class named as order in package com.abc I have another class
I have a package with a few modules, each module has a class (or
i want copy pdf file from application path(/data/data/package name) to sdcard.for that i have
In Android, assuming that I have files in /data/data/package.name/, without knowing the names or
I have a package with logic classes(like CheckAuthenticationDataLogic.java, GetVocabulariesLogic.java). And another class - ApiService.java
I have checked similarly named questions, but they don't answer this use case. Basically,
I have a Python script that displays images fullscreen on a BeagleBoard with the
i have a proyect that im trying to finish i wanna upload images i
In my android project I have several images int the drawable folder with a
I have a Java project called MyProject. I have a few different packages (keeping

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.