Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 109,491 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,183 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Clock refresh for JFrame

 
Reply to this topicStart new topic

Clock refresh for JFrame

mcamardo
post 2 Jul, 2008 - 02:20 AM
Post #1


New D.I.C Head

*
Joined: 29 Jun, 2008
Posts: 29


My Contributions


I am creating a break clock app in Java from my revious version written in VB6 and what it does is shows the current time, your breaks and luches which you enter (real time) and get a popup notification and tone which you choose to go on your break and then it dings 15 or 30 mins later to come back. I have he frame created and everything listed but the JPanel does not refresh every second or at all. Here is my timer code:

CODE

Timer t = new Timer(1000, Tick());
t.start();

class Tick implements ActionListener
{
  
  public void actionPerformed(ActionEvent event)
  {
    repaint();
  }
}


What I need to refresh is this:

CODE

Calendar time = Calendar.getInstance();
int hour = time.get(Calendar.HOUR_OF_DAY);
int min = time.get(Calendar.MINUTE);
int sec = time.get(Calendar.SECOND);

JPanel panel = new JPanel;

panel.add(new JLabel("The current time is: "));
lblCurrentTime = new JLabel(hour + ":" + min + ":" sec)
panel.add(lblCurrentTime);


This displays fine but does not refresh. I am new to Java and am learning on my own but am very familiar with other programming languages. So if you help a description of the solution would be helpful in me learning as well. Thanks in advance.
User is offlineProfile CardPM

Go to the top of the page


pbl
post 2 Jul, 2008 - 04:22 AM
Post #2


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 1,918



Thanked 113 times

Dream Kudos: 75
My Contributions


CODE

Timer t = new Timer(1000, Tick());
t.start();

class Tick implements ActionListener
{
  
  public void actionPerformed(ActionEvent event)
  {
    repaint();
  }
}


This will repaint Tick

You have to pass the JLabel to repaint to the constructor of Tick

CODE

