Solved

Clock in dialog

Posted on 2011-09-16
15
151 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
Comment Utility
Please, elaborate what it is that you want to achieve.
0
 

Author Comment

by:Mickeys
Comment Utility
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
Comment Utility
What kind of GUI do you want to have?


0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
Comment Utility
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
Comment Utility
Why would not you ruin the code I sent you ?
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
Or post the whole full code of the whole class you are running so that I could try to compile and run it.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Mickeys
Comment Utility
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
Comment Utility
Why do you need these threads and runnable?
0
 

Author Comment

by:Mickeys
Comment Utility
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
Comment Utility
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
Comment Utility
You don't need threads
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
Comment Utility
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
Comment Utility
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoreUp challenge 14 47
JAVA part two 5 39
Where to store the queries for modification of table 4 25
Java Jpanels and Jframe 8 19
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now