Java - How to update a panel every second

2019-04-16 06:52发布

I'm trying to create a Java GUI which displays the current time. Currently, I can make it display the current time, but only on startup. It then simply remains on that time forever. The reason is that I cannot figure out how to make it automatically load in the new current time every second. Here is my relevant code thus far:

// Getting the current time...
long epoch = System.currentTimeMillis() / 1000;
String date = new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date(epoch * 1000));

// Creating the panel...
JLabel lblThetime = new JLabel(date);
sl_panel.putConstraint(SpringLayout.NORTH, lblThetime, 55, SpringLayout.SOUTH, lblIBeA);
sl_panel.putConstraint(SpringLayout.WEST, lblThetime, 139, SpringLayout.WEST, panel);
lblThetime.setFont(new Font("Avenir Next", Font.PLAIN, 40));

// Adding the time to the panel
panel.add(lblThetime);

// My refresher which doesn't work
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        removeAll();
        validate();
        repaint();
    }
}, 1000, 1000);

I attempted to make a refresher using information from this thread, but to no avail, the window (when run) is just blank. I then tried making a different refresher using information from this thread, and created this:

// My refresher which doesn't work
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        contentPane.remove(lblThetime);
        contentPane.add(lblThetime);
    }
}, 1000, 1000);

With contentPane having been defined as private JPanel contentPane;

Which also didn't work, however only the time itself is blank, the rest of the content in the window (One other JLabel (just some text)) remains as normal.

Without any refresher it behaves as described above, whereby it just displays the time when it started and remains on that time forever.

I'm using Eclipse with WindowBuilder. (And I'm (probably evidently) a complete noob to Java GUI stuff xD)

6条回答
仙女界的扛把子
2楼-- · 2019-04-16 07:04

First of all you are using java.util.Timer instead of javax.swing.Timer. You need use this second class when working with Swing components to ensure GUI updates are made on the Event Dispatch Thread. Also take a look to Concurrency in Swing tutorial.

As suggested in other answers, there is no need to remove/add JLabel each time you want to update its text. Just call JLabel.setText() method.

If you still want remove/add the JLabel each time, then be aware of this:

From Container.add() javadoc:

This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to display the added component.

Then you'll need to call Component.revalidate() method. Like this:

contentPane.remove(lblThetime);
contentPane.add(lblThetime);
contentPane.revalidate();
查看更多
走好不送
3楼-- · 2019-04-16 07:09

I discovered the solution!

I tried all the solutions given as answers here, and none which gave code fully worked, however all answers pointed me in the right direction. I found the solution to the problem on a website which I have forgotten the name of, but I used its suggestion and came up with this final solution which worked:

        // New timer which works!
    int delay = 1000; //milliseconds
      ActionListener taskPerformer = new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              String date = new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date(System.currentTimeMillis()));
              lblThetime.setText(date);
          }
      };
      new Timer(delay, taskPerformer).start();

Thank you to all who answered as without said answers I probably would not have been able to find this solution :D

查看更多
别忘想泡老子
4楼-- · 2019-04-16 07:09

Clue 1: really search for Timer classes in Java. Did you pick the correct one?

Clue 2: update the label text instead.

HIH

KL

查看更多
走好不送
5楼-- · 2019-04-16 07:11

Instead of removing and adding the label to the panel you should change the string of the label every second.

Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
       String date = new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date() );

       lblThetime.setText(date);
    }
}, 1000, 1000);



// Creating the panel...
JLabel lblThetime = new JLabel(date);
查看更多
贪生不怕死
6楼-- · 2019-04-16 07:15
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        label.setText(new Date().toString());
    }
}, 1000, 1000);

Isn't it simpler?

查看更多
Luminary・发光体
7楼-- · 2019-04-16 07:19

You can try to use Swing Timer for that task, for example:

private static JLabel l;

public static void main(String[] args) {

    Timer timer = new Timer(1000, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            l.setText(new Date().toString());
        }
    });
    timer.start(); 
    JFrame f = new JFrame();
    l=new JLabel(new Date().toString());
    f.getContentPane().add(l);

    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

That example update JLabel with new date every second

查看更多
登录 后发表回答