Timer & JLabel

J

Joseph Gruber

Hi.

I am trying to create a clock on my GUI application but am getting a
NullPointerException error whenever the app runs. This is the code:

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new frmMain().setVisible(true);
}
});

java.util.Timer t = new java.util.Timer();
t.scheduleAtFixedRate(new java.util.TimerTask() {
public void run() {
jLabel1.setText("Test");
}
}, 0, 500);
}

-------
This was generated using NetBeans and the jLabel has been added
through the NetBeans GUI. It is in the variables declaration as
"private static javax.swing.JLabel jLabel1;"

Any ideas what I'm doing wrong? I assume I'm referencing jLabel1
incorrectly somehow...
 
T

Tom Hawtin

Joseph said:
java.util.Timer t = new java.util.Timer();
t.scheduleAtFixedRate(new java.util.TimerTask() {
public void run() {
jLabel1.setText("Test");
}
}, 0, 500);
}

jLabel1 isn't a great name for a variable.

Avoid static variables.

Swing should be used only from the EDT, so:

Don't assign Swing objects to statics.

Replace java.util.Timer with javax.swing.Timer.

Tom Hawtin
 
J

Joseph Gruber

jLabel1 isn't a great name for a variable.
Avoid static variables.

Swing should be used only from the EDT, so:

Don't assign Swing objects to statics.

Replace java.util.Timer with javax.swing.Timer.

Tom Hawtin

This is more of a test so the variable name was left alone. I've used
the javax.swing.Timer and that works perfectly but I need more control
over when the timer fires. With the Swing Timer the clock lags behind
every once in a while due to the overhead in the Swing Timer whereas
the java.util.Timer doesn't.
 
T

Tom Hawtin

Joseph said:
This is more of a test so the variable name was left alone.

If you have got a problem with your code, it generally helps if what is
there is as clear as possible.
I've used
the javax.swing.Timer and that works perfectly but I need more control
over when the timer fires. With the Swing Timer the clock lags behind
every once in a while due to the overhead in the Swing Timer whereas
the java.util.Timer doesn't.

That's no excuse. You must be in the Event Dispatch Thread (EDT) when
manipulating Swing components. I guess what you are finding is that the
EDT is busy. That's not going to be a good point to break the EDT rule.

Tom Hawtin
 
J

Joseph Gruber

That's no excuse. You must be in the Event Dispatch Thread (EDT) when
manipulating Swing components. I guess what you are finding is that the
EDT is busy. That's not going to be a good point to break the EDT rule.

Tom Hawtin

I'm new to Java so pardon me. Can you explain what you mean by the
EDT?
 
T

Tom Hawtin

Joseph said:
I'm new to Java so pardon me. Can you explain what you mean by the
EDT?

The Event Dispatch Thread (EDT) is the thread that all AWT events
dispatched in. If you run your code as an applet or under WebStart, you
will have multiple EDTs. However, only one EDT will be visible to your code.

Like the vast majority of GUI toolkits, Swing is not multithreaded. You
need to ensure that Swing objects are only used on a single thread (the
EDT). To execute code on the EDT from a non-EDT thread, use
java.awt.EventQueue.invokeLater (or possibly invokeAndWait). This is
what your original example correctly did within the main method.

Tom Hawtin
 
R

Roedy Green

This was generated using NetBeans and the jLabel has been added
through the NetBeans GUI. It is in the variables declaration as
"private static javax.swing.JLabel jLabel1;"

where is your code that says jLabel1 = new JLabel( "xxx" );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top