Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>How about using a timer, such as <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html" rel="noreferrer"><code>javax.swing.Timer</code></a>? (Do not make mistake in the import, there are more Timer classes.)</p> <pre><code>public static void main(String... args) throws InterruptedException { final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); int interval = 1000; // 1000 ms new Timer(interval, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Calendar now = Calendar.getInstance(); System.out.println(dateFormat.format(now.getTime())); } }).start(); Thread.currentThread().join(); } </code></pre> <p>This will simply execute the body of the ActionListener every second, printing the current time.</p> <p>The <code>Thread.join</code> call on the last line is not universally necessary, it's just needed for this example piece of code to run until the process is manually stopped. Otherwise, it would immediately stop.</p> <p>In a real application, in case it's a Swing app, then the timer should handle threading by itself, so you won't have to worry about it.</p> <hr> <h2>Edit</h2> <p>Integrating the above sample into your application is fairly simple, just add it into the <code>initGUI</code> method and instead of printing the current time to System.out set change the text of the given label:</p> <pre><code>public void initGUI() { setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setPreferredSize(new Dimension(800, 600)); setLayout(null); Calendar now = Calendar.getInstance(); tijd = new JLabel(dateFormat.format(now.getTime())); tijd.setBounds(100, 100, 125, 125); window.add(tijd); new Timer(1000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Calendar now = Calendar.getInstance(); tijd.setText(dateFormat.format(now.getTime())); } }).start(); pack(); } </code></pre>
    singulars
    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. This table or related slice is empty.
    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