#Java swing app, can't set app icon

85 messages · Page 1 of 1 (latest)

vestal bramble
#

I have a java swing app and I want it so that when I run my program (from terminal) and it loads the app the icon will be different other than the default.

But my program keeps failing to find the correct path with bs like Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.net.URL.toString()" because "url" is null even with absolute path & relative path that I think is correct (as I have printed the classpath out)

plush pondBOT
#

This post has been reserved for your question.

Hey @vestal bramble! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

frozen fern
#

don't close it xD
can you also share your file structure

vestal bramble
#

Tryna figure out how best to

#

The class is <project-root>src/com/Schoolio/MainWindow

frozen fern
#

a screenshot works, In theory I just need the resource folder

vestal bramble
#

It's the hamster image I want rn

#

In mainwindow I call String ICON_PATH = "/com/Schoolio/views/hamster.jpeg"; final Toolkit defaultToolkit = Toolkit.getDefaultToolkit(); final URL imageResource = Main.class.getClassLoader().getResource(ICON_PATH); final Image image = defaultToolkit.getImage(imageResource);

frozen fern
vestal bramble
#

Yh

frozen fern
#

then you haven't configured your resource path properly, from the looks of it you are currently in the source folder

vestal bramble
#

I am

#

Images have to be in the resource folder?

frozen fern
#

Preferably yes

#

Do you use a build tool like maven or gradle ?

vestal bramble
#

Gradle yes

#

gradle run to start it

frozen fern
#

first of all create a new folder called resources or whatever, move your image in there.
Then show me your build.gradle file

vestal bramble
#

Ok

#

Gradle

frozen fern
#

you are still in the source folder

vestal bramble
#

yh

frozen fern
#

create a new folder outside the source folder

#

or move your existing one there.

vestal bramble
#

Ok done

#

resources folder at the root

frozen fern
#

now edit your sourceSets to the following:

sourceSets {
        main {
                java {
                        srcDirs= ['java']
                }
                resources {
                        srcDirs= ['resources']
                }
        }
}
vestal bramble
#

Kaptain Iglo is called Bird's Eye in the uk

#

Now what?

frozen fern
vestal bramble
#

Could not find or load main class com.Schoolio.Launcher

frozen fern
# vestal bramble Now what?

Your path should be /hamster.jpeg when loading form the class and hamster.jpeg when loading from the classloader

vestal bramble
#

Omg no way

#

At last

#

That's been a 7 hour task

frozen fern
#

i mean I could be kind of rude and just say something along the lines of read the documentation but we are going to pretend I didn't say that xD

vestal bramble
#

Which documentation Java swing?

#

Or gradle

#

Also it takes about 3 seconds to change from the default app icon to my hamster, which is a bit of a shame

frozen fern
frozen fern
#

That should speed things up

vestal bramble
#

It's often about knowing thte right questions to ask

vestal bramble
frozen fern
#

that's true.

#

when configuring your JFrame basically

vestal bramble
#

Mm it is essentially the first thing I do on the jframe

#

Oh actually ik what it miht be

#

Haha

#

It was my authentication was running first

#

Taking like 2 seconds to get a response

frozen fern
#

your authentication is running on the EDT ?

vestal bramble
#

Eau de toilette

frozen fern
#

exactly no, EDT = Event Dispatch Thread the thread swing uses to send events and configure its components

vestal bramble
#

Nah my auth is just running in my class

#

I'm a PHP dev I'm not used to lots of this low level stuff

#

It's running in my mainwindow class which creates the JFrame

frozen fern
#

but you aren't calling your class with something like SwingUtilities.invokeLater()

vestal bramble
#

Indeed i am not

frozen fern
#

alright then nothing to worry about.

vestal bramble
#

Oh no i am actully

#

I am calling the MainWindow class whihc has the ui and the auth through that

frozen fern
#

yeah maybe don't do that.

#

At least leave the authentication outside of the swing thread.

vestal bramble
#

So leave the invokelater but make a new thread to do the auth

frozen fern
#

well you could just quickly try if the Jframe speeds up when you don't use invokeLater

vestal bramble
#

Yh it does

#

Which is gud

frozen fern
#

you don't really need invokeLater because setting the frame to visible should repaint everything anyways

vestal bramble
#

Idrk what it did anyway

#

But one last thing

#

Do I need to move the authentication stuff into a separate thread somehow, to speed up load times of new frames n stuff

#

And new JPanels

frozen fern
#

as long as you don't use invokeLater your main thread and the swing thread should run in parallel. So I would say you don't. If you have problems with panels not refreshing, call paint on the panel or the frame and that should hopefully fix that

vestal bramble
#

Do I need to explicitly declare a main and swingg thread or java does it for me?

frozen fern
#

java does that for you. Btw don't use show its deprecated. Use setVisible(true) instead.

vestal bramble
#

Yh show actually just calls setVisible

#

Thanks Fish Finger, have a nice rest of ur day

plush pondBOT