Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Java Concurrency

  1. #1
    Member
    Join Date
    Apr 2011
    Location
    Niperia
    Posts
    30
    My Mood
    Amazed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Unhappy Java Concurrency

    Hello Guys! I want to display a JFrame during execution of a timer, so far i've tried but failed. The following code is what i have done so far, dont know if am doing the right thing:

     
    /**
     * 
     */
    package logRemainder;
     
    import javax.swing.JOptionPane;
     
    import java.awt.Toolkit;
    import java.util.Date;
    import java.util.Calendar;
    import java.util.Timer;
    import java.util.TimerTask;
     
    import gui.CreateLogRemainder;
    /**
     * @author aknessy
     *
     */
    public class RemainderBeep{
    	private Timer timer;
    	private Toolkit toolkit;
    	private int time;
    	private static int numOfBeeps = 10;
     
    		public void Beep(){
    			timer = new Timer();
    			toolkit = Toolkit.getDefaultToolkit();
    			Calendar calendar = Calendar.getInstance();
    			String getCloseTime = JOptionPane.showInputDialog(null, "<html><body><font color='green'>"
    					+ "Please Enter Today's Closing Time: </font><br>"
    					+ "<font color='red'><p align='center'> 1 - 24HRS!</p></font>");
     
    			time = Integer.parseInt(getCloseTime);
     
    			calendar.set(Calendar.HOUR_OF_DAY, time);
    			calendar.set(Calendar.MINUTE, 20);
    			calendar.set(Calendar.SECOND, 0);
    			Date time = calendar.getTime();
     
    			timer.schedule(new RemainderTask(), time, 1*2000);
     
    		}
     
    		class RemainderTask extends TimerTask{
    			public void run(){
     
    				if(numOfBeeps > 0){
    					toolkit.beep();
     
    					 Thread td = new Thread(){
    						 public void run(){
    							 new gui.CreateLogRemainder();
    						 }
    					 };
    					 	td.start();
     
    			numOfBeeps--;	
    			}else{
    				toolkit.beep();
    				timer.cancel();
    			}
    		}
    	}
     
    		public static void main (String[] args){
    			new RemainderBeep().Beep();
    		}
    }

    I tried executing the programming without creating a new thread, but it didn't work! I have tried the same program with the Swing's Timer class and without a thread, the new Frame is displayed but the Toolkit does not beep.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Concurrency

    I don't see a JFrame and it's disappointing that the code you posted doesn't have a single comment. What's it supposed to do?

  3. #3
    Member
    Join Date
    Apr 2011
    Location
    Niperia
    Posts
    30
    My Mood
    Amazed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Java Concurrency

    That's true @GregBrannon., I apologize for the error! There's a Call to CreateLogRemainder Class within the run() method of the Thread, it contains the JFrame that I want to display while the Timer is active and while the Toolkit is beeping! That's basically what I want to achieve!

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Concurrency

    Oh. That's fairly important info to include.

    Correct me if I'm wrong: You want to display a JFrame for either a total amount of time set by the user or starting at a certain time and ending at a specified later time, beeping every 2 seconds while the JFrame is displayed. Is that right? If not, please restate.

  5. #5
    Member
    Join Date
    Apr 2011
    Location
    Niperia
    Posts
    30
    My Mood
    Amazed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Java Concurrency

    Exactly! I want the JFrame to start at the exact same time as the Timer and ending at the same time.

  6. #6
    Member
    Join Date
    Apr 2011
    Location
    Niperia
    Posts
    30
    My Mood
    Amazed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Java Concurrency

    @GregBrannon whats up Elite? Been expecting your reply since your last post! can see you are in a relax mood.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Concurrency

    I work. Weekends allow more time to consider the possiblities and respond; weekdays are limited to simpler, shorter problems. In this case, you haven't given us much to work with.

    Try writing a simple program that opens a JFrame when the timer starts and closes it when it ends. I'd probably use a JDialog instead of a JFrame to control modality, but since I don't have a complete picture of what you're doing, the other GUI elements involved, etc., so I'm not sure what's best. It doesn't seem too complicated, but I haven't worked it through.

  8. #8
    Member
    Join Date
    Apr 2011
    Location
    Niperia
    Posts
    30
    My Mood
    Amazed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Java Concurrency

    Okay.. No problems! I just nid to figure it out, thanks though!

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Concurrency

    Here's a simple demo of what I was thinking you could do. Let me know if you have any questions.

    Edit: Modified to actually show a JDialog to ensure that the javax.swing.Timer exists long enough to complete the demonstration.

    import java.awt.Dialog;
    import java.awt.Dimension;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    import javax.swing.JDialog;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
     
    // class TimerDemo demonstrates how a javax.swing.Timer could be
    // used to perform any action after a set amount of time
    public class TimerDemo implements ActionListener
    {
        Timer demoTimer;
        JDialog dialog;
     
        // a constructor that could be used to set the parameters
        // of the timer, especially the duration of the desired action
        public TimerDemo()
        {
            // define the timer with an initial delay and time between
            // events of 5,000 ms 
            demoTimer = new Timer( 5000, this );
     
            // create a JDialog to show while the timer is running
            dialog = new JDialog();
            dialog.setModalityType( Dialog.ModalityType.MODELESS );
            dialog.setSize( new Dimension( 400, 400 ) );
            dialog.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
            dialog.setLocationRelativeTo( null );
            dialog.setVisible( true );
     
            // simulate opening the JDialog here (start the desired action)
            System.out.println( "Opening the JDialog . . ." );
     
            // start the timer that will delay 5 seconds and then fire
            // the event that triggers the action listener
            demoTimer.start();
     
        } // end constructor
     
        // a standard main() method to launch the swing app on the EDT
        public static void main( String[] args )
        {
            SwingUtilities.invokeLater( new Runnable()
            {
                @Override
                public void run()
                {
                    new TimerDemo();
                }
            } );
     
        } // end method main()
     
        // method actionPerformed() is run when the action listener event
        // is triggered by the timer to simulate closing the JDialog or 
        // ending the desired event after the desired amount of time
        @Override
        public void actionPerformed( ActionEvent ae )
        {
            // simulate closing the JDialog (end the desired action)
            System.out.println( "The JDialog is closing" );
     
            // then stop the timer so the action doesn't repeat
            demoTimer.stop();
     
            // close the dialog
            dialog.dispose();
     
        } // end method actionPerformed()
     
    } // end class TimerDemo

  10. #10
    Member
    Join Date
    Apr 2011
    Location
    Niperia
    Posts
    30
    My Mood
    Amazed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Java Concurrency

    Thanks man! Your response alone is really a psychological boost! Thanks again!!

  11. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Concurrency

    Your response alone is really a psychological boost!
    I don't get that too often, unless you're standing on a ledge somewhere, contemplating stepping off.

    If not (and I hope not), glad that I boosted you.

  12. #12
    Member
    Join Date
    Apr 2011
    Location
    Niperia
    Posts
    30
    My Mood
    Amazed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Java Concurrency

    Working for weeks to complete a project out weighs that... It's infact
    "Stepping off the edge"! Thanks really, now I can go figure everything! Except I run into another "cyclope"!

  13. #13
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Concurrency

    Except I run into another "cyclope"!
    Not sure what that means. When you can explain it and need more help, ask. Post your latest code when you do.

    Edit: I updated my demo code. I realized that without a Swing component visible, the behavior or lifespan of the javax.swing.Timer is unpredictable.

  14. #14
    Member
    Join Date
    Apr 2011
    Location
    Niperia
    Posts
    30
    My Mood
    Amazed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Java Concurrency

    Hi @GregBrannon Its been awhile! You requested I post the latest code; fortunately i've solved "The Problem". It turned out that the designer (JiGloo) i was working with had a serious beef with me! So here it is:

     
    /**
     * 
     */
    package logRemainder;
     
    import javax.swing.JOptionPane;
     
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Date;
    import java.util.Calendar;
    import java.util.Timer;
    import java.util.TimerTask;
    import gui.CreateLogRemainder;
     
    /**
     * @author aknessy
     *
     */
    public class RemainderBeep{
    	private Timer timer;
    	private Toolkit toolkit;
    	private int time;
    	private static int numOfBeeps = 10;
    	private javax.swing.Timer swingTimer; 
    	private CreateLogRemainder fm = new CreateLogRemainder();
     
    		public void Beep(){
    			timer = new Timer();
    			toolkit = Toolkit.getDefaultToolkit();
    			Calendar calendar = Calendar.getInstance();
     
    			/**
    			 * Get The Time...
    			 */
     
    			String getCloseTime = JOptionPane.showInputDialog(null, "<html><body><font color='green'>"                                                                                                                                                                                                                                                                                                      
    					+ "Please Enter Today's Closing Time: </font><br>"
    					+ "<font color='red'><p align='center'> 1 - 24HRS!</p></font>");
    			time = Integer.parseInt(getCloseTime);
     
    			/** 
    			 * Use The collected time with a Calendar Instance
    			 */
     
    			calendar.set(Calendar.HOUR_OF_DAY, time);
    			calendar.set(Calendar.MINUTE, 0);
    			calendar.set(Calendar.SECOND, 0);
    			Date time = calendar.getTime();
     
    			timer.schedule(new RemainderTask(), time, 1*2000);
     
    			/**
    			 * A Swing Timer To Display
    			 * The MessagePanel
    			 */
     
    			swingTimer = new javax.swing.Timer(1000, new swingTimerListener());
    			swingTimer.setDelay(40);
    			swingTimer.setCoalesce(true);
    			swingTimer.setRepeats(false);
    			swingTimer.start();
     
    		}
     
    		/**
    		 * This Remainder Class counts down to zero,
    		 * and sets a Toolkit object to beep during
    		 * this count-down.
    		 * 
    		 *  If the count-down is equal to zero,
    		 *  stop the java.util.Timer, stop the swing timer
    		 *  and dispose the MessagePanel.
    		 */
     
    		class RemainderTask extends TimerTask{
    			public void run(){
     
    				if(numOfBeeps > 0){
    					toolkit.beep();
     
    			numOfBeeps--;	
    			}else{
    				toolkit.beep();
    				timer.cancel();
    				swingTimer.stop();
    				fm.dispose();
    			}
    		}
    	}
     
    		public static void main (String[] args){
    			new RemainderBeep().Beep();
    		}
     
    		class swingTimerListener implements ActionListener{
    			public void actionPerformed(ActionEvent ae){
    				fm.TestFadeLabel();
    			}
    		}
    }

Similar Threads

  1. Concurrency Programming GURUS: Help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    By Novice85 in forum Object Oriented Programming
    Replies: 7
    Last Post: September 29th, 2011, 05:31 PM
  2. java concurrency problem
    By Nick7 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 1st, 2011, 06:02 AM