Solved

Clock in dialog

Posted on 2011-09-16
15
157 Views
Last Modified: 2012-05-12
I want this to be updated in a dialog. How do I do that?
package Thread;

import java.awt.*;
import java.util.*;

public class Clock implements Runnable
{
    Font f = new Font("TimesRoman",Font.BOLD,24);
    Date d; Thread runner;
    public static void main(String[] args) {
        Clock c = new Clock();
        c.start();
    }

    public void start(){
        while(runner==null){runner=new Thread(this);runner.start();}}

    public void run()
    {
       while (true)
       {
           d = new Date();
           System.out.println(d.toString());
           try {Thread.sleep(1000);}
           catch(InterruptedException e){};
       }
    }
}

Open in new window

0
Comment
Question by:Mickeys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 4
15 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36550061
Please, elaborate what it is that you want to achieve.
0
 

Author Comment

by:Mickeys
ID: 36558614
Right now I just have the code as a Clock-class. But I can't it to show in my GUI. How do I do that?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36558638
What kind of GUI do you want to have?


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 36558680
Is this something you want to have:

import javax.swing.*;

import java.awt.*;
import java.text.SimpleDateFormat;

public class Clock {

    public static void main(String[] args) {
        JFrame f = new JFrame();
        JLabel l = new JLabel("Time:      ");
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
        java.util.Date d;
        Container c = f.getContentPane();
        f.add(l);

        f.setSize(200,100);
        f.setVisible(true);
        f.setLocation(300,300);
                    int count =0;
                 try{
              while(count < 60) {
        Thread.currentThread().sleep(1000);
               d = new java.util.Date();
                  l.setText("Time: " + sdf.format(d));
                  count++;
              }

                 }catch(Exception ex){
                     ex.printStackTrace();
                 }
                    System.exit(0);
    }


}

Open in new window


0
 

Author Comment

by:Mickeys
ID: 36581702
I did like this, men I run the code is hangs.
private void button_KlockaActionPerformed(java.awt.event.ActionEvent evt) {                                              
        Clock clock = new Clock();
        clock.startClock(); //Anropa klockan.
    }            




new class clock

 public void startClock() {
        JFrame f = new JFrame();
        JLabel l = new JLabel("Time:      ");
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
        java.util.Date d;
        Container c = f.getContentPane();
        f.add(l);

        f.setSize(200, 100);
        f.setVisible(true);
        f.setLocation(300, 300);
        int count = 0;
        try {
            while (count < 60) {
                Thread.currentThread().sleep(1000);
                d = new java.util.Date();
                l.setText("Time: " + sdf.format(d));
                count++;
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36581716
Why would not you ruin the code I sent you ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36581738
Or post the whole full code of the whole class you are running so that I could try to compile and run it.
0
 

Author Comment

by:Mickeys
ID: 36582205
You have the full code, I just replaced the main-metod in my first question with your code.

But here is the code anyway:
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Clock implements Runnable
{
    Font f = new Font("TimesRoman",Font.BOLD,24);
    Date d;
    Thread runner;
    
    public void startClock() {
        JFrame f = new JFrame();
        JLabel l = new JLabel("Time:      ");
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
        java.util.Date d;
        Container c = f.getContentPane();
        f.add(l);

        f.setSize(200, 100);
        f.setVisible(true);
        f.setLocation(300, 300);
        int count = 0;
        try {
            while (count < 60) {
                Thread.currentThread().sleep(1000);
                d = new java.util.Date();
                l.setText("Time: " + sdf.format(d));
                count++;
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void start(){
        while(runner == null){
            runner = new Thread(this);
            runner.start();}
    }

    public void run()
    {
       while (true)
       {
           d = new Date();
           System.out.println(d.toString());
           try {Thread.sleep(1000);}
           catch(InterruptedException e){};
       }
    }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36582215
Why do you need these threads and runnable?
0
 

Author Comment

by:Mickeys
ID: 36582224
Don't forget the button:
private void button_KlockaActionPerformed(java.awt.event.ActionEvent evt) {                                              
        Clock clock = new Clock();
        clock.startClock(); //Anropa klockan.
    }

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36582242
So you don't want it to start immediately - you want to have the button to start it?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36582249
You don't need threads
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36582839
No, you are right - if you really need the button to start - then you need to deal with threads.
I'll try to make the working code later
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36585039
This has buttons - starts, stops.
Check it out.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;

public class MyNewClock extends JFrame implements ActionListener {
    JLabel label;
    JButton button;
     JButton button2;
       JButton button3;
     PrimeTask pt;

    public MyNewClock(){
        super("My new clock");
        label = new JLabel();
        label.setText("Time: 09-22-2011 12:00:00");
        button = new JButton("start");
        button.addActionListener(this);
          button2 = new JButton("stop");
        button2.addActionListener(this);
                  button3 = new JButton("close");
        button3.addActionListener(this);
        Container c = this.getContentPane();
        c.setLayout(new FlowLayout());
        c.add(button);
        c.add(button2);
          c.add(button3);
        c.add(label);
        this.setSize(300,100);
        this.setLocation(300,300);
        this.setVisible(true);


    }



  public void actionPerformed(ActionEvent ae){
          if(ae.getSource().equals(button)){
        pt = new PrimeTask(label);
      pt.execute();
      }
               if(ae.getSource().equals(button2)){

      pt.stop();
      }
                     if(ae.getSource().equals(button3)){

      System.exit(0);
      }

  }

    public static void main(String[] args) {
        new MyNewClock();
    }

}
class PrimeTask extends SwingWorker{
         JLabel lbl;
          SimpleDateFormat sdf;
    boolean toRun = true;

    PrimeTask(JLabel lbl){
        this.lbl = lbl;
           sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
        
    }

    
    public String doInBackground(){
        String s = null;
              toRun = true;
              //int count =0;
                 try{

              while( toRun) {
        Thread.currentThread().sleep(1000);
              java.util.Date  d = new java.util.Date();
                  lbl.setText("Time: " + sdf.format(d));
                //  count++;
              }

                 }catch(Exception ex){
                     ex.printStackTrace();
                 } 


        return s;
    }

    public void stop(){
        toRun = false;

    }

}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36585048
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question