class Tick implements ActionListener
{
  JLabel label;
  Tick(JLabel lblCurrentTime) {
      label = lblCurrentTime;
  }
  public void actionPerformed(ActionEvent event)
  {
    // do whatever you want to your label here (updating time)
    // don't even need to do a repaint
  }
User is online!Profile CardPM

Go to the top of the page

mcamardo
post 2 Jul, 2008 - 08:01 PM
Post #3


New D.I.C Head

*
Joined: 29 Jun, 2008
Posts: 29


My Contributions


I updated the code to match and I have a new problem:

CODE

class Tick implements ActionListener
{
  JLabel label;
  Tick(JLabel lblCurrentTime)
  {
    label = lblCurrentTime;
  }
  public void actionPerformed(ActionEvent event)
  {
    repaint();
    // using this until I get the rest of the app the way I want it then I will
   // work on a better way (since I will need multiple items to refresh)

  }


The constructor call Tick() is stating it is undefined.

CODE

Timer t = new Timer(1000, Tick());
t.start();


User is offlineProfile CardPM

Go to the top of the page

pbl
post 2 Jul, 2008 - 08:27 PM
Post #4


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 1,918



Thanked 113 times

Dream Kudos: 75
My Contributions


CODE

class Tick implements ActionListener
{
  JLabel label;
  Tick(JLabel lblCurrentTime)
  {
    label = lblCurrentTime;
  }
  public void actionPerformed(ActionEvent event)
  {
    repaint();
  }


You DON'T have to repaint()
In your actionPerformed() get the system time and set your label correctly (to the new time)

QUOTE

The constructor call Tick() is stating it is undefined.


Yes you have to pass to Tick the JLabel to update when the time changes
CODE

Timer t = new Timer(1000, new Tick(lblCurrentTime));
t.start();


User is online!Profile CardPM

Go to the top of the page

mcamardo
post 10 Jul, 2008 - 10:32 PM
Post #5


New D.I.C Head

*
Joined: 29 Jun, 2008
Posts: 29


My Contributions


Ok I removed the repaint(). Here is my code in it's entirty. For some reason it still does not work and I have been pondering why. pbl you have been a great help so far and I know that when I pass this hurtle I can impliment it for the calculations as well. I am using Eclipse as my dev tool if it helps. Thanks again in advance.

CODE

import javax.swing.*;
import java.awt.*;
import java.awt.event.*; // Question? If I have import java.awt.*; is this really necessary?
import java.util.Calendar;

public BreakClock extends JFrame
{
   JLabel lblCurrentTime;

   public static void main (String [] args)
   {
     new BreakClock();
   }
  
   public BreakClock()
   {
     this.setsize(250, 225);
     this.setTitle("Break Clock");
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
     Calendar time = Calendar.getInstance();
    
     int Hour = time.get(Calendar.HOUR_OF_DAY);
     String hour = Integer.toString(Hour);
     if (Hour <= 9)
     {
       hour = hour.trim();
       hour = "0" + hour;
     }

     int Min = time.get(Calendar.MINUTE);
     String min = Integer.toString(Min);
     if (Min <= 9)
     {
       min = min.trim();
       min = "0" + min;
     }

     int Sec = time.get(Calendar.SECOND);
     String sec = Integer.toString(Sec);
     if (sec <= 9)
     {
       sec = sec.trim();
       sec = "0" + sec;
     }
    
     JPanel panel = new JPanel();
    
     panel.add(new JLabel("The current time is: "));
     lblCurrentTime = new JLabel(hour + ":" + min + ":" + sec);
     panel.add(lblCurrentTime);
    
     this.add(panel);
     this.setVisible(true);
    
     Timer t = new Timer(1000, new Tick(lblCurrentTime));
     t.start();
   }
  
   public class Tick implements ActionListener
   {
     JLabel label;
    
     Tick(JLabel lblCurrentTime)
     {
       label = lblCurrentTime;
     }
    
     public void actionPerformed(actionEvent event)
     {
       Calendar time = Calendar.getInstance();
    
       int Hour = time.get(Calendar.HOUR_OF_DAY);
       String hour = Integer.toString(Hour);
       if (Hour <= 9)
       {
         hour = hour.trim();
         hour = "0" + hour;
       }

       int Min = time.get(Calendar.MINUTE);
       String min = Integer.toString(Min);
       if (Min <= 9)
       {
         min = min.trim();
         min = "0" + min;
       }

       int Sec = time.get(Calendar.SECOND);
       String sec = Integer.toString(Sec);
       if (sec <= 9)
       {
         sec = sec.trim();
         sec = "0" + sec;
       }
      
       lblCurrentTime = new JLabel(hour + ":" + min + ":" + sec);
     }
   }
}
User is offlineProfile CardPM

Go to the top of the page

pbl
post 11 Jul, 2008 - 04:22 AM
Post #6


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 1,918



Thanked 113 times

Dream Kudos: 75
My Contributions


In Thick do NOT create a new JLabel... it is not the one that is displayed just update the text of the one you already have.

I also remove the setting of the hour in the main class... duplicate code for nothing I guess you can wait 1 second before seing the click

java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*; // Question? If I have import java.awt.*; is this really necessary?
import java.util.Calendar;

public class BreakClock extends JFrame
{
JLabel lblCurrentTime;

public static void main (String [] args)
{
new BreakClock();
}

public BreakClock()
{
this.setSize(250, 225);
this.setTitle("Break Clock");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Calendar time = Calendar.getInstance();

JPanel panel = new JPanel();

panel.add(new JLabel("The current time is: "));
lblCurrentTime = new JLabel("");
panel.add(lblCurrentTime);

this.add(panel);
this.setVisible(true);

Timer t = new Timer(1000, new Tick(lblCurrentTime));
t.start();
}

class Tick implements ActionListener
{
JLabel label;
Calendar time;

Tick(JLabel lblCurrentTime)
{
time = Calendar.getInstance();
label = lblCurrentTime;
}

public void actionPerformed(ActionEvent event)
{
int Hour = time.get(Calendar.HOUR_OF_DAY);
String hour = Integer.toString(Hour);
if (Hour <= 9)
{
hour = hour.trim();
hour = "0" + hour;
}

int Min = time.get(Calendar.MINUTE);
String min = Integer.toString(Min);
if (Min <= 9)
{
min = min.trim();
min = "0" + min;
}

int sec = time.get(Calendar.SECOND);
String Sec = Integer.toString(sec);
if (sec <= 9)
{
Sec = Sec.trim();
Sec = "0" + sec;
}

lblCurrentTime.setText(hour + ":" + min + ":" + sec);
}
}
}
User is online!Profile CardPM

Go to the top of the page

mcamardo
post 11 Jul, 2008 - 08:16 PM
Post #7


New D.I.C Head

*
Joined: 29 Jun, 2008
Posts: 29


My Contributions


OK I changed the code and it updates just once.. Should I loop the Timer section or is it supposed to update every second? Also it seems that now that the

CODE
Calendar time = Calendar.getInstance();


section is not used in the main section so is it necessary to keep?
User is offlineProfile CardPM

Go to the top of the page

pbl
post 11 Jul, 2008 - 08:31 PM
Post #8


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 1,918



Thanked 113 times

Dream Kudos: 75
My Contributions


QUOTE(mcamardo @ 11 Jul, 2008 - 08:16 PM) *

OK I changed the code and it updates just once.. Should I loop the Timer section or is it supposed to update every second?


Oups... sorry my fault
add that line as the first line of your actionListener:

CODE

public void actionPerformed(ActionEvent event)
{
       time.setTimeInMillis(System.currentTimeMillis());
       int Hour = time.get(Calendar.HOUR_OF_DAY);
       ....


QUOTE

Also it seems that now that the

CODE

Calendar time = Calendar.getInstance();


section is not used in the main section so is it necessary to keep?

You are right... as you dont set the time anymore in the main you do not need the calendar there anymore
User is online!Profile CardPM

Go to the top of the page

mcamardo
post 11 Jul, 2008 - 08:38 PM
Post #9


New D.I.C Head

*
Joined: 29 Jun, 2008
Posts: 29


My Contributions


QUOTE


Oups... sorry my fault
add that line as the first line of your actionListener:

CODE

public void actionPerformed(ActionEvent event)
{
       time.setTimeInMillis(System.currentTimeMillis());
       int Hour = time.get(Calendar.HOUR_OF_DAY);
       ....



pbl you helped me out a great deal in getting this started. You were very helpful in getting it to refresh. bananaman.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/7/08 02:08PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month