Changing Desktop Icon for Java Application
Asked Answered
G

1

8

I wish to thank you in advance for taking the time to read my question, and I would greatly appreciate any comments, answers, insights, techniques and critiques that you may be able to provide.

I'm looking for a useful method for changing the desktop icon for a Java application. I've looked into this for a few days now, but am not finding an accurate result.

Before you mark this down and call it a duplicate, I have read: How do I change the default application icon in Java? to others who asked this question), but this does not address my specific problem. I know that their method utilizes a url location instead of an import, but I am trying to learn how to use this with the import(if that is, in fact, possible). When I attempt to use their method for changing by source location. Besides that, the url example doesn't seem to work for a file stored on the computer. I get an "uncaught error" message when I attempt to run it.

I use the following format to declare an image that I have imported into NetBeans:

Image image = new ImageIcon("imported.png").getImage();
frame.setIconImage(image);

Now this works fine for the icon that displays in the toolbar and it also appears in the upper left-hand corner of the frame, but I still have the Java coffee-cup as the icon for the application when I clean and build it.

For additional resources to the code that I am using to attempt this:

import java.awt.Image;
import javax.swing.*;

public class Check {
    JFrame frame;
    public static void main(String[] args) {
        new Check().go();
    }
    private void go() {
        frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Image image = new ImageIcon("owl.gif").getImage();

        frame.setIconImage(image);
        frame.setVisible(true);
        frame.setSize(300, 300);
    }
}

The "owl.gif" bit is what I imported into NetBeans by click and drag method (as described in one of the books that I read that focused on NetBeans).

I'm looking for a way to make a file that I already have saved on my computer the desktop icon for my application after it is built.

Gambrill answered 11/8, 2013 at 15:41 Comment(5)
You simply can use this Image image = new ImageIcon(Check.class.getResource("/owl.gif")).getImage();. More info can be found on this answer of mine :-)Onshore
Or you can look at this approach, I hope this might can help on the given topic :-)Onshore
@nIcEcOw Like the ASCII art directory structure. :)Sail
@nIcEcOw When I attempt the first example, I receive a NullPointerException. Is it possible that I am importing the file incorrectly? When I click and drag it over to the main class under the Files tabs, it is displayed, but for some reason cannot be invoked.Gambrill
@JeremyJohnson : Please refer to the link, in my first comment, there is a link that works for NetBeans, though in simpler terms, the way I commented, the image have to be alongside the package, though if you remove the first forward slash, than the image is suppose to be present alongside the .class file, which contains the above statement. If still unclear, do watch the last link of the answer in the first comment, the first example of that link can give you that idea, about directory structure :-)Onshore
S
4

For deploying Java desktop apps., the best option is usually to install the app. using Java Web Start1. JWS works on Windows, OS X & *nix.

  1. JWS provides many appealing features including, but not limited to, splash screens, desktop integration, file associations, automatic update (including lazy downloads and programmatic control of updates), partitioning of natives & other resource downloads by platform, architecture or Java version, configuration of run-time environment (minimum J2SE version, run-time options, RAM etc.), easy management of common resources using extensions..

The 'desktop integration' will use the image identified in the launch file as the desktop or menu item icon.

Sail answered 11/8, 2013 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.