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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:25:14+00:00 2026-05-28T14:25:14+00:00

this is basic example I got for a GUI from a book called Java

  • 0

this is basic example I got for a GUI from a book called Java In Easy Steps, I implemented the code as per the example but the imagery does not appear. What do I need to do in order to make it appear, is because of the URL getClassLoader?

Ideally I would like to be able to save a file to my workspace and use that file as a part of the GUI.

import javax.swing.*;

class Buttons extends JFrame {

    JPanel pnl = new JPanel();
    ImageIcon tick = new ImageIcon("tickURL");
    ImageIcon cross = new ImageIcon("crossURL");

    JButton btn = new JButton("Click Me");
    JButton tickBtn = new JButton(tick);
    JButton crossBtn = new JButton("STOP", cross);

    ClassLoader ldr = this.getClass().getClassLoader();
    java.net.URL tickURL = ldr.getResource("tick.png");
    java.net.URL crossURL = ldr.getResource("cross.png");

    public Buttons(){

        super("Swing Window");
        setSize( 500, 200);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(pnl);
        setVisible(true);

        pnl.add(btn);
        pnl.add(tickBtn);
        pnl.add(crossBtn);

    }

    public static void main(String[]Args){

        Buttons gui = new Buttons();

    }

}
  • 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-28T14:25:15+00:00Added an answer on May 28, 2026 at 2:25 pm
    ClassLoader ldr = this.getClass().getClassLoader();
    java.net.URL tickURL = ldr.getResource("tick.png");
    java.net.URL crossURL = ldr.getResource("cross.png");
    
    JPanel pnl = new JPanel();
    ImageIcon tick = new ImageIcon(tickURL);     // <-- a URL is needed here, not a string
    ImageIcon cross = new ImageIcon(crossURL);   // same here
    
    JButton btn = new JButton("Click Me");
    JButton tickBtn = new JButton(tick);
    JButton crossBtn = new JButton("STOP", cross);
    
    1. load your assets with the classLoader (if they are in your classpath),
    2. create your icons with these assets,
    3. create your buttons with these icons

    As simple as that.

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

Sidebar

Related Questions

I've got this to work using a very basic example(outlined below) and I want
This is a very basic example of what I am trying to implement in
I'm trying to understand why this does not work. (Basic example with no validation
Forgive my ignorance in asking this basic question but I've become so used to
This is basic stuff, but I'm somewhat unfamiliar with VBA and the Word/Access object
This is very basic stuff , but here goes. I find that I am
This might be basic question but how do I create a list of lists
I've got some example Python code that I need to mimic in C++. I
I'm not sure exactly how to describe this question, but here goes. I've got
This is probably pretty basic, but I'm trying to find out how I can

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.