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.


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();


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;

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

public class clock extends JFrame{

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

    //Buttons for About/Exit/Set Alarm

    public JButton about;
    public JButton exit;
    public JButton setalarm;


    public clock(){
        // housekeeping
        super("Java Clock by Boris v0.1A");

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

        //initiliaze the date
        date = new JTextField(10);
        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);


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



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

        //initialize buttons for app
                about = new JButton("About");

                AboutClass handler = new AboutClass();
            setalarm = new JButton("Set Alarm");

            setAlarm set = new setAlarm();

            //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.setFont(new Font("Arial",Font.PLAIN,36));


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

            JFileChooser openf = new JFileChooser();
            Component j = null;
            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");


            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);

        catch(Exception e){}

Recommended Answers

All 5 Replies

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

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
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.

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?

Be a part of the DaniWeb community

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