Set Alarm watch using Swing

Here you can set the alarm using swing, see the code here
------------------------------------------------------------
package example.alarm;


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

import javax.swing.*;

class AlarmDemo extends Thread{
private JLabel timeLabel;
public AlarmDemo(JLabel label){
   this.timeLabel=label;}
   
    public void run(){
      while(true){
         Date d=new Date();
         int h=d.getHours();
         String time="PM";
         if (h>23){h-=24; time="AM";}
         timeLabel.setText(""+h+":"+d.getMinutes()+":"+d.getSeconds()+"  "+time);
         try{Thread.sleep(1000);}
 catch(Exception e){}
      }
         }

}


class Timer implements Runnable{
private JLabel timeLabel;
private JLabel alarmTime;
public Timer(JLabel label){
   this.timeLabel=label;}
   
    public void run(){
      while(true){
         Date d=new Date();
         timeLabel.setText(d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());
         
         System.out.println("current time:"+timeLabel.getText()+" And alarmed time:"+alarmTime.getText());
         if(timeLabel.getText().equals(alarmTime.getText())){
         JOptionPane.showMessageDialog(null,"Please walk up: The time is :"+timeLabel.getText());
         }
         try{Thread.sleep(1000);}
 catch(Exception e){}
      }
     }

public void setAlarmTime(JLabel alarmTime) {
this.alarmTime = alarmTime;
}

public JLabel getAlarmTime() {
return alarmTime;
}

}

class CounterSeconds implements ActionListener {
    private int counter;
    private JFrame timerFrame=new JFrame();
    private JLabel label1=new JLabel("              ");
    private JLabel label2=new JLabel("              ");
    private JLabel label4=new JLabel("              ");
    private JLabel label5=new JLabel("ALARM");   
    private JLabel hour=new JLabel("HH:");
    private JLabel minute=new JLabel("MM:");
    private JLabel second=new JLabel("SS:");
    private JLabel alarmTimeLabel=new JLabel("            ");
    private JTextField alarmDateNew=new JTextField(8);
    private JTextField hh=new JTextField(5);
    private JTextField mm=new JTextField(5);
    private JTextField ss=new JTextField(5);
    private Font fnt=new Font("TimesNewRoman",Font.BOLD,16);
    private JButton b1=new JButton("STOP");
    private JButton b2=new JButton("RESTART");
    private JButton alarmButton=new JButton("set alarm");
    final JComponent[] inputs;
    public static final String DATE_FORMAT_NOW = "dd-MM-yyyy HH:mm:ss";
    public static String dt;
    public static String currentDate;
    public static String currentTime;
    JTextField alarmDate ;
JTextField alarmTime ;
    static Thread t5;
    static Thread t6;
    AlarmDemo t1;
    
    CounterSeconds(){
           Date date= new Date();
                currentDate=date.getDay()+"-"+date.getMonth()+"-"+date.getYear();
                currentTime=date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
                alarmDate = new JTextField(currentDate);
             alarmTime = new JTextField(currentTime);
     
              inputs = new JComponent[] {
                     new JLabel("Date(DD-MM-YYYY)::"),
                     alarmDate,
                     new JLabel("Time(HH:MM:SS)::"),
                     alarmTime,
     
             };
           counter=0;
                Container c=timerFrame.getContentPane();
          label1.setFont(fnt); label2.setFont(fnt);
          label4.setFont(fnt);
          alarmTimeLabel.setFont(fnt);
          label1.setText("Counter:  "+0);
          timerFrame.setLayout(new FlowLayout());
timerFrame.setSize(160,180);
c.add(label1);   c.add(label2);
c.add(label4);
c.add(b1);c.add(b2);
c.add(alarmButton);
c.add(label5);
c.add(alarmDateNew);
c.add(hour);c.add(hh);
c.add(minute);c.add(mm);
c.add(second);c.add(ss);
c.add(alarmTimeLabel);
b1.addActionListener(this);b2.addActionListener(this);
alarmButton.addActionListener(this);
timerFrame.setVisible(true); }
    
    public void actionPerformed(ActionEvent e){
   Object target=e.getSource();
   if(target==b1){t5.suspend();
                  t1.suspend();}
   if(target==b2){t5.resume();}
   if(target==alarmButton){
  JOptionPane.showMessageDialog(null, inputs, "Set Alarm", JOptionPane.PLAIN_MESSAGE);
  alarmDateNew.setText(alarmDate.getText());
  alarmTimeLabel.setText(alarmTime.getText());
String [] time=alarmTime.getText().split(":");
hh.setText(time[0]);
mm.setText(time[1]);
ss.setText(time[2]);
try {
// new TimeDemo().run();
} catch (Exception e1) {
// TODO Auto-generated catch block
System.out.println("Here is exception:"+e1.getMessage());
e1.printStackTrace();
}
 }
 
    }
    public static String DateTime(){
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
        return sdf.format(cal.getTime());
    }
    
     public JLabel readLabel2(){return label2;}
     public JLabel readLabel3(){return label4;}
     public JLabel readLabel4(){return alarmTimeLabel;}
     
      public void display(){
while(counter <100)
       { counter++;
label1.setFont(fnt);
label1.setText("Counter:  "+counter);
// System.out.println("Current time:"+this.readLabel2());
          try{Thread.sleep(1000);}
catch(Exception e){}
 }
         }
      
      public void start_thread(){
          Timer t=new Timer (this.readLabel2());
          t1=new AlarmDemo (this.readLabel3());         
          t.setAlarmTime(this.readLabel4());          
          t5=new Thread(t);
          t1.start();
          t5.start();          
          
      }

      public static void main(String[] args) {
                     CounterSeconds c=new CounterSeconds();
                     c.start_thread();

                c.display();
      }
 }

Comments

Popular posts from this blog

Read Images from a xlsx file using Apache POI

Struts 2 : Warning :No configuration found for the specified action: 'Login.action' in namespace: '/'

How to create mail message in FRC822 format