Clock help

View: New views
7 Messages — Rating Filter:   Alert me  

Clock help

by Doctor Sup :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I am very new to Netbeans and java programming in general so excuse my ignorance, but i am trying to implement a simple digital clock via a Jlabel but when i attempt to add a TimerBean it says "Components from the default package cannot be used outside of the default pakage" what does this mean? and more importantly how can i implement this clock ( as a basic decktop aplication ).

Thankyou for any help.

Re: Clock help

by joshis :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi!

Do you mean something like this (the simplest/likely-dumbest
implementation possible)? You might play with the appearance, output
format, etc... The key portion of code:

    public NewJFrame() {
        initComponents();
        jLabel1.setText(Calendar.getInstance().getTime().toString());

        _iTimer = new Timer(true);
        _iTimer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
               
jLabel1.setText(Calendar.getInstance().getTime().toString());
            }
        }, 1000, 1000);
    }

With regards,

Petr Dvorak

Doctor Sup wrote:
> I am very new to Netbeans and java programming in general so excuse my
> ignorance, but i am trying to implement a simple digital clock via a Jlabel
> but when i attempt to add a TimerBean it says "Components from the default
> package cannot be used outside of the default pakage" what does this mean?
> and more importantly how can i implement this clock ( as a basic decktop
> aplication ).
>
> Thankyou for any help.
>  



clock.zip (20K) Download Attachment

Re: Clock help

by Johnny Kewl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


----- Original Message -----
From: "Doctor Sup" <kreitals@...>
To: <nbusers@...>
Sent: Thursday, October 09, 2008 7:51 PM
Subject: [nbusers] Clock help


>
> I am very new to Netbeans and java programming in general so excuse my
> ignorance, but i am trying to implement a simple digital clock via a
> Jlabel
> but when i attempt to add a TimerBean it says "Components from the default
> package cannot be used outside of the default pakage" what does this mean?
> and more importantly how can i implement this clock ( as a basic decktop
> aplication ).
>
> Thankyou for any help.

Doctor, sounds like its just bitchin about packages...
Its not good practive to stick stuff in the default package... so do this...

Right click on your source folder... new package and call it...
com.myclock.test

anything... idea is that its unique to you..

Then drag you application to the new package... it will ask you to
refactor... say ok, let it do its thing...

... should be happy...

 ---------------------------------------------------------------------------
HARBOR : http://www.kewlstuff.co.za/index.htm
The most powerful application server on earth.
The only real POJO Application Server.
See it in Action : http://www.kewlstuff.co.za/cd_tut_swf/whatisejb1.htm
---------------------------------------------------------------------------
If you cant pay in gold... get lost...


Re: Clock help

by Gregg Wonderly-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Petr Dvorak wrote:

> Do you mean something like this (the simplest/likely-dumbest
> implementation possible)? You might play with the appearance, output
> format, etc... The key portion of code:
>
>    public NewJFrame() {
>        initComponents();
>        jLabel1.setText(Calendar.getInstance().getTime().toString());
>
>        _iTimer = new Timer(true);
>        _iTimer.scheduleAtFixedRate(new TimerTask() {
>
>            @Override
>            public void run() {  
> jLabel1.setText(Calendar.getInstance().getTime().toString());
>            }
>        }, 1000, 1000);
>    }

You must code this with some variant of the following,

             public void run() {
                SwingUtilities.invokeAndLater( new Runnable() { public void run() {
  jLabel1.setText(Calendar.getInstance().getTime().toString());
                } } );
             }

to make sure that you don't futz with the AWT/swing environment?  A non event
thread should never be calling into any GUI component methods!  You could use
invokeAndWait() as well, but that requires handling interrupted exceptions.

Gregg Wonderly

Re: Clock help

by Petr Dvorak :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Gregg Wonderly wrote:

