I'm currently writing a program that acts as an application for a store manager, sorting, searching, reading in, etc. products. This program has two classes: one to handle the GUI and another to handle the actual calculations. I am completely done coding this, save for one requirement: I need to have the time displayed, and updated every minute. I already have the time displayed; the only problem I have is with having a new time displayed every minute.

I have tried using Thread.sleep(), but this causes my compiler to try and load it, but never succeed in actually bringing up the JFrame.
Right now the code I have for updating the time is as follows:

public void run()
    {
        while(true)
        {
            try
            {
                Thread.sleep(1000);
            }
            catch(InterruptedException e){}
            time.setText(store.time());
        }

and the code I have for getting the time and formatting it for display is as follows:

public String time()
    {
    GregorianCalendar calendar=new GregorianCalendar();
    Date date=calendar.getTime();
    DateFormat type=DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
    String time=type.format(date);
    return time;
    }

I would be very grateful for any help that anyone would be willing to offer. I have searched for how to do this throughout the Internet and have not been able to find it.

Read the API for one of the Timer Classes. The one you want is probably javax.swing.Timer.

Thanks a ton! It works perfectly now. :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.