Solved

Clock in dialog

Posted on 2011-09-16
15
156 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
Independent Software Vendors: 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb entity bean example issue 2 26
spring maven example issues 3 42
Where to place postgres JDBC driver jar on tomcat 8 29
Java Inheritance super keyword use 8 36
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

749 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