Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have to update the text in a separate thread every second. </p> <p>Ideally you should update swing component only in the EDT ( event dispatcher thread ) but, after I tried it on my machine, using <a href="http://java.sun.com/javase/6/docs/api/java/util/Timer.html#scheduleAtFixedRate%28java.util.TimerTask,%20long,%20long%29" rel="noreferrer">Timer.scheduleAtFixRate</a> gave me better results:</p> <p><a href="http://img175.imageshack.us/img175/8876/capturadepantalla201006o.png" rel="noreferrer">java.util.Timer http://img175.imageshack.us/img175/8876/capturadepantalla201006o.png</a></p> <p>The javax.swing.Timer version was always about half second behind:</p> <p><a href="http://img241.imageshack.us/img241/2599/capturadepantalla201006.png" rel="noreferrer">javax.swing.Timer http://img241.imageshack.us/img241/2599/capturadepantalla201006.png</a></p> <p>I really don't know why.</p> <p>Here's the full source:</p> <pre><code>package clock; import javax.swing.*; import java.util.*; import java.text.SimpleDateFormat; class Clock { private final JLabel time = new JLabel(); private final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm"); private int currentSecond; private Calendar calendar; public static void main( String [] args ) { JFrame frame = new JFrame(); Clock clock = new Clock(); frame.add( clock.time ); frame.pack(); frame.setVisible( true ); clock.start(); } private void reset(){ calendar = Calendar.getInstance(); currentSecond = calendar.get(Calendar.SECOND); } public void start(){ reset(); Timer timer = new Timer(); timer.scheduleAtFixedRate( new TimerTask(){ public void run(){ if( currentSecond == 60 ) { reset(); } time.setText( String.format("%s:%02d", sdf.format(calendar.getTime()), currentSecond )); currentSecond++; } }, 0, 1000 ); } } </code></pre> <p>Here's the modified source using javax.swing.Timer</p> <pre><code> public void start(){ reset(); Timer timer = new Timer(1000, new ActionListener(){ public void actionPerformed( ActionEvent e ) { if( currentSecond == 60 ) { reset(); } time.setText( String.format("%s:%02d", sdf.format(calendar.getTime()), currentSecond )); currentSecond++; } }); timer.start(); } </code></pre> <p><sub><em>Probably I should change the way the string with the date is calculated, but I don't think that's the problem here</em></sub></p> <p>I have read, that, since Java 5 the recommended is: <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html" rel="noreferrer">ScheduledExecutorService</a> I leave you the task to implement it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload