Results 1 to 4 of 4
  1. #1
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    107
    Rep Power
    0

    Default Not allowing me to close project

    I have tried to strip down my project to what you see. What the program should be is an alarm system for my computer, that loads the alarms from an external file named ALARMS.txt in my C: drive. What I expected to happen was to click on the START ALARMS label and have the alarms load and continuously check. What IS happening is the alarms are loading, but even with a 1 second sleep, I am still unable to close my window. Am I missing something?
    Java Code:
    package com.scheduler.ryan;
    
    import java.awt.Color;
    import java.awt.Desktop;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.StringTokenizer;
    
    import javax.swing.JComboBox;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class Alarm2 extends JComponent{
    
    	private JFrame frame;
    	private Dimension dim = new Dimension(1400,500);
    	private Calendar now;
    	private JPanel panel;
    	private JLabel startLabel, stopLabel;
    	private BufferedReader in;
    	private File file = new File("C:\\ALARMS.txt");
    	private int month, day, hour, min, ampm;
    	private String alarmLines, alarmMonth, alarmDay, alarmHour, alarmMinute, alarmAMPM, alarmName, alarmFile;
    	private ArrayList<String> alarms = new ArrayList<String>();
    	private StringTokenizer st;
    	private boolean alarmsRunning;
    	
    	public Alarm2(){
    		setFrame();
    		setPanels();
    		loadAlarms();
    	}
    	
    	public static void main(String[] args){
    		new Alarm2();
    	}
    	
    	private void setFrame(){
    		frame = new JFrame();
    		frame.setMinimumSize(dim);
    		frame.setMaximumSize(dim);
    		frame.setPreferredSize(dim);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
    	}
    	
    	
    	private void setPanels(){
    		panel = new JPanel();
    		panel.setSize(ScheduleMain.dim);
    		panel.setBackground(new Color(0,0,75));
    		panel.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    		Insets i = new Insets(5,5,5,5);
    		
    		c.gridx = 0;
    		c.gridy = 2;
    		c.insets = i;
    		startLabel = new JLabel("START ALARMS");
    		startLabel.setFont(new Font("Arial", Font.BOLD, 60));
    		startLabel.setForeground(Color.WHITE);
    		startLabel.addMouseListener(new MouseAdapter(){
    			@Override
    			public void mouseEntered(MouseEvent e) {
    				startLabel.setForeground(Color.ORANGE);
    				super.mouseEntered(e);
    			}
    			
    			@Override
    			public void mouseExited(MouseEvent e) {
    				startLabel.setForeground(Color.WHITE);
    				super.mouseExited(e);
    			}
    			
    			@Override
    			public void mouseClicked(MouseEvent e) {
    				startLabel.setVisible(false);
    				
    				try {
    					Thread.sleep(200);
    				} catch (InterruptedException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}
    				
    				checkAlarms2();
    				
    				super.mouseClicked(e);
    			}
    			
    		});
    		panel.add(startLabel, c);
    		
    		frame.add(panel);
    		
    	}
    		
    	private void getNow(){
    		now = Calendar.getInstance();
    		month = now.get(Calendar.MONTH);
    		day = now.get(Calendar.DAY_OF_MONTH);
    		hour = now.get(Calendar.HOUR);
    		min = now.get(Calendar.MINUTE);
    		ampm = now.get(Calendar.AM_PM);
    	}
    	
    	private void loadAlarms(){
    		try {
    			in = new BufferedReader(new FileReader(file));
    			while((alarmLines = in.readLine()) != null){
    				alarms.add(alarmLines);
    			}
    			
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		};
    	}
    	
    		
    	private void checkAlarms2(){
    		alarmsRunning = true;
    		loadAlarms();
    		while(alarmsRunning){
    		getNow();
    		for(int i = 0; i < alarms.size(); i++){
    			System.out.println(alarms.get(i));
    			st = new StringTokenizer(alarms.get(i), ",");
    			alarmMonth = st.nextToken();
    			alarmDay = st.nextToken();
    			alarmHour = st.nextToken();
    			alarmMinute = st.nextToken();
    			alarmAMPM = st.nextToken();
    			alarmName = st.nextToken();
    			alarmFile = st.nextToken();
    			if(month+1 == Integer.parseInt(alarmMonth) || alarmMonth.equalsIgnoreCase("0")){
    				if(day == Integer.parseInt(alarmDay) || alarmDay.equalsIgnoreCase("0")){
    					if(hour == Integer.parseInt(alarmHour) || alarmHour.equalsIgnoreCase("0")){
    						if(min == Integer.parseInt(alarmMinute)){
    							if(ampm == Integer.parseInt(alarmAMPM)){
    								File file = new File(alarmFile);
    								try {
    									Desktop.getDesktop().open(file);
    									try {
    										Thread.sleep(60000);
    									} catch (InterruptedException e) {
    										// TODO Auto-generated catch block
    										e.printStackTrace();
    									}
    								} catch (IOException e) {
    									// TODO Auto-generated catch block
    									e.printStackTrace();
    									}
    								}
    							}
    						}
    					}
    				}
    			}
    		try {
    			Thread.sleep(1000);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    		}
    	}
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Not allowing me to close project

    Even when the thread it is sleeping, it is sleeping on the GUI thread (i.e. it is the GUI thread that is sleeping). Check out the API docs and tutorials for SwingWorker.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Not allowing me to close project

    Your code is executing on the Event Dispatch Thread (EDT) so the MouseEvents cannot be processed until all the code from your checkAlarms2() method finishes executing.

    Read the section from the Swing tutorial on Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing) for more information. Basically you should never invoke Thread.sleep on the EDT. You may want to use a Swing Timer to schedule the events for your alarms. The tutorial also has a section on "How to Use Timers".

  4. #4
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    107
    Rep Power
    0

    Default Re: Not allowing me to close project

    Java Code:
    package com.scheduler.ryan;
    
    import java.awt.Color;
    import java.awt.Desktop;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.StringTokenizer;
    
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    public class Alarm2 extends JComponent{
    
    	private JFrame frame;
    	private Dimension dim = new Dimension(1400,500);
    	private Calendar now;
    	private JPanel panel;
    	private JLabel startLabel, stopLabel;
    	private BufferedReader in;
    	private File file = new File("C:\\ALARMS.txt");
    	private int month, day, hour, min, ampm;
    	private String alarmLines, alarmMonth, alarmDay, alarmHour, alarmMinute, alarmAMPM, alarmName, alarmFile;
    	private ArrayList<String> alarms = new ArrayList<String>();
    	private StringTokenizer st;
    	private boolean alarmsRunning;
    	private Timer timer;
    	
    	public Alarm2(){
    		
    		setFrame();
    		setPanels();
    		loadAlarms();
    		ActionListener al = new ActionListener(){
    
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				checkAlarms2();
    			}
    			
    		};
    		timer = new Timer(1000, al);
    		
    	}
    	
    	public static void main(String[] args){
    		new Alarm2();
    	}
    	
    	private void setFrame(){
    		frame = new JFrame();
    		frame.setMinimumSize(dim);
    		frame.setMaximumSize(dim);
    		frame.setPreferredSize(dim);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
    	}
    	
    	
    	private void setPanels(){
    		panel = new JPanel();
    		panel.setSize(ScheduleMain.dim);
    		panel.setBackground(new Color(0,0,75));
    		panel.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    		Insets i = new Insets(5,5,5,5);
    		
    		c.gridx = 0;
    		c.gridy = 2;
    		c.insets = i;
    		startLabel = new JLabel("START ALARMS");
    		startLabel.setFont(new Font("Arial", Font.BOLD, 60));
    		startLabel.setForeground(Color.WHITE);
    		startLabel.addMouseListener(new MouseAdapter(){
    			@Override
    			public void mouseEntered(MouseEvent e) {
    				startLabel.setForeground(Color.ORANGE);
    				super.mouseEntered(e);
    			}
    			
    			@Override
    			public void mouseExited(MouseEvent e) {
    				startLabel.setForeground(Color.WHITE);
    				super.mouseExited(e);
    			}
    			
    			@Override
    			public void mouseClicked(MouseEvent e) {
    				timer.start();
    				super.mouseClicked(e);
    			}
    			
    		});
    		panel.add(startLabel, c);
    		
    		frame.add(panel);
    		
    	}
    		
    	private void getNow(){
    		now = Calendar.getInstance();
    		month = now.get(Calendar.MONTH);
    		day = now.get(Calendar.DAY_OF_MONTH);
    		hour = now.get(Calendar.HOUR);
    		min = now.get(Calendar.MINUTE);
    		ampm = now.get(Calendar.AM_PM);
    	}
    	
    	private void loadAlarms(){
    		try {
    			in = new BufferedReader(new FileReader(file));
    			while((alarmLines = in.readLine()) != null){
    				alarms.add(alarmLines);
    			}
    			
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		};
    	}
    	
    		
    	private void checkAlarms2(){
    		loadAlarms();
    		getNow();
    		for(int i = 0; i < alarms.size(); i++){
    			System.out.println(alarms.get(i));
    			st = new StringTokenizer(alarms.get(i), ",");
    			alarmMonth = st.nextToken();
    			alarmDay = st.nextToken();
    			alarmHour = st.nextToken();
    			alarmMinute = st.nextToken();
    			alarmAMPM = st.nextToken();
    			alarmName = st.nextToken();
    			alarmFile = st.nextToken();
    			if(month+1 == Integer.parseInt(alarmMonth) || alarmMonth.equalsIgnoreCase("0")){
    				if(day == Integer.parseInt(alarmDay) || alarmDay.equalsIgnoreCase("0")){
    					if(hour == Integer.parseInt(alarmHour) || alarmHour.equalsIgnoreCase("0")){
    						if(min == Integer.parseInt(alarmMinute)){
    							if(ampm == Integer.parseInt(alarmAMPM)){
    								File file = new File(alarmFile);
    								try {
    									Desktop.getDesktop().open(file);
    									
    								} catch (IOException e) {
    									// TODO Auto-generated catch block
    									e.printStackTrace();
    								}
    							}
    						}
    					}
    				}
    			}
    		}		
    	}
    }
    Thank you both. It took a little bit of understanding, but hey, I got it working. Thank you so much.

Similar Threads

  1. Replies: 6
    Last Post: 07-03-2012, 04:09 AM
  2. Allowing only hex values in a textfield
    By elsanthosh in forum AWT / Swing
    Replies: 3
    Last Post: 07-20-2010, 09:31 AM
  3. Replies: 13
    Last Post: 04-09-2010, 11:15 AM
  4. Help with java project. Im so close!
    By DanK in forum New To Java
    Replies: 2
    Last Post: 02-28-2009, 01:03 AM

Posting Permissions

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