Solved

Clock in dialog

Posted on 2011-09-16
15
153 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
  • 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

773 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