Two things come to find. The first is, as you say, you're trying to reference a non-static variable from a static context.
The second is, you don't seem to understand how threading works...
Basically, main
is typically executed within the "main" thread (when executed by the JVM).
You then use EventQueue.invokeLater
. Which as, the name suggests, will execute the Runnable
"later"...at some time in the future...
EventQueue.invokeLater(new Runnable() {
public void run() {
You then try and change the the icon (let's pass over the non-static reference for a momement)...but lblStatusImg
won't have been initialized nor is it likely to have been displayed, as the Runnable
has not yet been executed, meaning, even if you didn't run into a NullPointerException
, you won't see the change...
You can test by adding a System.out
in your Runnable
and before the first lblStatusImg.setIcon
call in the main
method.
What you should do is...
- Move the "status" change change to within the
Runnable
context. - Provide a
setStatus
method that is capable of changing the label and UI content as required based on the provide status
For example...
public static final int SUCCESS = 0;
public static final int FAIL = 0;
//...
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StackOverflow_Image_Resource_Demo window = new StackOverflow_Image_Resource_Demo();
// This e
window.frmUpdate.setVisible(true);
window.setStatus(StackOverflow_Image_Resource_Demo.SUCCESS);
} catch (Exception e) {
e.printStackTrace();
Component frame = null;
window.setStatus(StackOverflow_Image_Resource_Demo.FAIL);
JOptionPane.showMessageDialog(frame, "Update Failed", "Update Failed", JOptionPane.ERROR_MESSAGE);
window.dispose();
}
}
});
}
You should avoid exposing instance fields as public
and instead, provide methods that either change their state indirectly (such as setStatus
) or directly (setStatusIcon
). In this case, I prefer the first method as this allows the class to determine what a change in status actually means.