1

I want to make a java.util.Date display in a JFrame with always being refreshed to view the new date.

package pro;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.Timer;

public class Date {

public static Timer t;


public static void main(String [] args){

    time();

}
public static Timer time(){

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

@SuppressWarnings("deprecation")

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, new java.util.Date().toGMTString());

        }

    });     

    t.setRepeats(true);

    t.start();

    return t;
}
}

I guess I made something wrong in the timer method

I also tried to edit it

package pro;

import java.awt.FlowLayout;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class date {

private static JLabel l;
private static Date d = new Date();
private static JFrame f;

@SuppressWarnings("deprecation")

public static void main(String [] args){

    f = new JFrame("Date program");

    f.setVisible(true);
    f.pack();
    f.revalidate();
    f.setLayout(new FlowLayout(FlowLayout.LEFT));
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    l = new JLabel(d.toGMTString());


    f.add(l);
    while(true){

    l.revalidate();

    }
}

}

any replies will be appreciated.

  • See [How to create a digital clock](http://stackoverflow.com/a/21619240/2587435) – Paul Samsotha Feb 18 '14 at 17:03
  • I am sorry to say that, but your "example code" made me laugh. If I may rephrase your question: *I want to build a complex enterprise project and I already have a Java Hello World program. Please provide the rest!* xD I would usually add a few helpful links to tutorials too, but there are already some helpful answers. – SebastianH Feb 18 '14 at 17:06
  • Add a label to the `JOptionPane` that refreshes the time text. See the link above. – Paul Samsotha Feb 18 '14 at 17:23

2 Answers2

1

"when I just write it like this it gets the date in the second I run it without being changed."

Just add a JLabel to the JOPtionPane and only update the JLabel in the Timer. Don't put the JOptionPane in the Timer code. Here's is an example

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Clock {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                final Date date = new Date();
                final JLabel timeLabel = new JLabel(date.toString());
                Timer timer = new Timer(1000, new ActionListener(){
                    public void actionPerformed(ActionEvent e) {
                        date.setTime(System.currentTimeMillis());
                        timeLabel.setText(date.toString());
                    }
                });
                timer.start();
                JOptionPane.showMessageDialog(null, timeLabel);
            }
        });
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

use timer to do this

t = new javax.swing.Timer(1000, new ActionListener() {
          public void actionPerformed(ActionEvent e) {


JOptionPane.showMessageDialog(null , new SimpleDateFormat("HH:mm:ss", Locale.FRANCE).format(new Date()));
}
       });
        t.start();

EDITED

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.Timer;

public class pro6 extends JFrame{
public  static Timer t;
public JLabel jlabel4;
public pro6() {
        System.out.println("heloo");
this.setLayout(null);
       jlabel4 = new JLabel();
       jlabel4.setBounds(0, 0, 100, 30);
       this.add(jlabel4);
       this.pack();

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

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        jlabel4.setText(new SimpleDateFormat("HH:mm:ss", Locale.FRANCE).format(new Date()));

        }

    });

 t.start();

 setSize(new Dimension(200, 60));

 setDefaultCloseOperation(DISPOSE_ON_CLOSE);

}
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       new pro6().setVisible(true);
    }}
  • thanks so much that was a clever thing adding a timer – user3314952 Feb 18 '14 at 17:03
  • hey I am trying to do it but that doesn't work I'll post the code in a new comment.. – user3314952 Feb 18 '14 at 17:16
  • is you use a label instead of JOptionPane the code will work very fine – Karim Massi Feb 18 '14 at 17:18
  • package pro; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JOptionPane; import javax.swing.Timer; public class Date { public static Timer t; public static void main(String [] args){ time(); } public static Timer time(){ t = new Timer(1000, new ActionListener(){ @SuppressWarnings("deprecation") @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub JOptionPane.showMessageDialog(null, new java.util.Date().toGMTString());}}); t.setRepeats(true); t.start(); return t; } } – user3314952 Feb 18 '14 at 17:18
  • @user3314952 no, just edit your post. There's a link under your post you can click. Don't delete your original post either. Just and it below what you currently have and mark it as an edit – Paul Samsotha Feb 18 '14 at 17:18
  • omg I am so sorry didn't know it'll be like this I ll post it in an answer – user3314952 Feb 18 '14 at 17:18
  • @user3314952 not in an answer either. _EDIT_ your post. – Paul Samsotha Feb 18 '14 at 17:19