Results 1 to 20 of 21
Thread: Break Clock
- 08-10-2008, 06:20 AM #1
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."
- 08-10-2008, 02:04 PM #2
What is it supposed to do?after two alarm popups the continuous clock stops
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 03:49 PM.
- 08-11-2008, 02:55 AM #3
I am using Eclipse..
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..after two alarm popups the continuous clock stops
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..Your code has several compile errors in it."I do know everything, just not all at once. It's a virtual memory problem."
- 08-11-2008, 03:11 AM #4
The pop up messages are modal and will hold the thread they are on until the user replies.
Note the uppercase S and the lowercase s. This would NOT compile as you have entered it.Thread playSong = new PlaySong();
public class Playsong extends Thread
{
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.
Also preloadJava 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 }
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 03:15 AM.
- 08-13-2008, 05:16 AM #5
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.
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.Note the uppercase S and the lowercase s.
You are correct these are warnings..What you mention as errors must be warnings.
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.For ease of testing, I'd suggest you put all the classes in a single file.
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 07:40 AM.
"I do know everything, just not all at once. It's a virtual memory problem."
- 08-13-2008, 01:54 PM #6
Here's what I get when I tried to compile your code.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)
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.
- 08-13-2008, 09:59 PM #7
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..Waste of time to post code that doesn't compile.
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."
- 08-14-2008, 12:00 AM #8
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.
- 08-14-2008, 04:20 AM #9
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:
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.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); }Last edited by BruenorBH; 08-14-2008 at 04:22 AM.
"I do know everything, just not all at once. It's a virtual memory problem."
- 08-14-2008, 02:06 PM #10
what values do fileName and songPath have when song is null?issue seems to be where the user selects the file and the sound class does not replace the default but changes it to null.
- 08-15-2008, 03:39 AM #11
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."
- 08-15-2008, 01:29 PM #12
Then that's the problem. Where else does song get set?
It will stay null until assigned a value.
- 08-23-2008, 10:55 AM #13
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:
This is the lines in ActionListener Section:Java Code:File fileName = new File("C:\\Program Files\\Break Clock\\Audio\\Horn.wav"); URL songPath = fileName.toURL(); AudioClip song = Applet.newAudioClip(songPath);
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 10:58 AM.
"I do know everything, just not all at once. It's a virtual memory problem."
- 08-23-2008, 02:19 PM #14
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.
- 08-28-2008, 06:30 AM #15
Norm,
Here is a screen shot. I am using Eclipse as the debugger."I do know everything, just not all at once. It's a virtual memory problem."
- 08-28-2008, 01:40 PM #16
Sorry, the image is too small to read.
Do it in a command prompt so you can copy and paste the text.
- 08-28-2008, 03:35 PM #17
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."
- 08-28-2008, 03:47 PM #18
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.Unhandled exception type MalformedURLException
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.
- 09-11-2008, 04:53 AM #19
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."
- 09-11-2008, 02:50 PM #20
At line 164 you do a start on a thread that is not in a legal start.IllegalThreadStateException
at java.lang.Thread.start(Unknown Source)
IF you read the doc for start you'll see that you get the errorWhat is the start of the thread? Has it been started already? Read the API doc for threads. There are methods that will help you.- if the thread was already started.
Similar Threads
-
How to use Break
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:45 PM -
Help please in digital clock
By jaidod in forum Java AppletsReplies: 1Last Post: 04-17-2008, 04:05 PM -
clock class program
By jvasilj1 in forum New To JavaReplies: 12Last Post: 02-01-2008, 08:27 PM -
Pogo clock problem
By tampacurt in forum New To JavaReplies: 0Last Post: 11-17-2007, 02:18 AM -
dynamically updating clock
By malakaherath in forum New To JavaReplies: 2Last Post: 08-01-2007, 07:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks