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
Solved

Clock in dialog

Posted on 2011-09-16
15
154 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 49
tomcat administrtor 12 60
How to execute a Python program and gather return output in Java 2 40
java mysql insert application 14 27
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

839 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