-2

I want to create one date and time class for show date and time on all frames (My Swing Application). It means though number of frames are create i want display date and time using created java class.so what is the best way to do it?

I tried this code to do it.

Testing Code......

final SimpleDateFormat df = new SimpleDateFormat("hh:mm:ss a");
Timer timer = new javax.swing.Timer(1000, new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        Date date = new Date();
        String time = df.format(date);
    }
});

timer.start ();

I want to access this time variable or whatever Methodist to show time on all Frames using this timer.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

3

i want to create one date and time class for show date and time on all frames(My Swing Application).

What do you mean by "date and time class"? Java already has a Date variable. Do you mean a common object of this type? Also your having multiple JFrames concerns me as an application usually only has one JFrame, and this suggests a design problem that may or may not have bearing on your problem.

it means though number of frames are create i want display date and time using created java class.so what is the best way to do it ?

If you want a single object, then pass it into your other objects when they are created, perhaps with a constructor parameter. But again, you really really do not want to create a lot of JFrames. The user will thank you if you don't.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    *"..you really really do not want to create a lot of JFrames. The user will thank you if you don't."* See also [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) for my views (AKA rave) on the topic. – Andrew Thompson Oct 18 '13 at 03:51