How to release a pressed java button - java

I have java method that executes a very long model. It takes about 20 minutes. When I press the button, the button remains pressed until the model is finished. I need to create a button that when is pressed, it stop the model execution
So far I have 2 buttons: Stop and Start. My problem is that when I press the start button, all the buttons from the GUI are freeze and they cannot be pressed until the model finishes
How can I create a stop button?
Thank you,
Ionut

You should use a SwingWorker. They are specifically designed for performing long running tasks off of the EDT while still being able to update the GUI with progress reports.

It is best to start a Thread once the button is clicked. You could make a stop button that stops the thread if it has not finished yet. For this you will have to keep track of the Thread though (for example in a static variable).
A quick guide to threading: http://www.tutorialspoint.com/java/java_multithreading.htm
Please take a look at this as well: How to properly stop the Thread in Java?.

If you are executing a 20 minute task in response to a button click you should probably be doing it in a background thread.

You have to use multi-threading. A basic example-
public class ThreadTest {
private int getSum(int x, int y){
return x + y;
}
public static void main(String args[]) {
ThreadTest tTest = new ThreadTest();
System.out.println(tTest.getSum(2, 5));
// Start the thread
new LongProcess().start();
// Since the thread sleeps for two seconds
// You will see 5 + 5 = 10 before Done! gets printed out
System.out.println(tTest.getSum(5, 5));
}
}
class LongProcess extends Thread {
public LongProcess(){
System.out.println("Thread started..");
}
public void run() {
try{
Thread.sleep(2000);
System.out.println("Done!");
} catch(InterruptedException iEx){
System.out.println(iEx.toString());
}
}
}

Related

JProgressBar moves instantly and not gradually (java)

I want to make a ProgressBar move gradually using a Jbutton. To achieve this I am using a for loop and the method Thread.sleep. The problem is that insted of moving a tiny bit every second (the progress bar) after pressing the button, the program waits until the loop finishes and then does instantly move the progress up. When I take the loop outside of the button listener it works as I want but I really need it to work when pressing the button. Here is the code:
progressBar.setOrientation(SwingConstants.VERTICAL);
progressBar.setMinimum(0);
progressBar.setMaximum(100);
progressBar.setValue(50);
panel1.setLayout(null);
panel1.add(progressBar);
progressBar.setBounds(40,6,100,100);
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int counter = 5;
for (int i = 0; i < 5; i++) {
progressBar.setValue(progressBar.getValue() + counter);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
}
});
If anyone can help me I will be very grateful!
Your code runs on the Event Dispatcher Thread (EDT). That thread is responsible for handling events, but also for repainting. Because it's just one thread, the repainting only occurs after your method ends.
There are some ways to solve this. In your case, a javax.swing.Timer would probably be easiest. Instead of running the loop in a single method, the button click starts a timer that runs every second. When it's done the timer can cancel itself. A slightly more difficult alternative is to use a SwingWorker. You could publish data in the doInBackGround method, and override process to perform the updates to the progress bar.
For more information, please read Concurrency in Swing.

How do you repaint a container immediately as components are added in a separate thread in JAVA?

I am using java.
I have a click event that adds "squares" to a container, in a loop. I want each square to show up RIGHT when it is added. I tried running the 'adding of the squares' in a separate thread, but it is not working.
Here is some code I use for 'public class GuiController implements ActionListener, MouseListener':
#Override
public void mouseClicked(MouseEvent e)
{
//createBoardPane();
new Thread
(
new Runnable()
{
public void run()
{
showAnimation();
}
}
).start();
}
public void showAnimation()
{
for(int i = 0; i < model.getAnimationList().size(); i++)
{
String coord = model.getAnimationList().get(i);
int x = Integer.parseInt(coord.substring(0, coord.indexOf(',')));
int y = Integer.parseInt(coord.substring(coord.indexOf(',') + 1, coord.length() - 2));
boolean shouldPlacePiece = (coord.charAt(coord.length() - 1) == 'p');
if(shouldPlacePiece)
{
model.getView().getBoardPane().getComponent(x + (y * model.getBoardSize())).setBackground(Color.BLACK);
}
else
{
model.getView().getBoardPane().getComponent(x + (y * model.getBoardSize())).setBackground(Color.WHITE);
}
model.getView().getBoardPane().repaint();
long time = System.currentTimeMillis();
while((System.currentTimeMillis() - time) < 250)
{
// wait loop
}
}
}
any help is appreciated!
Creating a separate Thread to run for this longish-running task was an excellent idea - unless you want to lock-up interactions with your GUI while doing your animation.
Now, Swing GUI objects are not Thread safe (with few exceptions), so you cannot work with them from a thread other than Swing's Event Dispatch Loop's thread. So take all the GUI update code in your for-loop, and wrap it with a new Runnable (yes, another one).
Then call SwingUtilities.invokeLater(Runnable doRun) with that Runnable on each iteration of the loop.
Your GUI update code will then be scheduled to run ASAP on the Event Dispatch Loop, which will occur while your worker thread goes to sleep (do you have anything against Thread.sleep?).
Alternative: Use SwingWorker instead of Thread
SwingWorker will create and manage a new the Thread for you, and publish data that it (SwingWorker) will cause to be run on the Event Dispatch Loop's thread. You'll override doInBackground with your code. Call publish with parameters to push across into the Event Dispatch Thread. Override process with code to process those parameters and update your GUI.
On gotcha with SwingWorker is that it accumulates published events over a period of about 33 milliseconds. If you're publishing more frequent than that you may get all your events bunched together every 33 milliseconds or so. In you case, 250 milliseconds between updates shouldn't be a problem.

Stopping a for loop with a button

I am trying to stop a for loop that is initiated by pressing a button. The only problem I have found is that the application is literally non-responsive once the start button is pressed. How would I go about making the stop button? At the moment the only way I can stop the application outside of my IDE is to go into task manager and forcibly delete it.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String L = "Hello";
int Num = Integer.parseInt(m1.getText());
int Num2 = Integer.parseInt(m2.getText());
nu = Num;
for (int kk = nu; nu > 0; nu--) {
if (O1.isSelected()) {
for (int num3 = nu; nu > 0; nu--) {
try {
try {
Thread.sleep(Num2 * 1000);
} catch (InterruptedException ex) {}
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_F);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
}
This is the code I have, I have been looking around and I think I need to use the SwingWorker class. I am not sure how to implement it though.
Have you put your loop inside the button action handler?
If so remove it and put it in a thread. Else it will block the EDT and app will become unresponsive. Use buttons only to trigger start and stop.
Without code it is hard to determine, but you are probably performing the loop on AWT thread, hence your UI is blocked and you can not press a button.
You should move your endless loop to another Thread, and then the button will work
You are entering in an infinite loop any how. I would suggest to put your loop in a separate thread and when you want to stop it just interrupt that thread on button click.
Move the code into a dedicated thread and use a variable check to end the loop externally.
The first problem is that you're performing this loop inside the Event Dispatch Thread (EDT). This is a thread which is dedicated to handling the user interface. Since you are looping and sleeping in this thread, it is unavailable to handle any other user interactions. So effectively you are locking yourself out of your own program.
The proper way to shut this down is to create a shutdown() method on the loop which will modify a variable that is checked in each iteration of the loop. Then, when you want to shut it down, you just call this method on the Runnable and the loop shuts down.
public class KeyPressRunnable implements Runnable {
private boolean isRunning = true;
public void shutdown() {
this.isRunning = false;
}
#Override
public void run() {
// put your loop in here, and include this code in the loop:
if (!this.isRunning) break;
}
}

Recurring Countdown Timer in Java

I'm trying to implement a countdown timer into a pre-existing public class and I have a few questions.
An overview: I want to have a timer within a program that counts down from 60 (seconds) once the program is initialized.
If the timer reaches zero, the program quits.
If the user meets certain parameters within the 60 second time frame, the timer resets to 60, presents a new set of parameters, and begins the countdown again. It should be able to do this an infinite number of times, until the user fails to meet parameters within 60 seconds.
There will also be some sort of (TBD) GUI representation of the timer, most likely either numerical countdown or JProgressBar.
I'm semi-new (~3 months) to programming, self-taught, and still learning lots (so be gentle) :)
My questions are:
What is the best way to implement this?
I'm assuming this needs to run in a thread?
Will the timer be easily configurable? (not important, just interesting)
Thanks for your help. If you need to see code, I can find some.
EDIT: Just for some clarification/context:
This is for a timed racing video game I'm working on to develop my skills as a programmer. The idea is that a player has 60 seconds to complete a lap. If the player completes a successful lap, the timer resets to 60 seconds and the track changes to be slightly more difficult. The game runs until the player is unable to complete a lap in 60 seconds due to the difficulty. The game records the number of laps as a high score, and asks to player if they would like to try again.
If I were you, I'd use:
an AtomicInteger variable which would keep the current countdown value;
a timer thread that would wake up every 1s and decrementAndGet() the variable, comparing the result to zero and terminating the app if the result is zero;
(possibly) a thread that would also wake up every 1s to repaint the GUI -- the best approach here depends on your GUI framework.
Finally, whenever you need to reset the count back to 60s, you just call set(newValue) from any thread.
The timer thread's run() method could be as simple as:
for (;;) {
if (counter.decrementAndGet() <= 0) {
// TODO: exit the app
}
Thread.sleep(1000);
}
I think it's much easier to get this right than trying to manage multiple Timer objects.
The best way to impliment timer in your application is using some sheduler frameworks like Quartz
You could use java.util.Timer to schedule an execution of a method and then cancel it if the requirements is met.
Like this:
timer = new Timer();
timer.schedule(new Task(), 60 * 1000);
And then make a class like this to handle the timerschedule:
class Task extends TimerTask {
public void run() {
System.exit(0);
}
}
If the requirements is met, then do this to stop it from executing:
timer.cancel();
If you need to update your GUI better to use SwingWorker http://en.wikipedia.org/wiki/SwingWorker
I would write something like this:
SwingWorker<String, Integer> timer = new SwingWorker<String, Integer>() {
Integer timer=60;
#Override
protected String doInBackground() throws Exception {
//update guiModel
//label.setText(timer.toString());
while(timer>0){
Thread.sleep(1000);
timer--;
}
return null;
}
#Override
public void done(){
System.exit(0);
}
};
JButton restart = new JButton(){
{
addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
timer.cancel(true);
timer.execute();
}
});
}
};

