Results 1 to 5 of 5
  1. #1
    zhangster is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default is there a way to pause java?

    so i am making a program with a GUI. I need the user to enter some data before the rest of my program is run. So, i was wondering if there was some way to pause the program until the user clicks my button (my GUI button).

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    The usual way to make GUIs is to make them event driven, i.e. do something based on an event that happens, such as pressing a button or entering text, so you don't pause the program, but leave the program idle until something happens. See a few tutorials about using ActionListeners, things should be clearer then.

  3. #3
    zhangster is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default

    yea that is what i want to do. i want to leave the program idle. any ideas how to do it?

    Java Code:
    	class enterp implements ActionListener
    		{
    			public void actionPerformed(ActionEvent enterpressed)
    			{
    				gues = guess.getText();
    				System.out.println(gues);
    			}
    		}
    		enterp entered = new enterp();
    		guess.addActionListener(entered);
    		
    		try {
    		Thread.sleep(6000);
    		} catch(InterruptedException e) {
    		} 
    		double answer = Double.parseDouble(gues);
    		System.out.println(answer);
    in my code, while waiting for the user to press enter, the program runs the
    Java Code:
    double answer = Double.parseDouble(gues);
    		System.out.println(answer);
    this results in an error since gues is a null...any ideas on how to not run that 2 lines of code before the user presses enter?

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You would do that by calling the method you want to run from inside the actionPerformed() method. So, while there is no input, the program does nothing, when you press the button, you execute the piece of code you want to.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Agrees with moon. What you want to achieve is already done for you with the Swing event-driven programming model. The last thing that you want to do is to call Thread.sleep(...) inside of your GUI code.

Similar Threads

  1. How to pause
    By ravjot28 in forum Threads and Synchronization
    Replies: 0
    Last Post: 03-31-2010, 07:09 AM
  2. Make Java Pause
    By FlyNn in forum New To Java
    Replies: 2
    Last Post: 02-10-2010, 11:18 AM
  3. Pause and Continue Long Cycles
    By pedro123 in forum Threads and Synchronization
    Replies: 2
    Last Post: 07-28-2009, 08:47 AM
  4. pause until JFrame is closed.
    By Tamu in forum Advanced Java
    Replies: 8
    Last Post: 11-30-2008, 10:46 PM
  5. Help with Pause
    By trill in forum Java Applets
    Replies: 2
    Last Post: 09-28-2007, 09:50 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
  •