Why is my button not clickable? (Java) - java

I have a java swing gui program and when I click a toggle button a timer begins but I want to be able to click the same button and the timer stops and right now it won't let me click on it again.
This is in my timer class
public void runningClock(){
isPaused = false;
while(!isPaused){
incrementTime();
System.out.println("Timer Current Time " + getTime());
time.setText(""+ getTime());
try{Thread.sleep(1000);} catch(Exception e){}
}
}
public void pausedClock(){
isPaused=true;
System.out.println("Timer Current Time " + getTime());
time.setText(""+ getTime());
try{Thread.sleep(1000);} catch(Exception e){}
}
and this is in my main class
private void btnRunActionPerformed(java.awt.event.ActionEvent evt) {
if(btnRun.getText().equals("Run")){
System.out.println("Run Button Clicked");
btnRun.setText("Pause");
test.runningClock();
}
else if(btnRun.getText().equals("Pause")){
System.out.println("Pause Button Clicked");
btnRun.setText("Run");
test.pausedClock();
}
}

You're freezing the Swing event thread with your Thread.sleep(...) and while (something) loops. Solution: don't do that -- don't call code on the event thread that occupies the event thread and prevents it from doing its necessary tasks. Instead change the state of your program. And for your clock, use a Swing Timer. For example, please look at my answer and code here.

You are doing this in your program, try{Thread.sleep(1000);} catch(Exception e){}. As this statement is applied on the main thread itself, so the application itself hangs or you can say freezes. What you can do is apply a separate thread for the timer.
new Thread(new Runnable(){
public void run(){
//Do Stuff
}
}).start();

Related

why i can't do anything in my application when Jbutton is pressed and performing its defined function in java?

this is how I'm trying to accomplish this:
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Hello there");
Thread.sleep(1000);
panel.updateUI();
}
});
I set Enter button as the default button so when I keep pressing it the button press maybe 100 times or more but because I'm using Thread.sleep(1000) it takes some time so I have time to type in my JtextField or even close the window but can't do anything.
also, I tried to put btnNewButton.addActionListener() in the run method of a thread but no difference.
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Hello there");
Thread.sleep(1000);
panel.updateUI();
}
});
}
});
thread.start();
// I used try catch block in real code
can anyone help me to solve the issue?
**I'm creating this application in eclipse using windowsBuilder.
This is because of something that is called EDT - the Event Dispatch Thread.
This special purpose thread is responsible for all the events in your GUI - including drawing (refreshing) and interactions (button click). This means, that exactly the same thread is used to draw your application as the one that is used to execute actionPerformed code. Since you are literally halting that thread for some amount of time, your application will be non responsive for that exact period of time.
Whatever is being executed on user interactions should be short and execute fast for the reason of not blocking the application. If you need to do some heavy stuff, there is a facility for that purpose called SwingWorker that allows you to easily do some processing in the background thread (pool) and schedule UI updates during execution (like update of progress bar)
In your second "thread" snippet, your thread is not doing anything beside adding actionListener to the button, and then it terminates (probably after less than 1ms ;)) - action callback is still executed by the EDT. If you would start that custom thread from inside of run method - then it would be indeed in parallel and GUI would not be frozen - but again, SwingWorker is the way to go here.
Not entirely sure what your code is supposed to do, but if you want to press the button and then 1000ms later it will check your field you should do something like this:
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
System.out.println("before 1000ms");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("after 1000ms");
System.out.println("Reading the text field ...");
}
});
thread.start();
System.out.println("after the thread");
Output:
after the thread
before 1000ms
after 1000ms
Reading the text field ...

Java: when trying to pause the program, the entire program freezes

I have a program here with buttons. one is a pause button and the other is a resume button. when i click on the pause button the program stops, but when i attempt to click the resume button nothing happens, i can't even click the button or even X out the window. And the click effects on the buttons also dont work. So i'm assuming the program just froze for some reason
public void createButtons() {
fastForwardButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
synchronized(this) {
this.notify();
}
}
});
stopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
synchronized(this) {
try {
this.wait();
} catch (InterruptedException e3) {
e3.printStackTrace();
}
}
}
});
}
public static void main(String args[]) {
new Sim().start();
}
Any ideas would be much appreciated, thanks
Update:
The purpose of this program is to have a simulation of a game and i have these 2 buttons were u can pause the game and then resume the game. but all in all, i really just want to pause the whole program and resume it
Went for a small hack, i just placed a while loop in the place i wish to pause and added a isPaused state that will be true or false depending on the button pressed. The while loop will be an infinite loop till someone clicks resume, which will update the state to false and the thing will resume itself. Worked out fine in the end

How to pause and resume game using the escape key?

How do I make my Escape key pause and resume a game when it is pressed twice? I have tried calling the key adapter class in my thread class but it only pauses the game; it does not resume it.
Here is the code that pauses the game:
//the thread class
class recMove extends Thread {
JFrame b;
public boolean running=true;
//public boolean gameover=false;
public recMove(JFrame b){
this.b=b;
pauseGame();
}
public void run(){
while(running){
b.repaint();
try {
Thread.sleep(100);
} catch(InterruptedException e){}
}
}
public void pauseGame(){
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
int keyCode=e.getKeyCode();
if(keyCode==KeyEvent.VK_ESCAPE) {
running=false;
System.out.println("escape pressed");
}
if(keyCode==KeyEvent.VK_END){
System.exit(0);
}
}
});
}
}
It doesn't resume because the thread is killed, when you press escape the running value is set to false therefore this loop:
while(running){
b.repaint();
try {
Thread.sleep(100);
} catch(InterruptedException e){}
}
Will end, which in turn will make the run() method exit. When a run() method of a class extending Thread (or implementing Runnable) exits that thread is being killed so there's nothing more listening for your keypresses.
You need to change your run() logic so it doesn't exit when running is set to false but instead wait for the next keypress or add the listener somewhere else (in a different thread) so it will create a new thread with the game again.
Furthermore in your logic esc only changes running to false, if you want it to then resume the game you should check the state of running and if it's false you should set it to true instead.

How to destroy a thread?

I'm making simple game, here is the code:
public class Game extends Canvas implements Runnable {
public void start() {
t = new Thread(this);
t.start();
}
#Override
public void run() {
setVisible(true); // visibility of the thread turn on
while (!t.isInterrupted()) {
if(condition for end the game) {
t.interrupt(); //here i need to destroy the thread
setVisible(false); //visibility to off
}
update();
render();
try {
Thread.sleep(20);
} catch(InterruptedException e) {}
}
}
}
I have another class which extends JFrame and this class is introducing main menu, if my "condition for end the game" is true, the thread dissapears and the menu is visible again, its good, but if i want to start new game again, the behavior of the thread is strange- its seems like the Thread.sleep() method changed from 20 to 10 because all its going faster, probably i need to kill the thread, but i dont know how, thanks
Simple, break the loop:
if(condition for end the game) {
t.interrupt(); //here i need to destroy the thread
setVisible(false); //visibility to off
break;
}
You end the loop and the thread will end.
The easiest way to terminate a Thread is to exit the run function. There is no special handling required, a simple return does the trick.
For you game you might want to consider using a ScheduledExecutorService, which allows you to schedule a Runnable to run at a fixed rate:
executor.scheduleAtFixedRate(gameLoop, 0, 1000/TARGET_FPS, TimeUnit.MILLISECONDS);
Keep in mind that you then need to take out the actual looping of your gameLoop, because that is done by the fixed-rate calling, which will reduce it to:
public void run() {
if (pause == false) {
update();
render();
}
}
Where pause is a boolean should you for some reason want to put the rendering on pause for a while.
With this setup you can terminate the game simply by calling executor.shutdown() which will then suppress any further calls to the runnable.
Not really on-topic, but I'm making a game too, and for pacing I'm using Timer (from swingx):
public class MainGameLoop implements ActionListener{
Timer timer;
public static void main(...){
timer = new Timer(10, this);
timer.start();
}
public void actionPerformed(ActionEvent e) {
...
}
}
Working well to me.

How to deselect an item in a jList after a certain ammount of milliseconds on a mouseExit event

I have a jList called todoList
When the user click on an item in the list, it stays selected. But I would like the currently selected item in the list to deselect "by itself" after 400 milliseconds when the mouse exits the jList.
This must only run if there is something already selected in the list.
I am using Netbeans IDE and this is what is have tried so far:
private void todoListMouseExited(java.awt.event.MouseEvent evt) {
if (!todoList.isSelectionEmpty()) {
Thread thread = new Thread();
try {
thread.wait(400L);
todoList.clearSelection();
} catch (InterruptedException ex) {
System.out.println(ex);
}
}
}
and
private void todoListMouseExited(java.awt.event.MouseEvent evt) {
if (!todoList.isSelectionEmpty()) {
Thread thread= Thread.currentThread();
try {
thread.wait(400L);
todoList.clearSelection();
} catch (InterruptedException ex) {
System.out.println(ex);
}
}
}
These both just make everything stop working.
My though process was that i need to create a new Thread that will wait for 400 milliseconds and then run the clearSelection() method of the jList. This would happen every time the mouse exits the list and run only if there is something in the list that is already selected.
I hope I am explaining my problem thoroughly enough.
The problem is that you are blocking the AWT-Event-Thread.
The solution is to use a swing timer:
private void todoListMouseExited(java.awt.event.MouseEvent evt)
{
if (!todoList.isSelectionEmpty()) {
new Timer(400, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
todoList.clearSelection();
}
}).start();
}
}
The problem is that Object#wait is waiting(rather than sleeping) to be notified but this is not happening. Instead the timeout causing an InterruptedException bypassing the call to clearSelection.
Don't use raw Threads in Swing applications. Instead use a Swing Timer which was designed to interact with Swing components.
if (!todoList.isSelectionEmpty()) {
Timer timer = new Timer(400, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
todoList.clearSelection();
}
});
timer.setRepeats(false);
timer.start();
}

Categories

Resources