Timer Help:Working on a project for my AP Computer Science class

In our class we are making a game. The user has to Guess words and stuff. I don't think info about the game is needed to answer my question/problem.
Ok so what I am trying to do is to give the user a time limit in which they have to guess the word. Something like 15 seconds. If the user does not guess the word in 15 seconds they lose a turn.
Problems:
We didn't learn how to use timers. I experiment with timers and stuff. I can get a timer to count down from 15.
I can't check the current time while waiting for the user to input a guess.
I don't know how to bypass Stdin.readString() and make the program check the time.
Thanks.
Well, you can use the Scanner class to gather input from the user.
You may want to avoid timers if you don't know what threading is yet, but if you do want to try, you might be interested looking into the TimerTask & Timer classes.
While you may already know, you can get time from the System class, like currentTimeMillis
You have a few options. As you said, your program is waiting for input and hence that thread is busy. What you can do is create a separate thread, pass your timer to that thread and have it check the timer. Perhaps something like the following:
public class TimerChecker implements Runnable {
private Timer timer;
public TimerChecker(Timer timer) { this.timer = timer; }
#Override
public void run() {
// implement logic here
}
}
Which you can have invoked in a new thread using:
Timer timer = ...
new Thread(new TimerChecker(timer)).start();
// Now you are free to perform your blocking operation in the current thread
Stdin.readString();
One way to do this is, well to a run a separate thread object for the timer... this thread shall handle the updating of the time and would then trigger a certain event when the time of the player runs out...
or more like, implementing a counter in a separate thread whose increments are triggered by time-step, in this case, in seconds, you can do this by calling sleep()..
the timer thread object shall maintain a variable which keeps track of the current time..
on the main method of your program, you shall continue to check the value of this variable, as a pre-condition of your main loop perhaps,
the idea is there i think, just a thought!
pseudocode
class Timer extends Thread{
int current_time = 0;
public void run(){
sleep(1000);
current_time += 1;
}
public void synchronized getCurTime(){
return current_time;
}
}
class Game{
public Game(){
Timer timer = new Timer();
timer.start();
while (timer.getCurTime() <16){
//get the guess of the user
//checks if it's correct
// if it is correct, output you win and break!
}
//time runs out
}
}
How about an Event driven architecture with Java eventing library?
Example of events with conditions.

Categories

Resources