The Artima Developer Community
Sponsored Link

Java Answers Forum
problems with swing and threads

5 replies on 1 page. Most recent reply: Dec 9, 2005 2:44 AM by Kondwani Mkandawire

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Andres Corral

Posts: 3
Nickname: iampiti
Registered: Jan, 2003

problems with swing and threads Posted: Dec 7, 2005 4:38 AM
Reply to this message Reply
Advertisement
Hi!.
I'm new to swing so I thought I would try something simple like a clock.
So the idea is a javax.swing.Timer that periodically checks if the time has changed and, if so calls a paintTime in a JFrame that displays the new time. The problem is that it doesn't work. But if I add a button in the frame that calls paintTime when clicked it does work.
Both when I click the button and when the timer calls it paintTime executes in the event dispatching thread.
My code is this:


//ClockView.java
public class ClockView {
...
public void paintTime(Calendar cal)
{
//we just make a setText in a couple of JLables here
}
}

//Clock.java
import java.awt.event.*;
import javax.swing.Timer;
import java.util.Calendar;

public class Clock implements ActionListener
{
private Timer myTimer;
private ClockView view;
private Calendar time;
private int previousMinutes,currentMinutes;

Clock(ClockView view,int delay)
{
myTimer=new Timer(delay,this);
myTimer.setInitialDelay(0);
this.view=view;
}


public void actionPerformed(ActionEvent e)
{
System.out.println("actionPerformed!");
time=Calendar.getInstance();
currentMinutes=time.get(Calendar.MINUTE);
if (currentMinutes!=previousMinutes)
{
System.out.println("inside condition");
previousMinutes=currentMinutes;
view.paintTime(time);
}
}

public void start()
{
myTimer.start();
}

public void stop()
{
myTimer.stop();
}
}


Any ideas?
Thanks a lot!


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: problems with swing and threads Posted: Dec 8, 2005 2:56 AM
Reply to this message Reply
> Hi!.
> I'm new to swing so I thought I would try something simple
> like a clock.
> So the idea is a javax.swing.Timer that periodically
> checks if the time has changed and, if so calls a
> paintTime in a JFrame that displays the new time. The
> problem is that it doesn't work. But if I add a button in
> the frame that calls paintTime when clicked it does work.
>
> Both when I click the button and when the timer calls it
> paintTime executes in the event dispatching thread.
> My code is this:
>
>
> //ClockView.java
> public class ClockView {
> ...
> public void paintTime(Calendar cal)
> {
> //we just make a setText in a couple of JLables here
> }
> }
>
> //Clock.java
> import java.awt.event.*;
> import javax.swing.Timer;
> import java.util.Calendar;
>
> public class Clock implements ActionListener
> {
> private Timer myTimer;
> private ClockView view;
> private Calendar time;
> private int previousMinutes,currentMinutes;
>
> Clock(ClockView view,int delay)
> {
> myTimer=new Timer(delay,this);
> myTimer.setInitialDelay(0);
> this.view=view;
> }
>
>
> public void actionPerformed(ActionEvent e)
> {
> System.out.println("actionPerformed!");
> time=Calendar.getInstance();
> currentMinutes=time.get(Calendar.MINUTE);
> if (currentMinutes!=previousMinutes)
> {
> System.out.println("inside condition");
> previousMinutes=currentMinutes;
> view.paintTime(time);
> }
> }
>
> public void start()
> {
> myTimer.start();
> }
>
> public void stop()
> {
> myTimer.stop();
> }
> }
>

>
> Any ideas?
> Thanks a lot!

I assume you call your start method from your main.
Short of that I don't see why it should not work...
public static void main(String []args){
    new Clock().start();
}

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: problems with swing and threads Posted: Dec 8, 2005 3:01 AM
Reply to this message Reply
Sorry I meant:

new Clock(view, 1000).start();

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: problems with swing and threads Posted: Dec 8, 2005 3:02 AM
Reply to this message Reply
> Sorry I meant:
>
> new Clock(view, 1000).start();


Your action is fired off every 1 second, this is similar
to using a button that has an ActionListener that listens
to a click every one second.

Andres Corral

Posts: 3
Nickname: iampiti
Registered: Jan, 2003

Re: problems with swing and threads Posted: Dec 8, 2005 8:12 AM
Reply to this message Reply
> Your action is fired off every 1 second, this is similar
> to using a button that has an ActionListener that listens
> to a click every one second.

Yeah, that's what I thought and why I asked :).

I read somewhere that any code that operates on swing objects has to be executed from the event-dispatching thread, but in both cases my code executes in it (checked).

Thanks a lot anyway.

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: problems with swing and threads Posted: Dec 9, 2005 2:44 AM
Reply to this message Reply
I'm looking at your code and looking at older code I've
written (Splash Screen) that made use of the swing Timer
class.

You have:

myTimer=new Timer(delay,this);

I think in fact the Timer class is meant to generate
a new Instance of the ActionListener being called.

e.g.

myTimer = new Timer(delay, new ActionListenerDeclared());

I may be wrong but that's the only difference I can see
with your implementation and what drives my Splash Screen.
Not to mention it makes sense from the stand point that
its a new Action each time.

maybe try:

myTimer = new Timer(delay, new Clock());

This will probably work. As opposed to calling
"this" takes in a new ActionListener.

Flat View: This topic has 5 replies on 1 page
Topic: Help Cleaning Code Previous Topic   Next Topic Topic: calling a stored procedure on AS400

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use