Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Break Clock

  1. #1
    BruenorBH's Avatar
    BruenorBH is offline Member
    Join Date
    Jan 2008
    Location
    Buffalo, NY
    Posts
    14
    Rep Power
    0

    Default Break Clock

    I am still somewhat new to Java. I created this app in VB6 for a co-worker and since I am learning Java I decided to redo this app in Java SE. The issue I have is that it does not play the song that the user selects and after two alarm popups the continuous clock stops. Here is what I have so far..

    Java Code:
    import javax.swing.*;
    import java.applet.*;
    import java.net.*;
    import java.awt.*;
    import java.io.*;
    import java.awt.event.*;
    import java.util.Calendar;
    
    public class BreakClock extends JFrame 
    {
      // instance variables
      JLabel lblCurrentTime;
      JTextField txtStartTime;
      JTextField txtBreak1Time;
      JTextField txtLunchTime;
      JTextField txtBreak2Time;
      JTextField txtLeaveTime;
      String hour, min, sec, AMPM;
      AudioClip song;
      File fileName = null;  
    
      Thread b1 = new BreakTimeClock();
      Thread l = new LunchTimeClock(); 
      Thread b2 = new BreakTimeClock();
      Thread playSong = new PlaySong();
    	
      private ExitButtonHandler ebHandler;
      private AudioButtonHandler abHandler;
      
      public static void main(String[] args) 
      {
        new BreakClock();
      }
    
      /**
      * Constructor for objects of class StrategoApp
      */
      public BreakClock()
      {
        // Sets the properties of the app window
        this.setSize(250,225);
        this.setTitle("Break Clock");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	 
        JPanel panel = new JPanel();
    	
        // Sets the layout of the panel to align left
        panel.setLayout(new FlowLayout(FlowLayout.CENTER));
    	
        panel.add(new JLabel("The Current Time is:          "));
        lblCurrentTime = new JLabel("");
        panel.add(lblCurrentTime);
    	
        panel.add(new JLabel("Start Time:                   "));
        txtStartTime = new JTextField(8);
        panel.add(txtStartTime);
    
        panel.add(new JLabel("First Break Time:        "));
        txtBreak1Time = new JTextField(8);
        panel.add(txtBreak1Time);
    	
        panel.add(new JLabel("Lunch Time:                 "));
        txtLunchTime = new JTextField(8);
        panel.add(txtLunchTime);
    	
        panel.add(new JLabel("Second Break Time:  "));
        txtBreak2Time = new JTextField(8);
        panel.add(txtBreak2Time);
    	
        panel.add(new JLabel("Leave Time:                 "));
        txtLeaveTime = new JTextField(8);
        panel.add(txtLeaveTime);
    		
        JButton cmdAudio = new JButton("Audio");
        abHandler = new AudioButtonHandler();
        cmdAudio.addActionListener(abHandler);
        panel.add(cmdAudio);
    		
        JButton cmdQuit = new JButton("Quit");
        ebHandler = new ExitButtonHandler();
        cmdQuit.addActionListener(ebHandler);
        panel.add(cmdQuit);
    	
        this.add(panel);
        this.setVisible(true);
    	
        Timer t = new Timer(1000, new Tick(lblCurrentTime));
        t.start();
      }
      
      public class Tick implements ActionListener
      {
        JLabel label;
        Calendar time;
        
        Tick(JLabel lblCurrentTime)
        {
          time = Calendar.getInstance();
          label = lblCurrentTime;
        } 
        
        public void actionPerformed(ActionEvent event)
        {
          time.setTimeInMillis(System.currentTimeMillis());
          
          int Hour = time.get(Calendar.HOUR_OF_DAY);
          if (Hour > 12)
          {
            Hour = Hour - 12;
            AMPM = "PM";
          }
          
          else
          {
            AMPM = "AM";
          }
          
          String hour = Integer.toString(Hour);
          if (Hour <= 9)
          {
            // Setting the hour to a formatted string "00 - 09"
            hour = hour.trim();
            hour = "0" + hour;
          }
        	
          int Min = time.get(Calendar.MINUTE);
          String min = Integer.toString(Min);
          if (Min <= 9)
          {
            // Setting the minute to a formatted string "00 - 09"
            min = min.trim();
            min = "0" + min;
          }
        	
          int Sec = time.get(Calendar.SECOND);
          String sec = Integer.toString(Sec);
          if (Sec <= 9)
          {
           // Setting the second to a formatted string "00 - 09"
           sec = sec.trim();
           sec = "0" + sec;
          }
          
          // Updates the clock display
          lblCurrentTime.setText(hour + ":" + min + ":" + sec + " " + AMPM);
          
          // Stores the current time as string
          String Time = hour + ":" + min + ":" + sec + " " + AMPM;
          
          if (Time.equals((((txtStartTime.getText()).toString()).toUpperCase()).trim()))
          {
            playSong.stop();
            JOptionPane.showMessageDialog(null, "Time to Start Work!", "Time for Work", JOptionPane.INFORMATION_MESSAGE);
            playSong.start();
          }
          
          if (Time.equals((((txtBreak1Time.getText()).toString()).toUpperCase()).trim()))
          {
            playSong.stop();
            JOptionPane.showMessageDialog(null, "Enjoy your Break", "Break", JOptionPane.INFORMATION_MESSAGE);  
            b1.start();
            playSong.start();
          }
          
          if (Time.equals((((txtLunchTime.getText()).toString()).toUpperCase()).trim()))
          {
            playSong.stop();
            JOptionPane.showMessageDialog(null, "Enjoy your Lunch", "Lunch", JOptionPane.INFORMATION_MESSAGE);  
            l.start();
            playSong.start();
          }
          
          if (Time.equals((((txtBreak2Time.getText()).toString()).toUpperCase()).trim()))
          {
            playSong.stop();
            JOptionPane.showMessageDialog(null, "Enjoy your Break", "Break", JOptionPane.INFORMATION_MESSAGE);  
            b2.start();
            playSong.start();
          }
          
          if (Time.equals((((txtLeaveTime.getText()).toString()).toUpperCase()).trim()))
          {
            playSong.stop();
            JOptionPane.showMessageDialog(null, "Time to Leave Work!", "Time to Go", JOptionPane.INFORMATION_MESSAGE);  
            playSong.start();
          }
        }
      }
      
      public class AudioButtonHandler implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
          JFileChooser fc = new JFileChooser();
          int result = fc.showOpenDialog(null);
          if (value == JFileChooser.APPROVE_OPTION && value == JFileChooser.FILES_ONLY)
          {
            fileName = fc.getSelectedFile();
          }
        }
      
      public class ExitButtonHandler implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
          System.exit(0);	
        }
      }
      
      public class BreakTimeClock extends Thread 
      {
        public void run()
        {
          for (int t = 0; t<= 1; t++)
          {
            try
            {
              playSong.stop();
              Thread.sleep(900000);
              JOptionPane.showMessageDialog(null, "Time to go back to work", "Break Over", JOptionPane.INFORMATION_MESSAGE);
              b1.stop();
              b2.stop();
            }
            catch(InterruptedException e)
            {
            }
          )
        }
      }
      
      public class LunchTimeClock extends Thread
      {
        public void run()
        {
          for (int t = 0; t<= 1; t++)
          {
            try
            {
              playSong.stop();
              Thread.sleep(1800000);
              JOptionPane.showMessageDialog(null, "Time to go back to work", "Lunch Over", JOptionPane.INFORMATION_MESSAGE);
              l.stop();
            }
            catch(InterruptedException e)
            {
            }
          }
        }
      }
    
      public class Sound
      {
        private URL songPath;
    
        Sound(File fileName)
        {
          try
          {
            songPath = fileName.toURL();
            song = Applet.newAudioClip(songPath);
          }
          catch(Exception e)
          {
          }
        }
      }
      
      public class Playsong extends Thread
      {
        public void run()
        {
          song.play();
        }
      }
    
    }
    "I do know everything, just not all at once. It's a virtual memory problem."

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,877
    Rep Power
    25

    Default

    after two alarm popups the continuous clock stops
    What is it supposed to do?
    Sounds like a logic problem.
    Are there error messages?

    Your code has several compile errors in it. Please don't post code with coding errors without posting the console showing the errors with it.
    Last edited by Norm; 08-10-2008 at 04:49 PM.

  3. #3
    BruenorBH's Avatar
    BruenorBH is offline Member
    Join Date
    Jan 2008
    Location
    Buffalo, NY
    Posts
    14
    Rep Power
    0

    Default

    I am using Eclipse..

    after two alarm popups the continuous clock stops
    This means that while you are working when the Alarm time = Current Time you will get a popup message telling you to go on a break..

    Your code has several compile errors in it.
    I am still new to Java so I am unfamiliar with the compile errors.. The errors that I get in Eclipse are The method stop() from the type Thread is depreciated and The method toURL() from the type File is depreciated .. but the app runs without errors..
    "I do know everything, just not all at once. It's a virtual memory problem."

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,877
    Rep Power
    25

    Default

    The pop up messages are modal and will hold the thread they are on until the user replies.
    Thread playSong = new PlaySong();
    public class Playsong extends Thread
    {
    Note the uppercase S and the lowercase s. This would NOT compile as you have entered it.
    When a program doesn't compile, I never know if its the one that you are using. What you mention as errors must be warnings. I don't think a class file is generated with errors.

    I'm not sure what happens to a thread once it's been stopped.

    For the display of the Time to continue, you need to be sure that the thread it is on does NOT stop anywhere for too long.

    For ease of testing, I'd suggest you put all the classes in a single file. Also preload the times for future events to keep from having to type them in.
    Java Code:
          if(Testing) {
             //manually set one of the below times fields
             txtBreak1Time.setText(lblCurrentTime.getText());
             time.set(Calendar.SECOND, Sec+10);
             String nextTime = new SimpleDateFormat("hh:mm:ss a").format(time.getTime());
             txtBreak2Time.setText(nextTime);
             Testing = false; // one time only
          }
    Also preload
    File filename = new File("D:\\JavaDevelopment\\Testing\\audio\\spacemu sic.au")
    and add new Sound(fileName);
    before you start the timer.
    Last edited by Norm; 08-11-2008 at 04:15 AM.

  5. #5
    BruenorBH's Avatar
    BruenorBH is offline Member
    Join Date
    Jan 2008
    Location
    Buffalo, NY
    Posts
    14
    Rep Power
    0

    Default

    Norm this is setup for any user and they can input there wide range of Break and Lunch times.. I created this for Call Centers were people do not always remember to take thier lunch and breaks at the correct time.

    Note the uppercase S and the lowercase s.
    This was an oops since I am writing it on my laptop and then retyping it on my work pc in Notepad2 so it was a typing error and it was correct on my laptop.

    What you mention as errors must be warnings.
    You are correct these are warnings..

    For ease of testing, I'd suggest you put all the classes in a single file.
    How can I accomplish this? I currently have 4 Threads, 2 classes and 2 ActionListeners. I have not tried creating seperate files yet so it is all exactly like I entered below.

    Current Code:

    Java Code:
    import javax.swing.*;
    import java.applet.*;
    import java.net.*;
    import java.awt.*;
    import java.io.*;
    import java.awt.event.*;
    import java.util.Calendar;
    
    public class BreakClock extends JFrame 
    {
      // instance variables
      JLabel lblCurrentTime;
      JTextField txtStartTime;
      JTextField txtBreak1Time;
      JTextField txtLunchTime;
      JTextField txtBreak2Time;
      JTextField txtLeaveTime;
      String hour, min, sec, AMPM;
      AudioClip song;
      File fileName = new File("C:\\Program Files\\Break Clock\\Audio\\Horn.wav");  
    
      Thread b1 = new BreakTimeClock();
      Thread l = new LunchTimeClock(); 
      Thread b2 = new BreakTimeClock();
      Thread playSong = new PlaySong();
    	
      private ExitButtonHandler ebHandler;
      private AudioButtonHandler abHandler;
      
      public static void main(String[] args) 
      {
        new BreakClock();
      }
    
      /**
      * Constructor for objects of class StrategoApp
      */
      public BreakClock()
      {
        // Sets the properties of the app window
        this.setSize(250,225);
        this.setTitle("Break Clock");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	 
        JPanel panel = new JPanel();
    	
        // Sets the layout of the panel to align left
        panel.setLayout(new FlowLayout(FlowLayout.CENTER));
    	
        panel.add(new JLabel("The Current Time is:          "));
        lblCurrentTime = new JLabel("");
        panel.add(lblCurrentTime);
    	
        panel.add(new JLabel("Start Time:                   "));
        txtStartTime = new JTextField(8);
        panel.add(txtStartTime);
    
        panel.add(new JLabel("First Break Time:        "));
        txtBreak1Time = new JTextField(8);
        panel.add(txtBreak1Time);
    	
        panel.add(new JLabel("Lunch Time:                 "));
        txtLunchTime = new JTextField(8);
        panel.add(txtLunchTime);
    	
        panel.add(new JLabel("Second Break Time:  "));
        txtBreak2Time = new JTextField(8);
        panel.add(txtBreak2Time);
    	
        panel.add(new JLabel("Leave Time:                 "));
        txtLeaveTime = new JTextField(8);
        panel.add(txtLeaveTime);
    		
        JButton cmdAudio = new JButton("Audio");
        abHandler = new AudioButtonHandler();
        cmdAudio.addActionListener(abHandler);
        panel.add(cmdAudio);
    		
        JButton cmdQuit = new JButton("Quit");
        ebHandler = new ExitButtonHandler();
        cmdQuit.addActionListener(ebHandler);
        panel.add(cmdQuit);
    	
        this.add(panel);
        this.setVisible(true);
    	
        Timer t = new Timer(1000, new Tick(lblCurrentTime));
        t.start();
      }
      
      public class Tick implements ActionListener
      {
        JLabel label;
        Calendar time;
        
        Tick(JLabel lblCurrentTime)
        {
          time = Calendar.getInstance();
          label = lblCurrentTime;
        } 
        
        public void actionPerformed(ActionEvent event)
        {
          time.setTimeInMillis(System.currentTimeMillis());
          
          int Hour = time.get(Calendar.HOUR_OF_DAY);
          if (Hour > 12)
          {
            Hour = Hour - 12;
            AMPM = "PM";
          }
          
          else
          {
            AMPM = "AM";
          }
          
          String hour = Integer.toString(Hour);
          if (Hour <= 9)
          {
            // Setting the hour to a formatted string "00 - 09"
            hour = hour.trim();
            hour = "0" + hour;
          }
        	
          int Min = time.get(Calendar.MINUTE);
          String min = Integer.toString(Min);
          if (Min <= 9)
          {
            // Setting the minute to a formatted string "00 - 09"
            min = min.trim();
            min = "0" + min;
          }
        	
          int Sec = time.get(Calendar.SECOND);
          String sec = Integer.toString(Sec);
          if (Sec <= 9)
          {
           // Setting the second to a formatted string "00 - 09"
           sec = sec.trim();
           sec = "0" + sec;
          }
          
          // Updates the clock display
          lblCurrentTime.setText(hour + ":" + min + ":" + sec + " " + AMPM);
          
          // Stores the current time as string
          String Time = hour + ":" + min + ":" + sec + " " + AMPM;
          
          if (Time.equals((((txtStartTime.getText()).toString()).toUpperCase()).trim()))
          {
            playSong.stop();
            JOptionPane.showMessageDialog(null, "Time to Start Work!", "Time for Work", JOptionPane.INFORMATION_MESSAGE);
            playSong.start();
          }
          
          if (Time.equals((((txtBreak1Time.getText()).toString()).toUpperCase()).trim()))
          {
            playSong.stop();
            JOptionPane.showMessageDialog(null, "Enjoy your Break", "Break", JOptionPane.INFORMATION_MESSAGE);  
            b1.start();
            playSong.start();
          }
          
          if (Time.equals((((txtLunchTime.getText()).toString()).toUpperCase()).trim()))
          {
            playSong.stop();
            JOptionPane.showMessageDialog(null, "Enjoy your Lunch", "Lunch", JOptionPane.INFORMATION_MESSAGE);  
            l.start();
            playSong.start();
          }
          
          if (Time.equals((((txtBreak2Time.getText()).toString()).toUpperCase()).trim()))
          {
            playSong.stop();
            JOptionPane.showMessageDialog(null, "Enjoy your Break", "Break", JOptionPane.INFORMATION_MESSAGE);  
            b2.start();
            playSong.start();
          }
          
          if (Time.equals((((txtLeaveTime.getText()).toString()).toUpperCase()).trim()))
          {
            playSong.stop();
            JOptionPane.showMessageDialog(null, "Time to Leave Work!", "Time to Go", JOptionPane.INFORMATION_MESSAGE);  
            playSong.start();
          }
        }
      }
      
      public class AudioButtonHandler implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
          JFileChooser fc = new JFileChooser();
          int result = fc.showOpenDialog(null);
          if (value == JFileChooser.APPROVE_OPTION && value == JFileChooser.FILES_ONLY)
          {
            fileName = fc.getSelectedFile();
          }
        }
      
      public class ExitButtonHandler implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
          System.exit(0);	
        }
      }
      
      public class BreakTimeClock extends Thread 
      {
        public void run()
        {
          for (int t = 0; t<= 1; t++)
          {
            try
            {
              playSong.stop();
              Thread.sleep(900000);
              JOptionPane.showMessageDialog(null, "Time to go back to work", "Break Over", JOptionPane.INFORMATION_MESSAGE);
              playSong.start();
              b1.stop();
              b2.stop();
            }
            catch(InterruptedException e)
            {
            }
          )
        }
      }
      
      public class LunchTimeClock extends Thread
      {
        public void run()
        {
          for (int t = 0; t<= 1; t++)
          {
            try
            {
              playSong.stop();
              Thread.sleep(1800000);
              JOptionPane.showMessageDialog(null, "Time to go back to work", "Lunch Over", JOptionPane.INFORMATION_MESSAGE);
              playSong.start();
              l.stop();
            }
            catch(InterruptedException e)
            {
            }
          }
        }
      }
    
      public class Sound
      {
        private URL songPath;
    
        Sound(File fileName)
        {
          try
          {
            songPath = fileName.toURL();
            song = Applet.newAudioClip(songPath);
          }
          catch(Exception e)
          {
          }
        }
      }
      
      public class PlaySong extends Thread
      {
        public void run()
        {
          song.play();
        }
      }
    
    }
    Last edited by BruenorBH; 08-13-2008 at 08:40 AM.
    "I do know everything, just not all at once. It's a virtual memory problem."

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,877
    Rep Power
    25

    Default

    Running: C:\Program Files\Java\jdk1.5.0_04\bin\javac.exe -cp D:\JavaDevelopment\Testing\;D:\JavaDevelopment -deprecation -g -Xlint BreakClock.java

    BreakClock.java:228: illegal start of expression
    )
    ^
    BreakClock.java:229: ';' expected
    }
    ^
    BreakClock.java:278: '}' expected
    }
    ^
    3 errors

    3 error(s)
    Here's what I get when I tried to compile your code.
    Looks like there is a right paran vs a }
    Waste of time to post code that doesn't compile.

    Have you tried debugging the program? PUt in some println() statements to see where the execution goes.

  7. #7
    BruenorBH's Avatar
    BruenorBH is offline Member
    Join Date
    Jan 2008
    Location
    Buffalo, NY
    Posts
    14
    Rep Power
    0

    Default

    Waste of time to post code that doesn't compile.
    Sorry Norm when at work I am pressed for time so the retype of the code is not as good as the original.. Here is the code from my laptop since I am home now.. This only has the warnings that I have posted above..

    Java Code:
    /**
    * Break Clock (Java Version)
    * 
    * @author Michael Camardo
    * @version 1
    * @date 06/2008
    */
    
    import javax.swing.*;
    import java.applet.*;
    import java.net.*;
    import java.awt.*;
    import java.io.*;
    import java.awt.event.*;
    import java.util.Calendar;
    
    public class BreakClock extends JFrame 
    {
      // instance variables
      JLabel lblCurrentTime;
      JTextField txtStartTime;
      JTextField txtBreak1Time;
      JTextField txtLunchTime;
      JTextField txtBreak2Time;
      JTextField txtLeaveTime;
      String hour, min, sec, AMPM;
      AudioClip song;
      File fileName = new File("C:\\Program Files\\Break Clock\\Audio\\Horn.wav");
      
      Thread b1 = new BreakTimeClock();
      Thread l = new LunchTimeClock();
      Thread b2 = new BreakTimeClock();
      Thread playSong = new PlaySong();
      
      private AudioButtonHandler abHandler;
      private ExitButtonHandler ebHandler;
      
      public static void main(String[] args) 
      {
    	new BreakClock();
      }
    
      /**
      * Constructor for objects of class StrategoApp
      */
      public BreakClock()
      {
    	// Sets the properties of the app window
    	this.setSize(250,225);
    	this.setTitle("HSBC Break Clock");
    	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	 
    	JPanel panel = new JPanel();
    	
    	// Sets the layout of the panel to align center
    	panel.setLayout(new FlowLayout(FlowLayout.CENTER));
    	
    	panel.add(new JLabel("The Current Time is:          "));
    	lblCurrentTime = new JLabel("");
    	panel.add(lblCurrentTime);
    	
    	panel.add(new JLabel("Start Time:                   "));
    	txtStartTime = new JTextField(8);
    	panel.add(txtStartTime);
    	
    	panel.add(new JLabel("First Break Time:        "));
    	txtBreak1Time = new JTextField(8);
    	panel.add(txtBreak1Time);
    	
    	panel.add(new JLabel("Lunch Time:                 "));
    	txtLunchTime = new JTextField(8);
    	panel.add(txtLunchTime);
    	
    	panel.add(new JLabel("Second Break Time:  "));
    	txtBreak2Time = new JTextField(8);
    	panel.add(txtBreak2Time);
    	
    	panel.add(new JLabel("Leave Time:                 "));
    	txtLeaveTime = new JTextField(8);
    	panel.add(txtLeaveTime);
    	
    	JButton cmdAudio = new JButton("Audio");
    	abHandler = new AudioButtonHandler();
    	cmdAudio.addActionListener(abHandler);
    	panel.add(cmdAudio);
    	
    	JButton cmdQuit = new JButton("Quit");
    	ebHandler = new ExitButtonHandler();
    	cmdQuit.addActionListener(ebHandler);
    	panel.add(cmdQuit);
    	
    	this.add(panel);
    	this.setVisible(true);
    	
    	Timer t = new Timer(1000, new Tick(lblCurrentTime));
    	t.start();
      }
      
      public class Tick implements ActionListener
      {
        JLabel label;
        Calendar time;
        
        Tick(JLabel lblCurrentTime)
        {
          time = Calendar.getInstance();
          label = lblCurrentTime;
        } 
        
        public void actionPerformed(ActionEvent event)
        {
          time.setTimeInMillis(System.currentTimeMillis());
          
          int Hour = time.get(Calendar.HOUR_OF_DAY);
          if (Hour > 12)
          {
        	Hour = Hour - 12;
        	AMPM = "PM";
          }
          
          else
          {
            AMPM = "AM";
          }
          
          String hour = Integer.toString(Hour);
          if (Hour <= 9)
          {
            // Setting the hour to a formatted string "00 - 09"
        	hour = hour.trim();
            hour = "0" + hour;
          }
        	
          int Min = time.get(Calendar.MINUTE);
          String min = Integer.toString(Min);
          if (Min <= 9)
          {
        	// Setting the minute to a formatted string "00 - 09"
        	min = min.trim();
        	min = "0" + min;
          }
        	
          int Sec = time.get(Calendar.SECOND);
          String sec = Integer.toString(Sec);
          if (Sec <= 9)
          {
        	// Setting the second to a formatted string "00 - 09"
        	sec = sec.trim();
        	sec = "0" + sec;
          }
          
          //Updates clock display
          lblCurrentTime.setText(hour + ":" + min + ":" + sec + " " + AMPM);
          
          // Stores the current time as string
          String Time = hour + ":" + min + ":" + sec + " " + AMPM;
          
          //Pop-up messages for if equal to entry times
          if (Time.equals((((txtStartTime.getText()).toString()).toUpperCase()).trim()))
          {
        	playSong.stop();
            JOptionPane.showMessageDialog(null, "Time to Start Work!", "Time for Work", JOptionPane.INFORMATION_MESSAGE);
            playSong.start();
          }
    
          if (Time.equals((((txtBreak1Time.getText()).toString()).toUpperCase()).trim()))
          {
        	playSong.stop();
        	JOptionPane.showMessageDialog(null, "Enjoy your Break", "Break", JOptionPane.INFORMATION_MESSAGE);
        	b1.start();
        	playSong.start();
          }
    
          if (Time.equals((((txtLunchTime.getText()).toString()).toUpperCase()).trim()))
          {
        	playSong.stop();
        	JOptionPane.showMessageDialog(null, "Enjoy your Lunch", "Lunch", JOptionPane.INFORMATION_MESSAGE);
        	l.start();
        	playSong.start();
          }
          
          if (Time.equals((((txtBreak2Time.getText()).toString()).toUpperCase()).trim()))
          {
        	playSong.stop();
        	JOptionPane.showMessageDialog(null, "Enjoy your Break", "Break", JOptionPane.INFORMATION_MESSAGE);
            b2.start();
            playSong.start();
          }
    
          if (Time.equals((((txtLeaveTime.getText()).toString()).toUpperCase()).trim()))
          {
        	playSong.stop();
        	JOptionPane.showMessageDialog(null, "Time to Leave Work!", "Time to Go", JOptionPane.INFORMATION_MESSAGE);
        	playSong.start();
          }
        }
      }
      
      public class AudioButtonHandler implements ActionListener
      {
    	public void actionPerformed(ActionEvent e)
    	{
    	  JFileChooser fc = new JFileChooser();
    	  int result = fc.showOpenDialog(null);
    	  if (result == JFileChooser.APPROVE_OPTION && result == JFileChooser.FILES_ONLY)
    	  {
    		fileName = fc.getSelectedFile();
    	  }
    	}
      }
      
      public class ExitButtonHandler implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
          System.exit(0);	
        }
      }
      
      public class BreakTimeClock extends Thread
      {
        public void run()
        {
          for (int t = 0; t <= 1; t++)
          {
        	try
        	{
        	  playSong.stop();
              Thread.sleep(900000);
              JOptionPane.showMessageDialog(null, "Time to go back to work!", "Break Over", JOptionPane.INFORMATION_MESSAGE);
              playSong.start();
              b1.stop();
              b2.stop();
        	}
        	catch(InterruptedException e)
        	{
        	}
          }
        }  
      }
      
      public class LunchTimeClock extends Thread
      {
        public void run()
        {
          for (int t = 0; t <= 1; t++)
          {
        	try
        	{
        	  playSong.stop();
              Thread.sleep(1800000);
              JOptionPane.showMessageDialog(null, "Time to go back to work!", "Lunch Over", JOptionPane.INFORMATION_MESSAGE);
              playSong.start();
              l.stop();
        	}
        	catch(InterruptedException e)
        	{
        	}
          }
        }  
      }
      
      public class Sound
      {
    	private URL songPath;
    	
    	Sound(File fileName)
    	{
    	  try
    	  {
    		songPath = fileName.toURL();
    		song = Applet.newAudioClip(songPath);
    	  }
    	  catch(Exception e)
    	  {
    	  }
    	}
      }
      
      public class PlaySong extends Thread
      {
    	public void run()
    	{
    	  song.play();
    	}
      }
      
    }
    "I do know everything, just not all at once. It's a virtual memory problem."

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,877
    Rep Power
    25

    Default

    How do I have to execute this program to see the errors you see? If I can't/don't see them, I can't make any suggestions.
    That would include preloading all the fields, so that I only have to execute the code to see what's wrong.

    A suggestion:
    Move the calls to display message and play songs OUT of the timer code. Have the timer code start another thread to do those steps.
    Another:
    Use the java.util.Timer class since it has a schedule() method that could fire the display/play methods.

  9. #9
    BruenorBH's Avatar
    BruenorBH is offline Member
    Join Date
    Jan 2008
    Location
    Buffalo, NY
    Posts
    14
    Rep Power
    0

    Default

    Well I commented out all of the play and stop commands and put in the place of the song.play(); in txtStartTime compare which looks like:

    Java Code:
          //Pop-up messages for if equal to entry times
          if (Time.equals((((txtStartTime.getText()).toString()).toUpperCase()).trim()))
          {
            //playSong.stop();
            JOptionPane.showMessageDialog(null, "Time to Start Work!", "Time for Work", JOptionPane.INFORMATION_MESSAGE);
            //playSong.start();
            JOptionPane.showMessageDialog(null, "Song path is "+ song, "Blah Blah", JOptionPane.INFORMATION_MESSAGE);
          }
    song variable comes back as null... Hmm so the issue seems to be where the user selects the file and the sound class does not replace the default but changes it to null.
    Last edited by BruenorBH; 08-14-2008 at 05:22 AM.
    "I do know everything, just not all at once. It's a virtual memory problem."

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,877
    Rep Power
    25

    Default

    issue seems to be where the user selects the file and the sound class does not replace the default but changes it to null.
    what values do fileName and songPath have when song is null?

  11. #11
    BruenorBH's Avatar
    BruenorBH is offline Member
    Join Date
    Jan 2008
    Location
    Buffalo, NY
    Posts
    14
    Rep Power
    0

    Default

    From what I can tell the app never even uses the Sound class..
    "I do know everything, just not all at once. It's a virtual memory problem."

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,877
    Rep Power
    25

    Default

    Then that's the problem. Where else does song get set?
    It will stay null until assigned a value.

  13. #13
    BruenorBH's Avatar
    BruenorBH is offline Member
    Join Date
    Jan 2008
    Location
    Buffalo, NY
    Posts
    14
    Rep Power
    0

    Default

    So I completly removed the Sound class and decided to complete the task when needed. I am getting error Unhandled exception type MalformedURLException on both sections. I have this line of code at the variable declaration section and at the ActionListener section

    This is at the Variable Declaration section:
    Java Code:
    File fileName = new File("C:\\Program Files\\Break Clock\\Audio\\Horn.wav");
    URL songPath = fileName.toURL();
    AudioClip song = Applet.newAudioClip(songPath);
    This is the lines in ActionListener Section:
    Java Code:
    public class AudioButtonHandler implements ActionListener
    {
      public void actionPerformed(ActionEvent e)
      {
        JFileChooser fc = new JFileChooser();
        int result = fc.showOpenDialog(null);
        if (result == JFileChooser.APPROVE_OPTION && result == JFileChooser.FILES_ONLY)
        {
          fileName = fc.getSelectedFile();
          songPath = fileName.toURL();
          song = Applet.newAudioClip(songPath);
        }
      }
    }
    Last edited by BruenorBH; 08-23-2008 at 11:58 AM.
    "I do know everything, just not all at once. It's a virtual memory problem."

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,877
    Rep Power
    25

    Default

    Please copy and post the full text of the error message without editting it.

    It looks like you need to read the API doc for some methods you are using that can throw an exception. To Handle the exception, use a try{}catch(){} block around the method call.

  15. #15
    BruenorBH's Avatar
    BruenorBH is offline Member
    Join Date
    Jan 2008
    Location
    Buffalo, NY
    Posts
    14
    Rep Power
    0

    Default

    Norm,

    Here is a screen shot. I am using Eclipse as the debugger.
    Attached Thumbnails Attached Thumbnails Break Clock-break-clock.jpg  
    "I do know everything, just not all at once. It's a virtual memory problem."

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,877
    Rep Power
    25

    Default

    Sorry, the image is too small to read.
    Do it in a command prompt so you can copy and paste the text.

  17. #17
    BruenorBH's Avatar
    BruenorBH is offline Member
    Join Date
    Jan 2008
    Location
    Buffalo, NY
    Posts
    14
    Rep Power
    0

    Default

    This is what I get:
    mcamardo@Linux-Desktop:~$ cd /home/mcamardo/Documents/BreakClock/src
    mcamardo@Linux-Desktop:~/Documents/BreakClock/src$ javac BreakClock.java
    ----------
    1. WARNING in BreakClock.java (at line 17)
    public class BreakClock extends JFrame
    ^^^^^^^^^^
    The serializable class BreakClock does not declare a static final serialVersionUID field of type long
    ----------
    2. ERROR in BreakClock.java (at line 29)
    URL songPath = fileName.toURL();
    ^^^^^^^^^^^^^^^^
    Unhandled exception type MalformedURLException
    ----------
    3. ERROR in BreakClock.java (at line 213)
    songPath = fileName.toURL();
    ^^^^^^^^^^^^^^^^
    Unhandled exception type MalformedURLException
    ----------
    4. WARNING in BreakClock.java (at line 239)
    b1.stop();
    ^^^^^^^^^
    The method stop() from the type Thread is deprecated
    ----------
    5. WARNING in BreakClock.java (at line 240)
    b2.stop();
    ^^^^^^^^^
    The method stop() from the type Thread is deprecated
    ----------
    6. WARNING in BreakClock.java (at line 261)
    l.stop();
    ^^^^^^^^
    The method stop() from the type Thread is deprecated
    ----------
    6 problems (2 errors, 4 warnings)mcamardo@Linux-Desktop:~/Documents/BreakClock/src$


    for this code:
    Java Code:
    import javax.swing.*;
    
    import java.applet.*;
    
    import java.net.*;
    
    import java.awt.*;
    
    import java.io.*;
    
    import java.awt.event.*;
    
    import java.util.Calendar;
    
    
    
    public class BreakClock extends JFrame 
    
    {
    
      // instance variables
    
      JLabel lblCurrentTime;
    
      JTextField txtStartTime;
    
      JTextField txtBreak1Time;
    
      JTextField txtLunchTime;
    
      JTextField txtBreak2Time;
    
      JTextField txtLeaveTime;
    
      String hour, min, sec, AMPM;
    
    
    
      File fileName = new File("C:\\Program Files\\Break Clock\\Audio\\Horn.wav");
    
      URL songPath = fileName.toURL();
    
      AudioClip song = Applet.newAudioClip(songPath);
    
      
    
      Thread b1 = new BreakTimeClock();
    
      Thread l = new LunchTimeClock();
    
      Thread b2 = new BreakTimeClock();
    
      Thread playSong = new PlaySong();
    
      
    
      private AudioButtonHandler abHandler;
    
      private ExitButtonHandler ebHandler;
    
      
    
      public static void main(String[] args) 
    
      {
    
    	new BreakClock();
    
      }
    
    
    
      /**
    
      * Constructor for objects of class BreakClock
      */
    
      public BreakClock()
    
      {
    
    	// Sets the properties of the app window
    
    	this.setSize(250,225);
    
    	this.setTitle("Break Clock");
    
    	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    	
    
    	JPanel panel = new JPanel();
    
    	
    
    	// Sets the layout of the panel to align center
    
    	panel.setLayout(new FlowLayout(FlowLayout.CENTER));
    
    	
    
    	panel.add(new JLabel("The Current Time is:          "));
    
    	lblCurrentTime = new JLabel("");
    
    	panel.add(lblCurrentTime);
    
    	
    
    	panel.add(new JLabel("Start Time:                   "));
    
    	txtStartTime = new JTextField(8);
    
    	panel.add(txtStartTime);
    
    	
    
    	panel.add(new JLabel("First Break Time:        "));
    
    	txtBreak1Time = new JTextField(8);
    
    	panel.add(txtBreak1Time);
    
    	
    
    	panel.add(new JLabel("Lunch Time:                 "));
    
    	txtLunchTime = new JTextField(8);
    
    	panel.add(txtLunchTime);
    
    	
    
    	panel.add(new JLabel("Second Break Time:  "));
    
    	txtBreak2Time = new JTextField(8);
    
    	panel.add(txtBreak2Time);
    
    	
    
    	panel.add(new JLabel("Leave Time:                 "));
    
    	txtLeaveTime = new JTextField(8);
    
    	panel.add(txtLeaveTime);
    
    	
    
    	JButton cmdAudio = new JButton("Audio");
    
    	abHandler = new AudioButtonHandler();
    
    	cmdAudio.addActionListener(abHandler);
    
    	panel.add(cmdAudio);
    
    	
    
    	JButton cmdQuit = new JButton("Quit");
    
    	ebHandler = new ExitButtonHandler();
    
    	cmdQuit.addActionListener(ebHandler);
    
    	panel.add(cmdQuit);
    
    	
    
    	this.add(panel);
    
    	this.setVisible(true);
    
    	
    
    	Timer t = new Timer(1000, new Tick(lblCurrentTime));
    
    	t.start();
    
      }
    
      
    
      public class Tick implements ActionListener
    
      {
    
        JLabel label;
    
        Calendar time;
    
        
    
        Tick(JLabel lblCurrentTime)
    
        {
    
          time = Calendar.getInstance();
    
          label = lblCurrentTime;
    
        } 
    
        
    
        public void actionPerformed(ActionEvent event)
    
        {
    
          time.setTimeInMillis(System.currentTimeMillis());
    
          
    
          int Hour = time.get(Calendar.HOUR_OF_DAY);
    
          if (Hour > 12)
    
          {
    
        	Hour = Hour - 12;
    
        	AMPM = "PM";
    
          }
    
          
    
          else
    
          {
    
            AMPM = "AM";
    
          }
    
          
    
          String hour = Integer.toString(Hour);
    
          if (Hour <= 9)
    
          {
    
            // Setting the hour to a formatted string "00 - 09"
    
        	hour = hour.trim();
    
            hour = "0" + hour;
    
          }
    
        	
    
          int Min = time.get(Calendar.MINUTE);
    
          String min = Integer.toString(Min);
    
          if (Min <= 9)
    
          {
    
        	// Setting the minute to a formatted string "00 - 09"
    
        	min = min.trim();
    
        	min = "0" + min;
    
          }
    
        	
    
          int Sec = time.get(Calendar.SECOND);
    
          String sec = Integer.toString(Sec);
    
          if (Sec <= 9)
    
          {
    
        	// Setting the second to a formatted string "00 - 09"
    
        	sec = sec.trim();
    
        	sec = "0" + sec;
    
          }
    
          
    
          //Updates clock display
    
          lblCurrentTime.setText(hour + ":" + min + ":" + sec + " " + AMPM);
    
          
    
          // Stores the current time as string
    
          String Time = hour + ":" + min + ":" + sec + " " + AMPM;
    
          
    
          //Pop-up messages for if equal to entry times
    
          if (Time.equals((((txtStartTime.getText()).toString()).toUpperCase()).trim()))
    
          {
    
        	//playSong.stop();
    
            JOptionPane.showMessageDialog(null, "Time to Start Work!", "Time for Work", JOptionPane.INFORMATION_MESSAGE);
    
            //playSong.start();
    
            JOptionPane.showMessageDialog(null, "The song is" + song, "Time for Work", JOptionPane.INFORMATION_MESSAGE);
    
            
    
          }
    
    
    
          if (Time.equals((((txtBreak1Time.getText()).toString()).toUpperCase()).trim()))
    
          {
    
        	//playSong.stop();
    
        	JOptionPane.showMessageDialog(null, "Enjoy your Break", "Break", JOptionPane.INFORMATION_MESSAGE);
    
        	b1.start();
    
        	//playSong.start();
    
          }
    
    
    
          if (Time.equals((((txtLunchTime.getText()).toString()).toUpperCase()).trim()))
    
          {
    
        	//playSong.stop();
    
        	JOptionPane.showMessageDialog(null, "Enjoy your Lunch", "Lunch", JOptionPane.INFORMATION_MESSAGE);
    
        	l.start();
    
        	//playSong.start();
    
          }
    
          
    
          if (Time.equals((((txtBreak2Time.getText()).toString()).toUpperCase()).trim()))
    
          {
    
        	//playSong.stop();
    
        	JOptionPane.showMessageDialog(null, "Enjoy your Break", "Break", JOptionPane.INFORMATION_MESSAGE);
    
            b2.start();
    
            //playSong.start();
    
          }
    
    
    
          if (Time.equals((((txtLeaveTime.getText()).toString()).toUpperCase()).trim()))
    
          {
    
        	//playSong.stop();
    
        	JOptionPane.showMessageDialog(null, "Time to Leave Work!", "Time to Go", JOptionPane.INFORMATION_MESSAGE);
    
        	//playSong.start();
    
          }
    
        }
    
      }
    
      
    
      public class AudioButtonHandler implements ActionListener
    
      {
    
    	public void actionPerformed(ActionEvent e)
    
    	{
    
    	  JFileChooser fc = new JFileChooser();
    
    	  int result = fc.showOpenDialog(null);
    
    	  if (result == JFileChooser.APPROVE_OPTION && result == JFileChooser.FILES_ONLY)
    
    	  {
    
    		fileName = fc.getSelectedFile();
    
    		songPath = fileName.toURL();
    
    		song = Applet.newAudioClip(songPath);
    
    	  }
    
    	}
    
      }
    
      
    
      public class ExitButtonHandler implements ActionListener
    
      {
    
        public void actionPerformed(ActionEvent e)
    
        {
    
          System.exit(0);	
    
        }
    
      }
    
      
    
      public class BreakTimeClock extends Thread
    
      {
    
        public void run()
    
        {
    
          for (int t = 0; t <= 1; t++)
    
          {
    
        	try
    
        	{
    
        	  //playSong.stop();
    
              Thread.sleep(900000);
    
              JOptionPane.showMessageDialog(null, "Time to go back to work!", "Break Over", JOptionPane.INFORMATION_MESSAGE);
    
              //playSong.start();
    
              b1.stop();
    
              b2.stop();
    
        	}
    
        	catch(InterruptedException e)
    
        	{
    
        	}
    
          }
    
        }  
    
      }
    
      
    
      public class LunchTimeClock extends Thread
    
      {
    
        public void run()
    
        {
    
          for (int t = 0; t <= 1; t++)
    
          {
    
        	try
    
        	{
    
        	  //playSong.stop();
    
              Thread.sleep(1800000);
    
              JOptionPane.showMessageDialog(null, "Time to go back to work!", "Lunch Over", JOptionPane.INFORMATION_MESSAGE);
    
              //playSong.start();
    
              l.stop();
    
        	}
    
        	catch(InterruptedException e)
    
        	{
    
        	}
    
          }
    
        }  
    
      }
    
      
    
      public class PlaySong extends Thread
    
      {
    
    	public void run()
    
    	{
    
    	  song.play();
    
    	}
    
      }
    
      
    
    }
    "I do know everything, just not all at once. It's a virtual memory problem."

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,877
    Rep Power
    25

    Default

    Unhandled exception type MalformedURLException
    The compiler is saying that the method called CAN return a MalformedURLException which your code is NOT handling. You need to add a try{}catch(){} block around the instruction to handle the exception.
    The authors of Java have decided that the stop method of Thread is dangerous to use and are telling you that by deprecating it. It's only a warning and a class file is generated.
    Read the API doc for more info.

  19. #19
    BruenorBH's Avatar
    BruenorBH is offline Member
    Join Date
    Jan 2008
    Location
    Buffalo, NY
    Posts
    14
    Rep Power
    0

    Default

    Sorry been very busy at work latley so haven't gotten far.. I added the try{} and catch(){} error handling to the break clock in the file selection. It works now somewhat. The origional issue of it not playing the song is still present (it selects the file when testing but never plays it) and after 2 consecutive popups the app freezes and in the cmd window I get:

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
    at java.lang.Thread.start(Unknown Source)
    at BreakClock$Tick.actionPerformed(BreakClock.java:16 4)
    at javax.swing.Timer.fireActionPerformed(Unknown Source)
    at javax.swing.Timer$DoPostEvent.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    Here is the current code:

    Java Code:
    /**
    * Break Clock (Java Version)
    * 
    * @author Michael Camardo
    * @version 1
    * @date 06/2008
    */
    
    import javax.swing.*;
    import java.applet.*;
    import java.net.*;
    import java.awt.*;
    import java.io.*;
    import java.awt.event.*;
    import java.util.Calendar;
    
    public class BreakClock extends JFrame 
    {
      // instance variables
      JLabel lblCurrentTime;
      JTextField txtStartTime;
      JTextField txtBreak1Time;
      JTextField txtLunchTime;
      JTextField txtBreak2Time;
      JTextField txtLeaveTime;
      String hour, min, sec, AMPM;
      
      File fileName;
      URL songPath;
      AudioClip song;
    
      Thread b1 = new BreakTimeClock();
      Thread l = new LunchTimeClock(); 
      Thread b2 = new BreakTimeClock();
      Thread PlaySong = new PlaySong();
    	
      private ExitButtonHandler ebHandler;
      private AudioButtonHandler abHandler;
      
      public static void main(String[] args) 
      {
        new BreakClock();
      }
    
      /**
      * Constructor for objects of class StrategoApp
      */
      public BreakClock()
      {
        // Sets the properties of the app window
        this.setSize(250,225);
        this.setTitle("Break Clock");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	 
        JPanel panel = new JPanel();
    	
        // Sets the layout of the panel to align left
        panel.setLayout(new FlowLayout(FlowLayout.CENTER));
    	
        panel.add(new JLabel("The Current Time is:          "));
        lblCurrentTime = new JLabel("");
        panel.add(lblCurrentTime);
    	
        panel.add(new JLabel("Start Time:                   "));
        txtStartTime = new JTextField(8);
        panel.add(txtStartTime);
    
        panel.add(new JLabel("First Break Time:        "));
        txtBreak1Time = new JTextField(8);
        panel.add(txtBreak1Time);
    	
        panel.add(new JLabel("Lunch Time:                 "));
        txtLunchTime = new JTextField(8);
        panel.add(txtLunchTime);
    	
        panel.add(new JLabel("Second Break Time:  "));
        txtBreak2Time = new JTextField(8);
        panel.add(txtBreak2Time);
    	
        panel.add(new JLabel("Leave Time:                 "));
        txtLeaveTime = new JTextField(8);
        panel.add(txtLeaveTime);
    		
        JButton cmdAudio = new JButton("Audio");
        abHandler = new AudioButtonHandler();
        cmdAudio.addActionListener(abHandler);
        panel.add(cmdAudio);
    		
        JButton cmdQuit = new JButton("Quit");
        ebHandler = new ExitButtonHandler();
        cmdQuit.addActionListener(ebHandler);
        panel.add(cmdQuit);
    	
        this.add(panel);
        this.setVisible(true);
    	
        Timer t = new Timer(1000, new Tick(lblCurrentTime));
        t.start();
      }
      
      public class Tick implements ActionListener
      {
        JLabel label;
        Calendar time;
        
        Tick(JLabel lblCurrentTime)
        {
          time = Calendar.getInstance();
          label = lblCurrentTime;
        } 
        
        public void actionPerformed(ActionEvent event)
        {
          time.setTimeInMillis(System.currentTimeMillis());
          
          int Hour = time.get(Calendar.HOUR_OF_DAY);
          if (Hour > 12)
          {
            Hour = Hour - 12;
            AMPM = "PM";
          }
          
          else
          {
            AMPM = "AM";
          }
          
          String hour = Integer.toString(Hour);
          if (Hour <= 9)
          {
            // Setting the hour to a formatted string "00 - 09"
            hour = hour.trim();
            hour = "0" + hour;
          }
        	
          int Min = time.get(Calendar.MINUTE);
          String min = Integer.toString(Min);
          if (Min <= 9)
          {
            // Setting the minute to a formatted string "00 - 09"
            min = min.trim();
            min = "0" + min;
          }
        	
          int Sec = time.get(Calendar.SECOND);
          String sec = Integer.toString(Sec);
          if (Sec <= 9)
          {
           // Setting the second to a formatted string "00 - 09"
           sec = sec.trim();
           sec = "0" + sec;
          }
          
          // Updates the clock display
          lblCurrentTime.setText(hour + ":" + min + ":" + sec + " " + AMPM);
          
          // Stores the current time as string
          String Time = hour + ":" + min + ":" + sec + " " + AMPM;
          
          if (Time.equals((((txtStartTime.getText()).toString()).toUpperCase()).trim()))
          {
            PlaySong.stop();
            JOptionPane.showMessageDialog(null, "Time to Start Work!", "Time for Work", JOptionPane.INFORMATION_MESSAGE);
            PlaySong.start();
          }
          
          if (Time.equals((((txtBreak1Time.getText()).toString()).toUpperCase()).trim()))
          {
            PlaySong.stop();
            JOptionPane.showMessageDialog(null, "Enjoy your Break", "Break", JOptionPane.INFORMATION_MESSAGE);  
            b1.start();
            PlaySong.start();
          }
          
          if (Time.equals((((txtLunchTime.getText()).toString()).toUpperCase()).trim()))
          {
            PlaySong.stop();
            JOptionPane.showMessageDialog(null, "Enjoy your Lunch", "Lunch", JOptionPane.INFORMATION_MESSAGE);  
            l.start();
            PlaySong.start();
          }
          
          if (Time.equals((((txtBreak2Time.getText()).toString()).toUpperCase()).trim()))
          {
            PlaySong.stop();
            JOptionPane.showMessageDialog(null, "Enjoy your Break", "Break", JOptionPane.INFORMATION_MESSAGE);  
            b2.start();
            PlaySong.start();
          }
          
          if (Time.equals((((txtLeaveTime.getText()).toString()).toUpperCase()).trim()))
          {
            PlaySong.stop();
            JOptionPane.showMessageDialog(null, "Time to Leave Work!", "Time to Go", JOptionPane.INFORMATION_MESSAGE);  
            PlaySong.start();
          }
        }
      }
      
      public class AudioButtonHandler implements ActionListener
      {
        public void actionPerformed(ActionEvent event)
        {
          JFileChooser fc = new JFileChooser();
          int result = fc.showOpenDialog(null);
          if (result == JFileChooser.APPROVE_OPTION && result == JFileChooser.FILES_ONLY)
          {
            try
            {
              fileName = fc.getSelectedFile();
              songPath = fileName.toURL();
              song = Applet.newAudioClip(songPath);
            }
            catch(IOException e)
            {
            }
          }
        }
      }
      
      public class ExitButtonHandler implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
          System.exit(0);	
        }
      }
      
      public class BreakTimeClock extends Thread 
      {
        public void run()
        {
          for (int t = 0; t<= 1; t++)
          {
            try
            {
              PlaySong.stop();
              Thread.sleep(900000);
              JOptionPane.showMessageDialog(null, "Time to go back to work", "Break Over", JOptionPane.INFORMATION_MESSAGE);
              PlaySong.start();
              b1.stop();
              b2.stop();
            }
            catch(InterruptedException e)
            {
            }
          }
        }
      }
      
      public class LunchTimeClock extends Thread
      {
        public void run()
        {
          for (int t = 0; t<= 1; t++)
          {
            try
            {
              PlaySong.stop();
              Thread.sleep(1800000);
              JOptionPane.showMessageDialog(null, "Time to go back to work", "Lunch Over", JOptionPane.INFORMATION_MESSAGE);
              PlaySong.start();
              l.stop();
            }
            catch(InterruptedException e)
            {
            }
          }
        }
      }
    
      public class Sound
      {
        private URL songPath;
    
        Sound(File fileName)
        {
          try
          {
            songPath = fileName.toURL();
            song = Applet.newAudioClip(songPath);
          }
          catch(Exception e)
          {
          }
        }
      }
      
      public class PlaySong extends Thread
      {
        public void run()
        {
          song.play();
        }
      }
    
    }
    "I do know everything, just not all at once. It's a virtual memory problem."

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,877
    Rep Power
    25

    Default

    IllegalThreadStateException
    at java.lang.Thread.start(Unknown Source)
    At line 164 you do a start on a thread that is not in a legal start.
    IF you read the doc for start you'll see that you get the error
    - if the thread was already started.
    What is the start of the thread? Has it been started already? Read the API doc for threads. There are methods that will help you.

Page 1 of 2 12 LastLast

Similar Threads

  1. How to use Break
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:45 PM
  2. Help please in digital clock
    By jaidod in forum Java Applets
    Replies: 1
    Last Post: 04-17-2008, 05:05 PM
  3. clock class program
    By jvasilj1 in forum New To Java
    Replies: 12
    Last Post: 02-01-2008, 09:27 PM
  4. Pogo clock problem
    By tampacurt in forum New To Java
    Replies: 0
    Last Post: 11-17-2007, 03:18 AM
  5. dynamically updating clock
    By malakaherath in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 08:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •