HI. I'm new at programing in Java and I made a little program that:
1. Show current date
2. Show curent time
3. When button about pressed pop up about
4. When button setalarm pressed set Alarm(for begining in minutes)
The problem is when i run the program everything works OK. Setting alarm and choosing files works ok but after that when the countdown begins program freeze(time stoped refreshing) and after countdown everything forks fine. I realy need help becouse I tried many things and nothing doesn't work.

MY MAIN CLASS:

import java.awt.event.ActionEvent;

import javax.swing.JFrame;

public class Executor {


    public static void main(String[] args) throws InterruptedException {
        new Executor();

    }
    public Executor() throws InterruptedException{
        new clock();
    }
}

MY CODE FOR ALARM:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.Date;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

//sound
import java.awt.Component;
import java.io.*;
import javax.swing.JFileChooser;
import sun.audio.*;


public class clock extends JFrame{

    //components
    JTextField timeF;
    JPanel panel;
    JLabel string;
    JTextField date;
    JTextField countd;
    JTextField alarm;



    //Buttons for About/Exit/Set Alarm

    //buttons
    public JButton about;
    public JButton exit;
    public JButton setalarm;




    //program

    public clock(){
        // housekeeping
        super("Java Clock by Boris v0.1A");
        setSize(650,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setLocationRelativeTo(null);

        //initiliaze the panel
        panel = new JPanel();
        panel.setLayout(new FlowLayout());

        //initiliaze the date
        date = new JTextField(10);
        date.setEditable(false);
        date.setFont(new Font("Arial",Font.PLAIN,36));

        Calendar rightNow = Calendar.getInstance();

        int year = rightNow.get(Calendar.YEAR);
        int day= rightNow.get(Calendar.DAY_OF_MONTH);
        int month = rightNow.get(Calendar.MONTH);

        date.setText("Date: "+day+"/"+month+"/"+year);

        panel.add(date);
        add(panel);

        //initiliaze the textField for time
        timeF = new JTextField(10);
        timeF.setEditable(false);
        timeF.setFont(new Font("Arial",Font.PLAIN,36));


        panel.add(timeF);

        add(panel);

        Timer t = new Timer(1000,new Listener());
        t.start();

        //initialize buttons for app
        //ABOUT
                about = new JButton("About");
                panel.add(about);
                add(panel);

                AboutClass handler = new AboutClass();
                about.addActionListener(handler);
        //SetAlarm
            setalarm = new JButton("Set Alarm");
            panel.add(setalarm);
            add(panel);

            setAlarm set = new setAlarm();
            setalarm.addActionListener(set);


            //classes for setting up things


}
    class Listener implements ActionListener{
        public void actionPerformed(ActionEvent e){

            Calendar rightNow = Calendar.getInstance();

            int hour = rightNow.get(Calendar.HOUR_OF_DAY);
            int min = rightNow.get(Calendar.MINUTE);
            int sec = rightNow.get(Calendar.SECOND);

            timeF.setText("Time: " + hour+":"+min+":"+sec);

        }

    }
    //Button for about


    private class AboutClass implements ActionListener{
        public void actionPerformed(ActionEvent event){
            JOptionPane.showMessageDialog(null, String.format("About\nThis app is everything you need to wake up\nin the mornings.", event.getActionCommand()));
        }
    }

    //Button for SetAlarm


        private class setAlarm implements ActionListener{
        public void actionPerformed(ActionEvent event){
        String minute = JOptionPane.showInputDialog("Alarm set(minutes)");
        int min = Integer.parseInt(minute);

        countd = new JTextField(20);
        countd.setEditable(false);
        countd.setFont(new Font("Arial",Font.PLAIN,36));

        panel.add(countd);
        add(panel);

        countd.setText("Alarm is set for "+ min + " seconds from now");

        try{
            JFileChooser openf = new JFileChooser();
            Component j = null;
            openf.showOpenDialog(j);
            File fl=openf.getSelectedFile();



            for(int i = min*60;i>=0;i-=1){


                //initialize for countdown
                countd.setText("Alarm is set for "+ i + " seconds from now");

                Thread.sleep(1000);
            }


            for(int x=0;x < 20;x++){ // for looping trought selected sound 20 times
            String st = fl.getAbsolutePath();
            InputStream in = new FileInputStream(st); 
            AudioStream au = new AudioStream(in);
            AudioPlayer.player.start(au);
            Thread.sleep(1000);


            }
        }
        catch(Exception e){}
        }
    }
    }

Edited 3 Years Ago by ZixCo

Everything to do with Swing (eg painting the screen, running actionPerformed methods) happens on a single thread - the "Event Dispatch Thread", or "EDT", or "Swing thread".
That means that once your actionPerformed method starts NOTHING else will happen in Swing, including no screen updates, until your method finishes. You can update stuff and loop and sleep as much as you like, but none of that will affect what's on the screen until your method has terminated.
If you want to do stuff after a period of time, here is the right way:
In your actionPerformed start a javax.swing.Timer, and return. When the timer fires you can update whatever needs updating and return. While the timer is running Swing will be free to update the screen.

Thanks for that!
Could someone please write that in code for me couse I'm 14 years old and I'm new at Java. So please be patient and write that. You will help me a lot. :D

Edited 3 Years Ago by ZixCo

You're doing very well for 14! And the best way to keep improving is to write this yourself. It's harder, yes, but that's how you learn. Keep trying and people here will help.
Here's a good tutorial on how to use a Timer
http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html
it focusses on repeating events (eg updating an animation), but in your case you will use setRepeats(false) so the Timer just runs and fires once.

I'll be stopping for the night very soon, but there will be others here to help.
J

Thanks for taking time and finding this stuff for me. I program in C++ too but then I figured out that in C++ is harder to write graphic stuff so I decided I need a little change and I started to learn Java. It's grat and I love it. I'm working on Linux so code in C++ isn't the same when you compile it(you probably know that :P). So thanks for all your help, I hope you will follow my progress and help me if I won't know something. So good night and thanks again. :D

Can somebody help me couse I don't get it. I mean I understand everything but I can't code it right. Anyone ready to help?

This question has already been answered. Start a new discussion instead.