Pausing Swing GUI - java

Hy!
So I'm starting with netbeans java gui development and I have run into this problem:
I have made a window with a button and a text field. When the user clicks the button I want the text field to start typing itself with a delay. So for example:
textfield.text=h
wait(1) sec
textfield.text=he
wait(1) sec
textfield.text=hel
wait(1) sec
textfield.text=hell
wait(1) sec
textfield.text=hello
I have already tried with Thread.sleep(), but in the example above it waits 4 seconds or so and after that displays the whole text (so it's not giving me the typo effect that I would want).
Can somebody help me with this?

If you use Thread.sleep(...) or any other code that delays the Swing event thread, you'll end up putting the entire Swing event thread to sleep, and with it your application. The key here is to instead use a Swing Timer. In the Timer's ActionListener's actionPerformed method, add a letter and increment your index, and then use that index to decide what letter to next add.
i.e.,
String helloString = "hello";
// in the Timer's ActionListener's actionPerformed method:
if (index >= helloString.length()) {
// stop the Timer here
} else {
String currentText = textField.getText();
currentText += helloString.charAt(index);
textField.setText(currentText);
index++;
}

Got it working by using something like this:
Timer a,b,c
Timer c=new Timer(500, new ActionListener(){public void actionPerformed(ActionEvent e){textfield.setText(abc)}})
Timer b=new Timer(500, new ActionListener(){public void actionPerformed(ActionEvent e){textfield.setText(ab);c.start()}})
Timer a=new Timer(500, new ActionListener(){public void actionPerformed(ActionEvent e){textfield.setText(a);b.start()}})
a.setRepeats(false);
b.setRepeats(false);
c.setRepeats(false);
a.start()
Does anybody know a more simple method with the same effect maybe?

Related

How to use a swing timer to wait 1 second before continuing to execute code?

I literally just want to do the following:
System.out.println("First line");
// wait 1 second
button.setText("newText");
I tried using thread sleep but it doesn't work. I'm trying to understand swing timers but its so needlessly confusing. I just want the code to delay a second.
Further clarification:
public void actionPerformed(ActionEvent e) {
JButton clicked = (JButton) e.getSource();
clicked.setText("you clicked this button");
//timer here for 1 second,like a delay
clicked.setText("");
Try it like this. The first argument is the initial delay of 1000 ms. Then the ActionEvent object is used to get the creator of the event (the timer) and stop it.
System.out.println("First line");
new Timer(1000, (ae)->{
button.setText("newText");
((Timer)ae.getSource()).stop();
}).start();

Delay between multiple setText java

I want to put a delay between every if in this for
I've triend with Thread.sleep() but this freezes the gui and I don't know is it viable to use multiple swing timers in a loop.
Here I'm trying with a swing timer and keeps freezing the gui, what I'm doing wrong?.
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
int i=0;
public void actionPerformed(ActionEvent evt) {
try
{
System.out.print(solucion.get(i)+" "+solucion.get(i+1)+" "+solucion.get(i+2)+" \n"+solucion.get(i+3)+" "+solucion.get(i+4)+" "+solucion.get(i+5)+" \n"+solucion.get(i+6)+" "+solucion.get(i+7)+" "+solucion.get(i+8));
System.out.println("\n");
Btn1.setText(solucion.get(i));
Btn2.setText(solucion.get(i+1));
Btn3.setText(solucion.get(i+2));
Btn4.setText(solucion.get(i+3));
Btn5.setText(solucion.get(i+4));
Btn6.setText(solucion.get(i+5));
Btn7.setText(solucion.get(i+6));
Btn8.setText(solucion.get(i+7));
Btn9.setText(solucion.get(i+8));
i++;
}
catch(IndexOutOfBoundsException e){((Timer)evt.getSource()).stop();} //if it gets a error we are at the end of the list and stop the timer
}
};
new Timer(delay, taskPerformer).start();
Use a Swing Timer. The Timer replaces the loop.
Every time the Timer fires you set the text and then increment the value of "i". When "i" reaches a specific value you stop the Timer.
See: Jlabel showing both old and new numbers for a simple example to get you started.
Read the section from the Swing tutorial on How to Use Swing Timers for more information.
If you want the guid not to freeze you need to execute it in a different thread. Running it in the main thread will aways cause the guid to freeze. You are using swing so the way to go would be:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// put your statements and delay here
}
});

