Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Timer & JLabel

Thread view: 
Joseph  Gruber - 25 Jun 2007 02:24 GMT
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...
Tom Hawtin - 25 Jun 2007 06:56 GMT
Joseph Gruber wrote:

>   java.util.Timer t = new java.util.Timer();
>   t.scheduleAtFixedRate(new java.util.TimerTask() {
[quoted text clipped - 8 lines]
> through the NetBeans GUI.  It is in the variables declaration as
> "private static javax.swing.JLabel jLabel1;"

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
Joseph  Gruber - 25 Jun 2007 11:14 GMT
> jLabel1 isn't a great name for a variable.
>
[quoted text clipped - 7 lines]
>
> 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.
Tom Hawtin - 25 Jun 2007 11:26 GMT
Joseph Gruber wrote:

> 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
Joseph  Gruber - 25 Jun 2007 11:40 GMT
> 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?
Joseph  Gruber - 25 Jun 2007 11:52 GMT
> > 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
[quoted text clipped - 4 lines]
> I'm new to Java so pardon me.  Can you explain what you mean by the
> EDT?

Figured it out :)

For those who may stumble upon this the following webpage helped a
lot: http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
Tom Hawtin - 25 Jun 2007 11:55 GMT
Joseph Gruber wrote:

> 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
Roedy Green - 25 Jun 2007 12:11 GMT
On Mon, 25 Jun 2007 01:24:06 -0000, Joseph  Gruber
<joseph.gruber@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>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" );
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.