How can I make my Java program pause until a specific button has been pressed?
-
I am currently writing a game similar to brick breaker and need to know how to make my program wait for a specific function to be called. I need to pause the program until a decision has been made between buttons. For example, the game starts up, and the user is given a choice of which colors he wants in his game. Thus, the game pauses until his selection has been made and one he is happy then the program needs to resume to a different stage.
-
Answer:
If you're using Swing components, you can add an http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html to the button and include the logic that needs to be executed within the overridden http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html#actionPerformed%28java.awt.event.ActionEvent%29 method. See also http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html. Edit I need to pause the program until a decision has been made between buttons. For example, the game starts up, and the user is given a choice of which colors he wants in his game. Thus, the game pauses until his selection has been made and one he is happy then the program needs to resume to a different stage. There are several ways of going about this, but I'm not about to delve into the intricacies of concurrent programming here; you can find other resources for that. That being said, one comment I would like to make is that you should neither execute long-running tasks, nor sleep in the Event Dispatch Thread (EDT) because doing so will cause the UI to become unresponsive (i.e. "freeze").
Chris at Stack Overflow Visit the source
Other answers
I need to pause the program.. For games such as brick-breaker, you would normally have an animation loop. On button press, act as advised by user132.. As to what to do when that happens, it depends on how the code is organized to loop in the first place. E.G. The Swing http://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html, this is an easy way to loop & pause rendering in a GUI - it offers methods like http://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html#start%28%29 & http://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html#stop%28%29. If you had multiple timers running (e..g one for the rendering loop, another for a 'count-down timer' for the level) you'd obviously need to pause them all. More details might be forthcoming with an http://sscce.org/ of your current code.
Andrew Thompson
You are in the area of GUI-programming which is basically using events (event driven) In some way you asked the wrong question. In an event driving system you are always waiting. You continue with work when some event happens. In your case you should think what to do when the button is pressed.
stefan bachert
Here is a basic outline of the kind of structure you will need. import java.swing.* public class Breaker { private Listener listener; JButton b1; public Breaker() { listener = new Listener(); b1 = new JButton("b1"); b1.addActionListener(listener); } class Listener implements ActionListener { public void actionPerformed(ActionEvent e) Component theEventer = (Component) e.getSource(); if (theEventer == b1) { You're stuff here } } public static void main (String[] args) { new Breaker(); } }
Mike
Related Q & A:
- How can I make a Spinner only by java?Best solution by stackoverflow.com
- I have a blog how can I make money with it.Best solution by Yahoo! Answers
- How can I make a myspace comment into a picture caption?Best solution by Yahoo! Answers
- How can I make a hole through a stone or crystal?Best solution by Yahoo! Answers
- How can I make JAXB-generated classes participate in a visitor pattern?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.