Peach
Peach

Reputation: 37

Refresh Text / JLabel value

I am a beginner looking for some advice from the more experienced programmers.

I have two solutions to display the current date/time in a GUI and refresh it like a live clock.

Solution 1

    **DateFormat** dateFormat = new SimpleDateFormat("dd MMMM yyyy - HH:mm:ss z");
    JLabel dateTimeLabel = new JLabel();
    add(dateTimeLabel, BorderLayout.CENTER);

    new Thread() {
        public void run() {
            while (true) {
                Date curEngDate = new Date();
                dateTimeLabel.setText(dateFormat.format(curEngDate));
            }
        }
    }.start();

Solution 2

    Timer timer = new Timer(3000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Date date = new Date();
            label.setText(dateFormat.format(date));
        }
    });
    timer.start();

Questions:

To my understanding, a Timer will run continuously, similarly to a thread. and using event listener may be better than using a while loop for performance.

But what are your experiences? how would you do this?

I am looking for general advice on this type of feature, not necessarily for a clock or date/time

Thanks in advance for all your advice

Upvotes: 0

Views: 225

Answers (3)

user3437460
user3437460

Reputation: 17454

Which is the more suitable one to your experience?

It all depends on what you want to do. Generally using the timer to animate or to run things with a specific delay is good enough. However if you do this, everything runs in the EDT (Event Dispatch Thread). But if you doing a complicated program with plenty of calculation and image rendering, it may slow down your rendering (and your JFrame may turned white) since everything runs in the EDT.

Alternatively, you can implementing your own program-loop (or game loop) - which is your solution 1. But writing a good loop isn't that straight forward though. It will run as fast as it can wasting CPU cycles. You will then have to call Thread.sleep() to yield the thread (pause your thread, so other thread can run). You will also have to control the frame rate your self. No matter what, just remember not to run Thread.sleep() in the EDT.

The easiest approach will still be using a swing timer from Java unless you plan to do something sophisticated.

Will either be better in terms of memory / power usage?

It doesn't really matters.

What impact will these have on performance?

Performance wise, running a separate thread seemed to be better in my opinion. But it doesn't really make a difference in the given 2 solution unless you are doing a relatively large project.

Or is there any better solution?

Generally these are the 2 solutions if you want to code with in Java by yourself. If not, you can use some external packages.

Upvotes: 1

user3969578
user3969578

Reputation:

The Timer is by far the better option of the two, however I would recommend you use a SwingWorker instead.

In terms of performance, If you look at the code you wrote, you'll see that the timer only executes every 3 seconds (3000 milliseconds), while the Thread will continuously run with no delay due to the while loop, making the Timer much better, unless you really need the label to be updated constantly.

However, because your task is long running, you should probably use a SwingWorker, as it is useful for long running tasks related to the Swing GUI. Long running tasks tend to lock up the GUI, which is obviously not good. A SwingWorker is able to keep the GUI responsive while performing your task. Here is a tutorial provided by Oracle.

Upvotes: 1

toskv
toskv

Reputation: 31600

The difference is in the threads your code is running. Solution one creates it's own thread and updates the label from that.

Solution 2 uses the awt Timer class. It spawns it's own thread but the actionPerformed method is called on the AWT-EventQueue thread.

Since all changes to the components should only happen on the AWT-EventQueue that's the correct solution to pick.

Check out the swing tutorial on threading for more information. https://docs.oracle.com/javase/tutorial/uiswing/concurrency/

Upvotes: 1

Related Questions