Making a clock that updates every second.

I am at a loss and quitting for the night if anyone could help point me in the right direction I would appreciatte the advice. Here goes the code
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class Clock
public static void main(String[] args)
TimerTask task = new DateTimer();
Timer timer = new Timer();
timer.schedule(task, 0, 1000);
JFrame frame = new JFrame();
JButton button = new JButton("Date & Time");
frame.add(button);
final JLabel label = new JLabel("Currently: ");
JPanel panel = new JPanel();
panel.add(button);
panel.add(label);
frame.add(panel);
class ClockViewer implements ActionListener
public void actionPerformed(ActionEvent event)
label.setText("Currently: " + task.getDate());
ActionListener listener = new ClockViewer();
button.addActionListener(listener);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
private static final int FRAME_WIDTH = 500;
private static final int FRAME_HEIGHT = 100;
import java.util.*;
public class DateTimer extends TimerTask
public void run()
Date now = new Date();
public Date getDate()
return now;
private final Date now;
Basically I am confused how to print the task out in the label.

Hi,
I have used [javax.swing.Timer|http://java.sun.com/javase/6/docs/api/index.html]
Hope this will be useful to you.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Clock {
     public static void main(String[] args) {          
          JFrame frame = new JFrame();
          JButton button = new JButton("Date & Time");
          frame.add(button);
          final JLabel label = new JLabel("Currently: ");
          JPanel panel = new JPanel();
          panel.add(button);
          panel.add(label);
          frame.add(panel);
          ActionListener listener = new ActionListener()
               public void actionPerformed(ActionEvent ae)
                    new Timer(1000,this).start();
                    label.setText("Currently : " + new Date().toString());
          button.addActionListener(listener);
          frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
     private static final int FRAME_WIDTH = 500;
     private static final int FRAME_HEIGHT = 100;
}Thanks

Similar Messages

  • How to read two files (one is updating every second, the other is constant) simoltaneously

    Dear All,
    I will appreciate if somebody help me.
    I want to read two files, one is temperature wich is updating every second, and the other is hysteresis energy which has specific rows and is constant.
    I got a program in discussion forum which read a file as it is updating. I checked this program in my case and it works.
    Then I added reading hysteresis energy file to the existing file. But while i am running the program, the hysteresis energy file is read with all rows.
    But the aim is that everytime which temperature updates, I need to read just one row of the hysteresis energy file.
    so, in this way in every second i have a new temperature data and one row of the other file.
    I tried to used "for loop" inside the program, but it did not work, becuase reading temperature will be stopped untill for loop ends.
    I attached the program.
    Could somebody help me how to read just one row of hysteresis energy file everytime temperature updates?
    Solved!
    Go to Solution.
    Attachments:
    Readfiles.vi ‏23 KB

    I do not understand relationship between your two files.
    You have another VI or app that is writing data to both files and when new temperature data added you wish just to read the new data and then read one new line of the hysteresis file?????
    But is one is constant, why do you need to keep reading it?
    Anyway…
    The “Read From Spreadsheet File.vi” preformed an Open and a Close for each time it is called (if you will open the VI up and drill down and you will see). So that resets the file pointer back to the beginning and there for will read in the whole file from the first to the last each time if you do not set the start of read offset or number of rows.
    Now the Read from Binary File does not reset the file pointer and leaves the file pointer just past the last byte read so it will start with any new bytes automaicly only giving you the new data.
    I do not think you need to do the math with the Previous EOF=0 and the File size to get what I think you are trying to do.
    Plus use shift reg for the Data string instead of local var
    Set the mechinacl action of the stop botton to Latched
    So you do not need to init the two controls
    Also, I do not get the End of file encountered error (4) from the Read from Binary file if I attempt to read past. It just returns and empty string.
    Omar
    Attachments:
    Readfiles.vi ‏22 KB

  • Macro in Pages that deletes every second line?

    IM looking for a macro to run in pages that deletes every second line.
    I want to be able to run it though tue document so that starting at the 1st line, it deletes lines 1,3,5,7,9,11 etc all the way to the end.
    Anyone know what I can do? Perhaps a working AppleScript/programme? Had no luck as of yet.
    Thanks

    http://www.adobe.com/education/instruction/webtech/CS2/unit_planning1/idb_paragraph_format .htm -- it's for CS2, but it's unchanged since then.

  • Have anybody experienced a frog like sounds that keeps burping on macbook? Its a very annoying sounds that burping every seconds.

    Have anybody experienced a frog like sounds that keeps burping on macbook? Its a very annoying sounds that burping every seconds.

    Did you ever find a solution to this problem because I have it too. I have gotten used to it when listening to movies etc., although it can startle me, but I find it very irritating when I am trying to record something off my computer, like a quiet meditation, onto a voice memo, and this happens…I have to start over and hope it doesn't keep happening.

  • Cursor only updates every second row

    Hello Helpful Peoples
    I have a table with a set of delivery dates, and am wanting to add a date on which a reminder flag should be raised by the system. The delivery dates are already populated and I'm wanting to add functionality where the end user can set how many days prior they are reminded.
    I've opened a cursor to retrieve all the current due dates from my source table. When I loop through this cursor and update the reminder date and reminder text, my code is only updating every second row.
    I can't work out why this code seems to skip a row every time it updates a record. Is anyone able to assist?
    declare
    v_date rep_delivery.reminderdate%type;
    v_rownum number;
    CURSOR reminder_cur
    IS
    SELECT DEL.DUEDATE
    FROM REP_DELIVERY DEL
    WHERE DEL.REP_ID = :P212_REP_ID:
    FOR UPDATE OF DEL.REMINDERDATE;
    begin
    FOR x IN reminder_cur LOOP
    FETCH reminder_cur INTO v_date;
    EXIT WHEN reminder_cur%NOTFOUND;
    v_date := v_date - to_number(:P212_REMDAYS);
    UPDATE REP_DELIVERY DEL2
    SET DEL2.REMINDERDATE = v_date
    WHERE CURRENT OF reminder_cur;
    UPDATE REP_DELIVERY DEL3
    SET DEL3.REMINDERTEXT = :P212_DESCRIPTION
    WHERE CURRENT OF reminder_cur;
    END LOOP;
    commit;
    end;

    Oolite,
    Use this code:
    DECLARE
       v_date   rep_delivery.reminderdate%TYPE;
       CURSOR reminder_cur
       IS
          SELECT        ROWID, duedate
                   FROM rep_delivery
                  WHERE rep_id = :p212_rep_id
          FOR UPDATE OF reminderdate;
    BEGIN
       FOR x IN reminder_cur
       LOOP
          v_date := x.duedate;
          v_date := v_date - TO_NUMBER (:p212_remdays);
          UPDATE rep_delivery del2
             SET del2.reminderdate = v_date,
                 del2.remindertext = :p212_description
           WHERE ROWID = x.ROWID;
       END LOOP;
       COMMIT;
    END;and it will work for you.
    Denes Kubicek
    http://deneskubicek.blogspot.com/
    http://htmldb.oracle.com/pls/otn/f?p=31517:1
    -------------------------------------------------------------------

  • Small clock that appears every few seconds

    hi! i just had my first bb - curve 9300.. i must've done something in the settings or something, cause there's this annoying small clock that keeps appearing every few seconds, which makes my phone work slowly, how do i remove it? thanks!!

    candie026 wrote:
    hi! i just had my first bb - curve 9300.. i must've done something in the settings or something, cause there's this annoying small clock that keeps appearing every few seconds, which makes my phone work slowly, how do i remove it? thanks!!
    To prevent freezing or lagging/slow response on any BlackBerry, the most important practice you can adopt is to be sure to CLOSE applications when you are finished using them.
    If you just hit the red "end call" key to go back to the home screen the application actually stays open and running in the background. If you do this repeatedly you will have lots of apps open and consequently your memory will get used up. Lack of memory is the biggest reason your BlackBerry slows to a crawl or locks up. If you get into the habit of actually using the menu key to close the applications this will at least slow down this memory-clogging process. You still may have to do a battery pull or use the "Quickpull" app once in a while when things get slow.
    To check what applications are running on your device in the background, press the Menu key then choose Switch Applications. You will see the icons of each application there.
    So now, check your applications running in the background. There are commonly four or five applications that will always be running (Messages, Call Logs/Phone, BlackBerry Messenger, Homescreen, and the Browser; there are other third party applications such as BeeJive, and BlackBerry Alerts which will also run in the background which you cannot close if you want them to operate). Make certain that the browser is NOT on an active webpage (open the browser and choose Menu > Close). Close any other applications that do not need to be running (the camera or a game you were playing or Google Maps).
    1. If any post helps you please click the below the post(s) that helped you.
    2. Please resolve your thread by marking the post "Solution?" which solved it for you!
    3. Install free BlackBerry Protect today for backups of contacts and data.
    4. Guide to Unlocking your BlackBerry & Unlock Codes
    Join our BBM Channels (Beta)
    BlackBerry Support Forums Channel
    PIN: C0001B7B4   Display/Scan Bar Code
    Knowledge Base Updates
    PIN: C0005A9AA   Display/Scan Bar Code

  • 1080p 25fps exported to 1080p 23.98fps uncompressed Quicktime that stutters every second

    I am authoring a blu-ray and the QuickTime file of the feature is 25fps. I didn't think it would be a big deal and just set Premiere to export the 25fps timeline to a 1080p uncompressed QuickTime at 23.98 but the picture hiccups every second like clockwork.
    Can anyone tell me how to get around the frame skip hiccup when converting a PAL 25FPS HD file to a 23.98? Thanks in advance!

    is retiming an option?
    in the 90's when I was doing commerical dvd authoring in Australia (PAL) our masters were often from NTSC telecine transfers.
    We re-interpreted the video as 25 from 24 and then speed and pitch shifted the audio.
    It's a pretty common method. For example the NTSC DVD release of "The Thomas Crown Affair" (Brosnon version) obviously used 25fps audio retimed down to 24fps for the titles and then a different audio file at 24fps for the feature because the loop music in the title matches the opening credit music and you can hear it drop pitch when you go from the menu to the feature.
    If that isn't an option then you're pretty much going to have to put up with the stutter since you're dropping 1 in 25 frames on the floor.

  • Updating JTable every second, want to ignore these events in my listener

    I have a JTable that I update every second using
    table.getModel().setValueAt(data, row, col);I'm also reacting to when the user clicks on a row in a the table, using a ListSelectionListener:
    public class MyTableListener implements ListSelectionListener {
              public void valueChanged(ListSelectionEvent e) {
                   if (e.getValueIsAdjusting()) return;
                   //do stuff
    }And of course I've done this:
    table.getSelectionModel.addListSelectionListener(new MyTableListener());Problem is, every time I update the table data it generates an event handled by valueChanged() above. I need to prevent this.
    Do I need a customized table model for this?
    Thanks!

    Found the problem: I forgot I was using JXTable, not JTable. Here's my example:
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
    import java.awt.*;
    import org.jdesktop.swingx.*;
    public class Test {
         JXTable table = null;//change this to JTable to fix
         public class MyTableListener implements ListSelectionListener {
              public void valueChanged(ListSelectionEvent e) {
                   System.out.println("event="+e.toString());
         public Test()
              JFrame frame  = new JFrame();
              String columns[] = {"one", "two"};
              Object data[][] = {{0, 0}, {0, 0}, {0, 0}};
              table = new JXTable(data, columns);//change this to JTable to fix
              table.getSelectionModel().addListSelectionListener(new MyTableListener());
              frame.add(new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
                          BorderLayout.CENTER);
              startThread();
              frame.setVisible(true);
         public static void main (String args[]) {
              Test test = new Test();
         public void startThread()
              class RefreshThread extends Thread {
                   public void run() { try {
                             while(true) { SwingUtilities.invokeLater(new Runnable() {public void run() {
                                       updateTable();
                                  sleep(1*1000);
                        } catch (Exception e) {e.printStackTrace();}
              RefreshThread rt = new RefreshThread();
              rt.start();
         public void updateTable()
              TableModel model = table.getModel();
              try {
                   for(int row = 0; row < model.getRowCount(); row++)
                        model.setValueAt(System.currentTimeMillis(), row, 0);
                        model.setValueAt(System.currentTimeMillis(), row, 1);
              } catch (Exception e) {
                   e.printStackTrace();
    }When I change it to a JTable, valueChanged() is not called every time I programmatically update the table.
    If it's a JXTable, valueChanged() is called with every update. Annoying!
    To compile/run the JXTable version you'll need something like
    java -cp swingx-1.0.jar:. TestAs for the events themselves, I added print statements with e.toString() and the events are not distinguishable from user interaction. I wonder if this is a JXTable bug or feature.
    Thanks!

  • IOS update image every second?

    Hello,
    I am very new to app development, so apologies for the simple questions.
    I have an image on a remote server of which updates every second, now I would like to put that image in a view in an app and have it effectively refresh every second.
    Currently its done using javascript and I just reference the image on the remote server with a time stamp on the end of the image like:
    image.jpg?Tue Jul 03 2012 10:57:08 GMT+0100 (GMT Daylight Time)
    Now I have this working with a local html file and a webview, I just wanted and think it should be possible using native iOS objects like the timer and image view.
    Just not sure how to achieve this.
    Any help is much appreciated.
    Thanks

    Any one?

  • Help making a clock

    Hey,
    I was hoping to get some help making a clock update for me, but I don't want just to show the actual date, I know how to do that. What I want is to be able to set a time, and then the clock start running from then. For instance, after I enter Jan 3rd, 2005 at 3:43 as my time and date, I want the clock to start from there and update every second. Does that make sense?
    Could someone help me out where to look to get this to happen? Thanks!

    I think the OP wants to be able to set a time OTHER
    than the current time. He doesn't want to wait
    untill the time you entered to have the clock tick,
    he wants to have it tick immediately from the "fake"
    time that you've entered.
    At least that's how I understood him. The offset is
    the difference between the real time and the fake
    time entered by the user.
    If he just wants to display the current time, but not
    stat doing that untill some point in the future then
    yeah TimerTask is the simplest way to do it, but I
    don' think that's what he's trying to do.thanks for the replies guys. I appreciate it. Sorry if I was a bit unclear. Norweed was right in saying that I want to set the time and start updating immediately, not wait until the time reaches my entered time and then count. Sorry for the confusion.
    I was thinking that a offset would work, thanks for recommending that. I'm wondering if the time will get off after a while, I need this program to run probably arounnd 2-3 hours. Hopefully it won't drift very much over that time.
    Thanks all for the recommendations and I will check all that out.

  • REGEXP_REPLACE issue/bug - replaces only every second occurence

    Hi All
    I am using
    Oracle Database 10g Express Edition Release 10.2.0.1.0 - Product
    PL/SQL Release 10.2.0.1.0 - Production
    CORE     10.2.0.1.0     Production
    TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
    NLSRTL Version 10.2.0.1.0 - Production
    I need a regular expression to rename a variable in a mathematical formula, when testing my code I discovered that if the variable was repeated right after itself only one occurence was replaced. I need to only replace whole words as some variables can be a subset of a longer variable name, and variable names can have underscores in them.
    Below is an example of what happens.
    Can someone please tell me why this happens? Example code is:
    SELECT REGEXP_REPLACE('ab+ab+ab+ab+ab+ab+ab+abc+abc+ab', '(\W|^)ab(\W|$)' , '\1xy\2',1,0,'i') from DUAL;
    The regular expression matches all 'ab' with a non alpha character or beginning line before and a non alpha or end of line at the end.
    I then replace the 'ab' with 'xy' and keep the original non alpha on either side. What happens is that only every second occurence is replaced.
    If I place a space after every '+' then it replaces all occurences. (Obviously this is a user field in a table of mathematical formulas so I cannot expect the user to worry about this problem.)
    I believe it may be due to the fact the pointer in finding all occurence is moved incorrectly and it does not find the match immediately after.
    Can someone please explain this? Or am I missing something in my example.
    Thanks
    Dudley

    Is this what you require? I changed your query a bit.
    SQL> SELECT REGEXP_REPLACE ('ab+ab+ab+ab+ab+ab+ab+abc+abc+ab',
      2                         '(\W|^)ab(\W|$)',
      3                         '\1xy\2',
      4                         1,
      5                         0,
      6                         'i'
      7                        )
      8    FROM DUAL;
    REGEXP_REPLACE('AB+AB+AB+AB+AB+
    xy+ab+xy+ab+xy+ab+xy+abc+abc+xy
    1 row selected.
    SQL>
    SQL> SELECT REGEXP_REPLACE ('ab+ab+ab+ab+ab+ab+ab+abc+abc+ab',
      2                         '(ab)(\W|$)',
      3                         'xy\2',
      4                         1,
      5                         0,
      6                         'i'
      7                        )
      8    FROM DUAL;
    REGEXP_REPLACE('AB+AB+AB+AB+AB+
    xy+xy+xy+xy+xy+xy+xy+abc+abc+xy
    1 row selected.
    SQL> -- checking data with white spaces
    SQL> SELECT REGEXP_REPLACE ('ab + ab + ab + ab + ab + ab + ab + abc + abc + ab',
      2                         '(ab)(\W|$)',
      3                         'xy\2',
      4                         1,
      5                         0,
      6                         'i'
      7                        )
      8    FROM DUAL;
    REGEXP_REPLACE('AB+AB+AB+AB+AB+AB+AB+ABC+ABC+AB',
    xy + xy + xy + xy + xy + xy + xy + abc + abc + xy
    1 row selected.
    SQL> Regards,
    Jo

  • I am looking to find out if it is possible in Adobe Muse to have a section on a webpage that will update every 30 seconds or 30 minutes with a snapshot URL From a live webcam stream

    I am looking to find out if it is possible in Adobe Muse to have a section on a webpage that will update every 30 seconds or 30 minutes with a snapshot URL From a live webcam stream

    You can do this via insert HTML , you would need to use the live feed code in Muse page.
    Thanks,
    Sanjit

  • Since the Kit Kat update every time I am on a call I here a Beep on my phone every 30 seconds very annoying anyone else having that problem.

    Since the Kit Kat update every time I am on a call I here a Beep on my phone every 30 seconds very annoying anyone else having that problem.

        Thanks for the information DiGravio1! Let's look further into this. Please navigate to Settings>My Device>Call>Call Alerts>Under Call Status Tones Un-check alerts>Power off/on, test and share. Thank you.
    TominqueBo_VZW
    Follow us on Twitter @VZWSupport

  • System Clock Apparently Gaining One Second Every 45 Minutes

    MacBook Pro Core Duo, OS X 10.6.8
    According to the console log, the clock on my MacBook Pro seems to be gaining about a second every 45 minutes:
    7/10/11 1:11:13 AM    ntpd[7969]    time reset -0.808832 s
    7/10/11 1:28:31 AM    ntpd[7969]    time reset -0.774710 s
    7/10/11 1:54:24 AM    ntpd[7969]    time reset -0.734610 s
    7/10/11 2:11:39 AM    ntpd[7969]    time reset -0.636148 s
    7/10/11 2:54:30 AM    ntpd[7969]    time reset -1.447670 s
    7/10/11 3:29:31 AM    ntpd[7969]    time reset -0.869028 s
    7/10/11 3:47:35 AM    ntpd[7969]    time reset -0.439925 s
    7/10/11 4:13:30 AM    ntpd[7969]    time reset -1.055200 s
    7/10/11 4:48:20 AM    ntpd[7969]    time reset -1.043842 s
    7/10/11 5:23:06 AM    ntpd[7969]    time reset -1.050135 s
    7/10/11 5:58:13 AM    ntpd[7969]    time reset -1.054077 s
    7/10/11 6:33:21 AM    ntpd[7969]    time reset -1.056519 s
    7/10/11 7:08:20 AM    ntpd[7969]    time reset -1.035000 s
    7/10/11 7:43:24 AM    ntpd[7969]    time reset -1.061526 s
    I am using the date and time auto-update in the system preferences with the Apple Time Server.  I have the identical setup on two other systems (another MacBook Pro and an intel Mac Mini) and they don't exhibit the problem.
    When I disable the auto-update and reset the clock manually using ntpdate and the NIST server, the clock only gains a second in three days with respect to the NIST server (using ntpdate to check).  When I turn the auto-update back on the same symptom recurrs.
    Any ideas as to what might be causing this and how to fix it?

    Barney-15E wrote:
    It's not your clock. There is more to network time protocol than just setting the system clock that is in the computer. Take a look here: http://www.meinberg.de/english/info/ntp.htm
    Well, that's very interesting.  Thanks.  But it doesn't explain why only one out of three identically configured computers manifests the symptom.

  • TS1702 I have 8 apps that when I tried to update, are now stuck on "installing" in the box that says update. every time  I try to update another app, it gets frozen like this as well. I was able to quit all apps, but these, as they do not respond. Any ide

    I have 8 apps that, when I tried to update, are now stuck on "installing" in the box that says update. every time  I try to update another app, it gets frozen like this as well. I was able to quit all apps, but these, as they do not respond. Any ideas?

    Reset your iPad by holding the home and on/off buttons together for 6 or 7 seconds till the Apple logo appears.  Then update your apps one at a time.  Do not start the next app until the current one downloads and finishes installing.

Maybe you are looking for

  • Can I use my newly created iCloud email address as my Apple ID?

    Hey, I know there have been loads of post regarding iCloud and account issues. I was wondering if it was possible to use my newly created iCLoud email address as my Apple ID and its primary address. I had a hotmail account as my Apple ID before signi

  • Customize Insufficient privileges (WWV-12600) Page

    Can i customize the Insufficient privileges (WWV-12600) page ?? If that's posssible, how can that be done ?? Thanks

  • ODS architecture

    Hi, I want some help on ODS architecture. Please tell me why change log and active table is used for. And why only active table is used for reporting and not change log table. Does the change log table in ODS and change log delta type in LO extractio

  • TS1394 When will 10.7.4 come out?

    I can't play one of my games till it does.It's black ops for mac and i can't play till the next update.

  • Can't preview anything in browsers

    Would anyone have a clue why I can't preview nearly anything in a browser? The only pages that work are the ones with simple html and graphics. Yesterday my javascript pop up menus I'm using in a template that I created in dreamweaver went crazy (and