> Petr Dvorak wrote:
>> Do you mean something like this (the simplest/likely-dumbest
>> implementation possible)? You might play with the appearance, output
>> format, etc... The key portion of code:
>>
>>    public NewJFrame() {
>>        initComponents();
>>        jLabel1.setText(Calendar.getInstance().getTime().toString());
>>
>>        _iTimer = new Timer(true);
>>        _iTimer.scheduleAtFixedRate(new TimerTask() {
>>
>>            @Override
>>            public void run() {          
>> jLabel1.setText(Calendar.getInstance().getTime().toString());
>>            }
>>        }, 1000, 1000);
>>    }
>
> You must code this with some variant of the following,
>
>             public void run() {
>         SwingUtilities.invokeAndLater( new Runnable() { public void
> run() {
>          jLabel1.setText(Calendar.getInstance().getTime().toString());
>         } } );
>             }
>
> to make sure that you don't futz with the AWT/swing environment?  A
> non event thread should never be calling into any GUI component
> methods!  You could use invokeAndWait() as well, but that requires
> handling interrupted exceptions.
>
> Gregg Wonderly

Thanks for the good remark (suggestion to use of
SwingUtilities.invokeLater) - I said it is simplest and likely-dumbest
implementation possible. I still think that use of my simple code is
good enough for this demo app (the application should do nothing more
that the clock), but generally you are right... P.D.

        _iTimer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                SwingUtilities.invokeLater(new Runnable() {

                    public void run() {
                       
jLabel1.setText(Calendar.getInstance().getTime().toString());
                    }
                });

            }
        }, 1000, 1000);


Re: Clock help

by Gregg Wonderly-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Petr Dvorak wrote:

> Gregg Wonderly wrote:
>> Petr Dvorak wrote:
>>> Do you mean something like this (the simplest/likely-dumbest
>>> implementation possible)? You might play with the appearance, output
>>> format, etc... The key portion of code:
>>>
>>>    public NewJFrame() {
>>>        initComponents();
>>>        jLabel1.setText(Calendar.getInstance().getTime().toString());
>>>
>>>        _iTimer = new Timer(true);
>>>        _iTimer.scheduleAtFixedRate(new TimerTask() {
>>>
>>>            @Override
>>>            public void run() {          
>>> jLabel1.setText(Calendar.getInstance().getTime().toString());
>>>            }
>>>        }, 1000, 1000);
>>>    }
>>
>> You must code this with some variant of the following,
>>
>>             public void run() {
>>         SwingUtilities.invokeAndLater( new Runnable() { public void
>> run() {
>>          jLabel1.setText(Calendar.getInstance().getTime().toString());
>>         } } );
>>             }
>>
>> to make sure that you don't futz with the AWT/swing environment?  A
>> non event thread should never be calling into any GUI component
>> methods!  You could use invokeAndWait() as well, but that requires
>> handling interrupted exceptions.
>>
>> Gregg Wonderly
>
> Thanks for the good remark (suggestion to use of
> SwingUtilities.invokeLater) - I said it is simplest and likely-dumbest
> implementation possible. I still think that use of my simple code is
> good enough for this demo app (the application should do nothing more
> that the clock), but generally you are right...

What happens Petr, is that examples turn into "demos" which turn into "small
examples" which get to have a life of their own as "the way" to do things.

The Swing/AWT platform programming model (or lack thereof) causes this small
detail of "context management" to become a real headache for those that don't
know that it is a requirement for GUI applications to manage this issue.

I think its best to deal with this issue up front to make sure people go down
the right paths of abstracting their interactions with the real world vs
updating components so that they have this one knocked out from the start.

Maybe someday we'll get a GUI frame work design that actually hides this so that
methods like this and classes like SwingWorker are a distant memory...

Gregg Wonderly

Re: Clock help

by Doctor Sup :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Firstly thankyou all for your advise, I have been able to implement an instance of time but i cannot get it to update. Petr using your code works but opens a new JFrame, if i try to modify this to simply update the JLabel i wish it to i either only get the current instance of time or too many errors i cannot comprehend. Similarly with that of Gregg, invokeLater seems to cause some issues. Any further advice would be great.

Sorry for my ignorance i have spent most of my time coding almost exclusivelyt in C and Prolog, this is a little hard to get used to not to mention the extensive modulation and abstraction...


Doctor Sup wrote:
I am very new to Netbeans and java programming in general so excuse my ignorance, but i am trying to implement a simple digital clock via a Jlabel but when i attempt to add a TimerBean it says "Components from the default package cannot be used outside of the default pakage" what does this mean? and more importantly how can i implement this clock ( as a basic decktop aplication ).

Thankyou for any help.