0

I have written this function for date time. Now I want to use in every JFrame. I want to declare a function(method) for date time in my main (First.java) and I want to call that method in another few JFrame(like JFrame1.java, Jframe2.java ....)

import java.util.*;

public class TestDate {

    int day, month, year;

    public TestDate( ) {
    }

    void TestData(){
        int day, month, year;
        int second, minute, hour;
        GregorianCalendar date = new GregorianCalendar();

        day = date.get(Calendar.DAY_OF_MONTH);
        month = date.get(Calendar.MONTH);
        year = date.get(Calendar.YEAR);

        second = date.get(Calendar.SECOND);
        minute = date.get(Calendar.MINUTE);
        hour = date.get(Calendar.HOUR);

        System.out.println("Current date is  "+day+"/"+(month+1)+"/"+year);
        System.out.println("Current time is  "+hour+" : "+minute+" : "+second);
    }
}

I have two JFrame ( Jframe1.java and JFrame2.java) I want to show date time in these two JFrame using Jlabel.

lefloh
  • 10,653
  • 3
  • 28
  • 50
Indranil Banerjee
  • 133
  • 1
  • 1
  • 11
  • 1
    *"Now I want to use in every JFrame."* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) (pretty bad practice) – dic19 Jul 16 '14 at 15:11

1 Answers1

2

The class you created has nothing to do with a GUI and can't be used in a JFrame. So you need to do something like:

  1. Create a "DateTimePanel" that contains two JLabels, one to display the date and the other to display the time. Instead of using System.out.println(...) you would use the setText(...) method of the label to set a string to display.

  2. Then you can create an instance of the DateTimePanel and add it to your GUI as required.

Of course the current time will keep changing so you may want to update the time. In this case you would use a Swing Timer to continually update the label. Here is a simple example of using a panel and a Timer:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;

public class TimerTime extends JPanel implements ActionListener
{
    private JLabel timeLabel;

    public TimerTime()
    {
        timeLabel = new JLabel( new Date().toString() );
        add( timeLabel );

        Timer timer = new Timer(1000, this);
        timer.setInitialDelay(1);
        timer.start();
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        //System.out.println(e.getSource());
        timeLabel.setText( new Date().toString() );
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("TimerTime");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new TimerTime() );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

I suggest you also start by reading the Swing Tutorial for the basics of creating a GUI.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Yes. But try to understand my question please . I want to declear a function(method) for date time in my main (First.java) and I want to call that method in another few JFrame(like JFrame1.java, Jframe2.java ....) – Indranil Banerjee Jul 16 '14 at 15:31