Allright. It's me again. :D

This time, I'm making an alarm clock program.
I've already completed the bit that gets and displays the system time:

    Date vrijeme = new Date();    
    int sati = vrijeme.getHours();
    int minuti = vrijeme.getMinutes();
    int sekunde = vrijeme.getSeconds();

Also made a method that compares that time to the desired alarm time:

    public void prikažiVrijeme(){
        Variables V1 = new Variables();
        trenutnoVrijeme.setText(String.valueOf(V1.sati)+":"+String.valueOf(V1.minuti)+":"+String.valueOf(V1.sekunde));              
        V1.upamćenoVrijeme = String.valueOf(V1.sati)+":"+String.valueOf(V1.minuti);
        traženoVrijemeGUI.setText(sat.getText()+":"+minut.getText());
        V1.traženoVrijeme = sat.getText()+":"+minut.getText();
        if (provjeraVremena(V1.upamćenoVrijeme, V1.traženoVrijeme)){
            // PLAY MP3 HERE
        }
    }    

    public boolean provjeraVremena(String upamćeno, String traženo){
        boolean provjeravanje = false;

        if (upamćeno.equals(traženo)){
            provjeravanje = true;
        }

        return provjeravanje;
    }

Tested it with a simple JTextField.setText("It works!");
I used java.util.Timer; to animate the digital clock in the GUI (and also compare times every second).
Is it possible to load an .mp3/.wav file and play it?
Later I'd add an option to select your own .mp3, right now I just want any .mp3 to play.

it's pretty easy to play sound files.
.wav files are supported by core Java, no additional libraries required.
if you want to play .mp3 files, on the other hand, you'll have to look for some additional resources: jmf (even though a bit outdated) can help you, but in my personal experience, the JLayer library is pretty easy to handle with.

Thanks stultuske.
I've played around with this, and managed to cook up this:

    public void prikažiVrijeme(){
        Variables V1 = new Variables();
        trenutnoVrijeme.setText(String.valueOf(V1.sati)+":"+String.valueOf(V1.minuti)+":"+String.valueOf(V1.sekunde));              
        V1.upamćenoVrijeme = String.valueOf(V1.sati)+":"+String.valueOf(V1.minuti);
        traženoVrijemeGUI.setText(sat.getText()+":"+minut.getText());
        V1.traženoVrijeme = sat.getText()+":"+minut.getText();
        if (provjeraVremena(V1.upamćenoVrijeme, V1.traženoVrijeme)){
            V1.muzika(); // the music method
        }
    }  

the V1.muzika method looks like this:

    public void muzika() {
        AudioPlayer BGP = AudioPlayer.player;
        AudioStream BGM;
        AudioData MD;
        ContinuousAudioDataStream loop;
        loop = null;

        try {
            BGM = new AudioStream(new FileInputStream("ding_on.wav"));
            MD = BGM.getData();
            loop = new ContinuousAudioDataStream(MD);
        } catch (IOException error) {
        }

        BGP.start();
    }

And when I run it, once the system time reaches the alarm time, I get this error:

Exception in thread "Timer-0" java.lang.IllegalThreadStateException
    at java.lang.Thread.start(Thread.java:682)
    at alarmclock.Variables.muzika(Variables.java:35)
    at alarmclock.GUI.prikažiVrijeme(GUI.java:26)
    at alarmclock.VrijemeTask.run(VrijemeTask.java:22)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)
Java Result: 1

Might my timer be causing problems?

Two quick points:
1. Date.getHours etc have been deprecated since Java 1.1 (in 1997!) You should use GregorianCalendar instead
2. For updating a GUI you should use a javax.swing.Timer to run your updates on the swing thread, not a java.util.Timer which runs in its own thread.

To be honest, I have never liked Java's Date and Calendar stuff, and luckily there's this available.

Alright James, how does this look:

package alarmclock;

import java.util.Calendar;
import java.util.GregorianCalendar;


public class Promjenjive {

    String trenutnoVrijeme = tačnoVrijeme();    

    public String tačnoVrijeme(){
        String vrijeme;
        GregorianCalendar gregCan = new GregorianCalendar();

        int sat = gregCan.get(Calendar.HOUR);
        int minut = gregCan.get(Calendar.MINUTE);
        int sekund = gregCan.get(Calendar.SECOND);
        vrijeme = (String.valueOf(sat)+":"+String.valueOf(minut)+":"+String.valueOf(sekund));

        return vrijeme;
    }
}

I'm having difficulties with action listeners and swing timers, it seems I don't fully understand how to construct a timer and make it update a jLabel in the GUI.
I've been reading things like this. The part that I don't understand are the action listeners.
I've managed to get a java.util.timer to update the jLabel, but you said that's wrong so I'm trying to understand the javax.swing.timer.
Help?

PS
Haven't had much time to practice code for the last 2 days, but now I'm back. :)

The part that I don't understand are the action listeners.
I've managed to get a java.util.timer to update the jLabel, but you said that's wrong so I'm trying to understand the javax.swing.timer.

An action listener is a class that implements java.awt.event.ActionListener and has a actionPerformed(java.awt.event.ActionEvent e) method. This should work for you just as well as when you created a TimerTask for java.util.Timer, and it is even better for Swing because it will run in the event dispatch thread which makes it safe. The only part that might be tricky is that ActionListeners can listen to things other than timers. I recommend that you create a special ActionListener just for your timer instead of reusing an ActionListener that you have listening to other things.

If for any reason you are unable to understand ActionListener then you can get the same thread safety by calling javax.swing.SwingUtilities.invokeLater(Runnable doRun) inside the run method of your TimerTask, with doRun being a Runnable that does what you really want the timer to do. You can use invokeLater in any situation to do something on the event dispatch thread, with the down-side being that it won't happen until sometime after you call invokeLater. You can also use invokeAndWait which blocks until the thing you want to do is actually done, but there's no reason to do a thing like that for a timer and it is more complicated than invokeLater.

I'm still wrestling with listeners! :@

and who's winning?

Listeners are pretty easy to use, but you need to know which Listener to use when, and in which method to put your logic.

Right now, it's a stalemate. :)
My university covers the basics of java programing in the first semestre, and in the second semestre we have a subject called "data structures and algorithms".
We'll cover listeners and all GUI related coding in the third semestre, in a subject called "graphical user interfaces". :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.