Results 1 to 4 of 4

Thread: Event Handling

  1. #1
    sedj601 is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Event Handling

    I am a year in with Java and I think I am an OK Java programmer, but I have been trying to learn GUI and Event Handling for an Internship I have coming up soon and I am having a problem mastering the event handling part. I have found plenty of YouTube videos that are great help and I follow them pretty close but my program will not work for some reason. The program I am creating should work like a chess clock. So once one button is clicked on the other buttons text should start counting down but I can't seem to get it to work. I have tried JTextField, JButton, and now JToggleButton. Any help would be greatly appreciated.

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package guichessclock1;
    
    /**
     *
     * @author sedrick
     */
    public class Clock 
    {
        int hours, minutes, seconds;
        
        Clock()
        {
            hours = 0;
            minutes = 0;
            seconds = 0;
        }
        
        Clock(int hour, int mins, int sec)
        {
            hours = hour;
            minutes = mins;
            seconds = sec;
        } 
        
        void runHour()
        {
            if(hours > 0)
            {
                hours = hours - 1;
            }
            else 
            {
                hours = 0;
            }      
        }
        
        void runMinutes()
        {
            if(minutes > 0)
            {
                minutes = minutes -1;
            }
            else
            {
                minutes = 0;
                if(hours > 0)
                {
                    runHour();
                    minutes = 59;
                }     
            }
        }  
    
        void runSeconds()
        {
            if(seconds > 0)
            {
                seconds = seconds - 1;
            }
            else
            {
                if(minutes >= 0 && hours > 0)
                {
                    runMinutes();
                    seconds = 59;
                }
                else if(minutes > 0)
                {
                    runMinutes();
                    seconds = 59;
                }
                else if(hours == 0 && minutes == 0 && seconds == 0)
                {
                   System.exit(1);
                }            
            }
        }
        
         int getHour()
        {
            return hours;
        }
        
        int getMinutes()
        {
            return minutes;
        }
        
        int getSeconds()
        {
            return seconds;
        }
        
    }

    Java Code:
    package guichessclock1;
    
    import java.awt.BorderLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JToggleButton;
    import java.util.Scanner;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JTextField;
    /**
     *
     * @author sedrick
     */
    public class GUIComponents extends JFrame 
    {
        private  JToggleButton[] items;
        private String names[] ;
        private BorderLayout layout;
        private Clock whiteClock;
        private Clock blackClock;
        private String turn;
        
        
        Scanner input = new Scanner(System.in);
        
        GUIComponents()
        {
            super("Chess Clock");
            
            layout = new BorderLayout(5,5);//horzontal and vertical gap between buttons
            setLayout(layout);//set frame layout
            names = new String[3];
             items = new JToggleButton[names.length];//set size of array
            
            
            //create JButtons and register listeners for them
            thehandler handler = new thehandler();
            for(int i = 0; i < names.length; i++)
            {
                items[i] = new JToggleButton(names[i]);
                items[i].setVisible(true);
                items[i].addActionListener(handler);    
               // items[i].setEditable(false);
            }
            
            add(items[0], BorderLayout.EAST);
            
            add(items[2], BorderLayout.CENTER);
            add(items[1], BorderLayout.WEST);     
            
            
            System.out.print("Please enter hours: ");
            int hours = input.nextInt();
            System.out.print("\n Please enter minutes: ");
            int minutes = input.nextInt();
            System.out.print("\n Please enter seconds: ");
            int seconds = input.nextInt();
            
            
            whiteClock = new Clock(hours, minutes, seconds);
            blackClock = new Clock(hours, minutes, seconds);
            
            items[2].setText("Pause The Game");
            items[1].setText(whiteClock.getHour() + ":" + whiteClock.getMinutes() + ":" + whiteClock.getSeconds());
            items[0].setText(blackClock.getHour() + ":" + blackClock.getMinutes() + ":" + blackClock.getSeconds());
        }
        
        private class thehandler implements ActionListener
        {
           
            
            @Override
            public void actionPerformed(ActionEvent event)
            {
                int control;
                if(event.getSource() == items[0])
                {
                    control = 1;
                    while(control == 1)
                    {
                        System.out.println(whiteClock.getHour() + ":" + whiteClock.getMinutes() + ":" + whiteClock.getSeconds());
                        items[1].setText(whiteClock.getHour() + ":" + whiteClock.getMinutes() + ":" + whiteClock.getSeconds());
                        whiteClock.runSeconds();
                        try 
                        {
                            Thread.sleep(1000);
                        } 
                        catch (InterruptedException ex) 
                        {
                            Logger.getLogger(GUIComponents.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
                else if(event.getSource() == items[1])
                {
                    control = 2;
                    while(control == 2)
                    {
                        System.out.println(blackClock.getHour() + ":" + blackClock.getMinutes() + ":" + blackClock.getSeconds());
                        items[0].setText(blackClock.getHour() + ":" + blackClock.getMinutes() + ":" + blackClock.getSeconds());
                        blackClock.runSeconds();
                        try 
                        {
                            Thread.sleep(1000);
                        } 
                        catch (InterruptedException ex) 
                        {
                            Logger.getLogger(GUIComponents.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                    
                }
                else if(event.getSource() == items[2])
                {
                    control = 3;
                }
            }
    
            
        }
        
    }



    Java Code:
    package guichessclock1;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    /**
     *
     * @author sedrick
     */
    public class GUIChessClock1 
    {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws InterruptedException 
        {
            
            
            
           
            
           GUIComponents GUI = new GUIComponents();
           GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           GUI.setSize(600, 400);
           GUI.setVisible(true);
           
            
        }
        
        
    }

  2. #2
    sedj601 is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Event Handling

    I am going to create new getHour(), getSecond(), and getMinute() methods that have a return type of String.

  3. #3
    sedj601 is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Event Handling

    Still did not work properly

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: Event Handling

    First, events are processed on the Event Dispatch Thread. And anything running on the EDT should run quickly so other events can be processed quickly. Therein lies the problem. Here is part of you code:

    Java Code:
                    control = 1; 
                    while(control == 1)
                    {
                        System.out.println(whiteClock.getHour() + ":" + whiteClock.getMinutes() + ":" + whiteClock.getSeconds());
                        items[1].setText(whiteClock.getHour() + ":" + whiteClock.getMinutes() + ":" + whiteClock.getSeconds());
                        whiteClock.runSeconds();
                        try
                        {
                            Thread.sleep(1000);
                        }
                        catch (InterruptedException ex)
                        {
                            Logger.getLogger(GUIComponents.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
    You remain in a while loop as long as control == 1. But how is control set to anything else? No other events can be processed while you are in the actionPerformed method. So you need to remove all but the bare necessities out of the listener and off of the EDT.

    I suggest you stay away from YouTube videos for programming and instead read the tutorials. Especially the one mentioned in my signature. It does an in depth covering of event handling.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. JSP Event Handling!!!
    By hassaanid2012 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 06-22-2012, 04:25 PM
  2. Event handling
    By Suresh karthi in forum New To Java
    Replies: 1
    Last Post: 05-03-2012, 03:43 PM
  3. Event handling
    By smallmos1 in forum New To Java
    Replies: 7
    Last Post: 11-24-2010, 09:22 PM
  4. Event handling... help?
    By kevzspeare in forum New To Java
    Replies: 2
    Last Post: 04-04-2009, 09:46 PM
  5. Event Handling
    By luisarca in forum Sun Java Wireless Toolkit
    Replies: 5
    Last Post: 05-07-2007, 07:05 PM

Posting Permissions

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