Java pause GUI from inside ActionListener [duplicate]

Hy!
So I'm starting with netbeans java gui development and I have run into this problem:
I have made a window with a button and a text field. When the user clicks the button I want the text field to start typing itself with a delay. So for example:
textfield.text=h
wait(1) sec
textfield.text=he
wait(1) sec
textfield.text=hel
wait(1) sec
textfield.text=hell
wait(1) sec
textfield.text=hello
I have already tried with Thread.sleep(), but in the example above it waits 4 seconds or so and after that displays the whole text (so it's not giving me the typo effect that I would want).
Can somebody help me with this?
If you use Thread.sleep(...) or any other code that delays the Swing event thread, you'll end up putting the entire Swing event thread to sleep, and with it your application. The key here is to instead use a Swing Timer. In the Timer's ActionListener's actionPerformed method, add a letter and increment your index, and then use that index to decide what letter to next add.
i.e.,
String helloString = "hello";
// in the Timer's ActionListener's actionPerformed method:
if (index >= helloString.length()) {
// stop the Timer here
} else {
String currentText = textField.getText();
currentText += helloString.charAt(index);
textField.setText(currentText);
index++;
}
Got it working by using something like this:
Timer a,b,c
Timer c=new Timer(500, new ActionListener(){public void actionPerformed(ActionEvent e){textfield.setText(abc)}})
Timer b=new Timer(500, new ActionListener(){public void actionPerformed(ActionEvent e){textfield.setText(ab);c.start()}})
Timer a=new Timer(500, new ActionListener(){public void actionPerformed(ActionEvent e){textfield.setText(a);b.start()}})
a.setRepeats(false);
b.setRepeats(false);
c.setRepeats(false);
a.start()
Does anybody know a more simple method with the same effect maybe?

Swing Timer Not Working (Java)

For some reason even though I am using the exact code example from oracle's website for the Swing Timer it is not waiting for 1 second. It just skips to the JOptionPane that says "Your score was etc etc".
Here is my source code for a school project. Why is this not working and not waiting for 1 second before running the rest of the code?
//Check to see if user has enetered anything
if(!answered)
{
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//...Perform a task...
}
};
new Timer(delay, taskPerformer).start();
afk = true;
incorrect += 1;
answered = true; //This breakes it out of the loop
}
A timer is used to run a callback after a specific amount of time. If you simply want to delay, you can either move the code to be run after the delay into the taskPerformer action listener.
Thread.sleep(1000) is not ideal here, because it will cause the UI to completely freeze as you will make the UI thread sleep.

Pause game and add flashing text Java

I have this loop
while (true) {
game.update();
view.repaint();
Thread.sleep(DELAY);
}
In the game.update various components of the game have their position changed and those updates are reflected when the repaint() method is called on the view. The view extends JComponent and loops through the game objects and calls their print methods.
What I want to do is have a boolean called nextLevel in the game and if it's true Flash text on the screen for the player to notify them that they're going onto the next level. Maybe flash 4-5 times. Then continue the game.
Is this possible? I have been playing around with Thead.Sleep() but this only seems to pause the displaying and in the background the game is still going on.
Any ideas on how to do this?
Maybe you want to avoid threading by using a Timer object.
an example like that could be
int flashTimer = 0;
if(nextLevel) {
Timer timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
//flash something method here
flashTimer++;
}
});
timer.start();
}
and then check your flashTimer if it reaches the number you want then just stop the timer by timer.stop();
Just an idea which seems to me a bit simpler. the 1000 value is milliseconds which is passed and executes the code inside the actionPerformed method every 1 sec.
Hope it helped

Categories

Resources