-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
  • 166,747
  • 40
  • 210
  • 420
KIW
  • 1
  • 4
  • 1
    You want to have a single instance of date right?did you try anything? – Thusitha Thilina Dayaratne Oct 18 '13 at 03:08
  • what did you try till now? – Crickcoder Oct 18 '13 at 03:09
  • Have you considered putting that class on your `CLASS_PATH` and then importing it where you want to use it? – Litmus Oct 18 '13 at 03:27
  • 1
    By *"..or whatever Methodist"* DYM *"..or whatever method it is"*? ..Because the other is simply nonsense (even though 'Methodist' is a valid word). – Andrew Thompson Oct 18 '13 at 03:49
  • @AndrewThompson Sorry.i want to display time on all frames my swing application using created one java class and access it.so how can do it better way? – KIW Oct 18 '13 at 03:57
  • Your question remain woefully lacking in necessary detail. Please put as much effort into asking your question as you'd like someone to expend answering it. – Hovercraft Full Of Eels Oct 18 '13 at 04:01
  • 1
    *"Sorry"* An [edit to the question](http://stackoverflow.com/posts/19440938/edit) to make it more clear, is better than apologies. – Andrew Thompson Oct 18 '13 at 04:13
  • how to show time 5 jframes when one time class is written ? – KIW Oct 18 '13 at 04:15
  • `"how to show time 5 jframes when one time class is written ?"` -- please clarify as this doesn't make sense to me. Write out what you mean by this. And while you're at it, clarify your entire question please. – Hovercraft Full Of Eels Oct 18 '13 at 04:42
  • suppose..i create jframe and i want to show time.then i create simedateformat object and show it. (simpledateformat object * 5 (jframes) = 5 sdf objects) but if i create one time class for it can i access it for 5 jframes ? – KIW Oct 18 '13 at 05:26

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
  • 280,125
  • 25
  • 247
  • 360
  • 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