Java add Typewriter effect to JTextArea - java

I need to display output to a JTextArea one character at a time, with a slight delay between each character. My attempt is as follows:
private static void printInput(final String input)
{
Timer timer = new Timer(60,new ActionListener(){
public void actionPerformed(ActionEvent e)
{
for(int i = 0; i<input.length(); i++)
{
messageArea.append(Character.toString(input.charAt(i)));
}
}
});
}
There are similar questions however I could not find one with an example I could use to figure out my problem

A Timer is a pseudo loop, that it is, it triggers an cycle after a predefined delay, each cycle is an iteration of the loop and each iteration you need to update the UI and update the iterator value.
Now, because I don't like working in the static context, the first thing I suggest is you wrap up the basic concept into a separate class. This way you can easily encapsulate the state
public class TypeWriter {
private Timer timer;
private int characterIndex = 0;
private String input;
private JTextArea textArea;
public TypeWriter(JTextArea textArea, String input) {
this.textArea = textArea;
this.input = input;
timer = new Timer(60, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (characterIndex < input.length()) {
textArea.append(Character.toString(input.charAt(characterIndex)));
characterIndex++;
} else {
stop();
}
}
});
}
public void start() {
textArea.setText(null);
characterIndex = 0;
timer.start();
}
public void stop() {
timer.stop();
}
}
So, this is pretty basic, all this does is, using a Timer, check to see if there are any more characters that need to be printed, if there is, it takes the next character and appends it to the text area and updates the iterator value. If we're at the end of the text, it will stop the Timer (ie, the exit condition)

Related

What to use to create search engine using onKeyReleased event

I am trying to create a search bar which will search the entered string or character from db. While first character is typed in textfield it should wait for next 200ms, if next character is entered within that time then it will restart the counter and again wait for next 200ms if not, then it will search from db.
Here is some code which i tried but not work for me
#FXML protected void keyReleased(KeyEvent evt)throws Exception {
if (evt.getCode() != KeyCode.BACK_SPACE) {
String ch = evt.getText();
String[] myArray = new String[5];
run();
searchFrmDb(ch, myArray);
}
}
public void run(){
for(int i=1;i<5;i++){
try{
Thread.sleep(200);
}catch(InterruptedException e){System.out.println(e);}
System.out.println(i);
}
}
public void searchFrmDb(String ch,String[] myArray){
//db search operation ...
}
I am new in java help me out to sort out my problem
What should i use thread or Timer or anything else
Consider using a PauseTransition for functionality like this.
public class ControllerClass {
private final PauseTransition pauseBeforeSearch = new PauseTransition(Duration.millis(200));
#FXML protected void keyReleased(KeyEvent evt)throws Exception {
if (evt.getCode() != KeyCode.BACK_SPACE) {
pauseBeforeSearch.setOnFinished(e -> {
searchFrmDb(evt.getText(), new String[5]);
});
pauseBeforeSearch.playFromStart();
}
}
}
I would have a look at java.util.Timer and java.util.TimerTask:
class SearchTimerTask extends TimerTask{
#Override
public void run(){
searchFrmDb();
}
#Override
public void cancel(){
super.cancel();
//handle cancellation logic if necessary
}
}
Keep a reference to a Timer and a TimerTask somewhere:
Timer timer = new Timer();
TimerTask task;
Then in your key event handler you simply cancel the current task if there is one and fire up a new one:
#FXML protected void keyReleased(KeyEvent evt)throws Exception {
if (evt.getCode() != KeyCode.BACK_SPACE) {
if(task != null){
task.cancel();
task = new SearchTimerTask();
}
timer.schedule(task, 200);
}
}

Increment an integer in a JTextField by one

I am trying to increment the value of a JTextfield (tenSecs) by one for every instance that my other JTextfield (oneSecs) reaches 0. I am having difficulty trying to increase the value of my tenSecs JTextfield as it is always 0. When manually changing the number, as soon as oneSecs reaches 0, tenSecs reverts to 0.
javax.swing.Timer tm = new javax.swing.Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
AddOneActionPerformed(evt);
}
});
private void StartStopTimerActionPerformed(java.awt.event.ActionEvent evt) {
if (!tm.isRunning()) {
tm.start();
} else {
tm.stop();
}
ScheduledExecutorService e= Executors.newSingleThreadScheduledExecutor(); //Start new scheduled executor service to invoke a timer that start wehn button is pressed
e.scheduleAtFixedRate(new Runnable() {
#Override
public void run() {
//Follow this Override to do task
SwingUtilities.invokeLater(new Runnable() {
//Override will let the task run
#Override
public void run() {
oneSecsDisplay.setIcon(new ImageIcon("images\\" + TextGrabber() + ".png"));
oneSecs.setText( DigitValue.getText());
int i = 0;
if (Integer.parseInt(oneSecs.getText()) == i) {
tenSecs.setText(Integer.toString(i++));
}
}
});
}
}, 0, 100, TimeUnit.MILLISECONDS); //Update will be for every 100 milliseconds concurrent to system time
}
// Variables declaration - do not modify
private javax.swing.JTextField oneSecs;
private javax.swing.JTextField tenSecs;
// End of variables declaration
I am sure that the error is occurring around the line int i = 0;. I am also sure that my way of increasing the value is not the best way to do this, if someone could kindly point me in the correct direction
If I'm understanding you correctly, try tenSecs.setText(String.valueOf(Integer.parseInt(tenSecs.getText()) + 1));

JLabel.setText() is only setting text for last element in a loop

First of all, apologies for how long winded this is.
I'm trying to make a simple roulette game that allows a user to add players, place bets for these players, and spin the roulette wheel, which is represented as a simple JLabel that updates it's text with each number it passes.
However, I've run into a bug that I'm having a lot of trouble with: the JLabel only updates the text for the last element in my loop.
Basically, my solution works like this:
When a user presses a button labelled "Spin" (given that users have been added to the game), I call a method from a class called SpinWheelService, which is an Observable singleton which in turn calls the notifyObservers() method:
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
String description = null;
if (ADD_PLAYER.equals(cmd)) {
addDialog();
} else if (PLACE_BET.equals(cmd)) {
betDialog();
} else if (SPIN.equals(cmd)) {
SpinWheelService.sws.setSpinWheelService();
} else if (DISPLAY.equals(cmd)) {
System.out.println("Display selected!");
}
}
Here is my SpinWheelService class:
package model;
import java.util.*;
public class SpinWheelService extends Observable {
public static SpinWheelService sws = new SpinWheelService();
public void setSpinWheelService() {
setChanged();
notifyObservers();
}
}
The only listener registered for SpinWheelService is this class, where GameEngine is my game engine that handles internal game logic, WheelCallbackImpl is a class that updates the View:
class SpinWheelObserver implements Observer {
GameEngine gameEngine;
ArrayList<SimplePlayer> players;
WheelCallbackImpl wheelCall;
int n;
public SpinWheelObserver(GameEngine engine, WheelCallbackImpl wheel, ArrayList<SimplePlayer> playerList) {
players = playerList;
gameEngine = engine;
wheelCall = wheel;
}
public void update(Observable sender, Object arg) {
// check if any players are present
if (players.size() == 0) {
System.out.println("Empty player array!");
return;
}
do {
gameEngine.spin(40, 1, 300, 30, wheelCall);
n = wheelCall.playback();
} while (n== 0);
}
}
The main point of note here is my gameEngine.spin() method, which is this:
public class GameEngineImpl implements GameEngine {
private List<Player> playerList = new ArrayList<Player>();
// method handles the slowing down of the roulette wheel, printing numbers at an incremental delay
public void delay(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
System.out.println("Sleep method failed.");
}
}
public void spin(int wheelSize, int initialDelay, int finalDelay,
int delayIncrement, WheelCallback callback) {
Random rand = new Random();
int curNo = rand.nextInt(wheelSize) + 1;
int finalNo = 0;
assert (curNo >= 1);
// while loop handles how long the wheel will spin for
while (initialDelay <= finalDelay) {
delay(initialDelay);
initialDelay += delayIncrement;
// handles rotating nature of the wheel, ensures that if it reaches wheel size, reverts to 1
if (curNo > wheelSize) {
curNo = 1;
callback.nextNumber(curNo, this);
curNo++;
}
assert (curNo <= wheelSize);
callback.nextNumber(curNo, this);
curNo++;
finalNo = curNo - 1;
}
calculateResult(finalNo);
callback.result(finalNo, this);
}
The method callback.nextNumber(curNo, this):
public void nextNumber(int nextNumber, GameEngine engine) {
String strNo = Integer.toString(nextNumber);
assert (nextNumber >= 1);
System.out.println(nextNumber);
wcWheel.setCounter(strNo);
}
Where in, wcWheel is my singleton instance of my View, which contains the method setCounter():
public void setCounter(String value) {
label.setText(value);
}
Sorry for how convoluted my explanation is, but basically what it boils down to is that setCounter() is definitely being called, but seems to only call the setText() method on the final number. So what I'm left with is an empty label that doesn't present the number until the entire roulette has finished spinning.
I've determined that setCounter() runs on the event dispatch thread, and I suspect this is a concurrency issue but I have no idea how to correct it.
I've tried to include all relevant code, but if I'm missing anything, please mention it and I'll post it up as well.
I'm at my wits end here, so if anyone would be kind of enough to help, that would be so great.
Thank you!
Your while loop along Thread.sleep() will block and repainting or changing of the UI until the loop is finished.
Instead you'll want to implement a javax.swing.Timer for the delay, and keep a counter for the number of ticks, to stop it. You can see more at How to Use Swing Timers
The basic construct is
Timer ( int delayInMillis, ActionListener listener )
where delayInMillis is the millisecond delay between firing of an ActionEvent. This event is listened for by the listener. So every time the event is fired, the actionPerfomed of the listener is called. So you might do something like this:
Timer timer = new Timer(delay, new ActionListener()(
#Override
public void actionPerformed(ActionEvent e) {
if (count == 0) {
((Timer)e.getSource()).stop();
} else {
//make a change to your label
count--;
}
}
));
You can call timer.start() to start the timer. Every delay milliseconds, the label will change to what you need it to, until some arbitrary count reaches 0, then timer stops. You can then set the count variable to whatever you need to, if you want to to be random, say depending on how hard the wheel is spun :D
I think you didn't post all the relevant code that is required to know exactly the problem.
But most likely the problem is due to you run your loop and JLabel.setText() in the EDT (Event Dispatching Thread).
Note that updating the UI components (e.g. the text of a JLabel) also happens in the EDT, so while your loop runs in the EDT, the text will not be updated, only after your loop ended and you return from your event listener. Then since you modified the text of the JLabel it will be refreshed / repainted and you will see the last value you set to it.
Example to demonstrate this. In the following example a loop in the event listener loops from 0 to 9 and sets the text of the label, but you will only see the final 9 be set:
JPanel p = new JPanel();
final JLabel l = new JLabel("-1");
p.add(l);
JButton b = new JButton("Loop");
p.add(b);
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
for ( int i = 0; i < 10; i++ ) {
l.setText( "" + i );
try { Thread.sleep( 200 ); } catch ( InterruptedException e1 ) {}
}
}
} );
A proposed solution: Use javax.swing.Timer to do the loop's work. Swing's timer calls its listeners in the EDT so it's safe to update swing components in it, and once the listener returns, a component UI update can happen immediately:
JPanel p = new JPanel();
final JLabel l = new JLabel("-1");
p.add(l);
JButton b = new JButton("Loop");
p.add(b);
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new Timer(200, new ActionListener() {
int i = 0;
#Override
public void actionPerformed(ActionEvent e2) {
l.setText("" + i);
if ( ++i == 10 )
((Timer)e2.getSource()).stop();
}
}).start();
}
} );
In this solution you will see the label's text counting from 0 up to 9 nicely.
It's appears to me that your entire game must block in the action handler until the while loop has finished? So the text of the label will be getting updated but only the last update will be visible once the AWT thread is running again.

How do I make this java for loop pause for 1/2 a second between each iteration?

private class MultipleGensListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
for(int i = 0; i < 25; i++)
{
game.runSimulationOneGen();
changeGrid();
}
}
}
//this is the loop. The changeGrid method displays a game grid on a GUI but
// only the 25th iteration is visible on screen. I would like each one to be
// visible for about a half a second before the loop continues.
// I have seen some questions answered on here that are very close to what I'm asking,
// but I just don't really understand how to apply it to my program..
// thanks for any help.
If the code performed by the simulation is quick and does not consume too much CPU and time, then consider using a Swing Timer to do your looping and delay. Otherwise, you'll need to use a background thread such as can be done with a SwingWorker object.
For e.g. if using both Swing Timer and SwingWorker:
private class MultipleGensListener implements ActionListener {
protected static final int MAX_INDEX = 25;
public void actionPerformed(ActionEvent e) {
int timerDelay = 500; // ms delay
new Timer(timerDelay, new ActionListener() {
int index = 0;
public void actionPerformed(ActionEvent e) {
if (index < MAX_INDEX) { // loop only MAX_INDEX times
index++;
// create the SwingWorker and execute it
new SwingWorker<Void, Void>() {
#Override
protected Void doInBackground() throws Exception {
game.runSimulationOneGen(); // this is done in background thread.
return null;
}
#Override
protected void done() {
changeGrid(); // this is called on EDT after background thread done.
}
}.execute(); // execute the SwingWorker
} else {
((Timer) e.getSource()).stop(); // stop the timer
}
}
}).start(); // start the Swing timer
}
}
NEVER BLOCK THE GUI EVENT THREAD
you can use a timer for that and have it only fire 25 times
final Timer t = new Timer(500,null);
t.addActionListener(new ActionListener(){
int i=0;
public void actionPerformed(ActionEvent e){
game.runSimulationOneGen();//run 1 iteration per tick
changeGrid();
if(i>25){t.stop();}
i++;
}
});
t.setRepeats(true);
t.start();
btw the reason only the last iteration is shown is that gui updates (redraws) are done in a separate event, but to let another event trigger you need to return from the listener method which you didn't
the Timer I showed is a more elaborate iteration which lets other events run in between iterations allowing the gui to show the changes
check my post that shows both methods java.swing.Timer#setDelay(int)
and
correct usage of Thread.sleep(int)
java wait cursor display problem

Having a problem getting my ActionListeners, Handlers, and GUI to communicate

So I am trying to get my GUI to work. When I run the code below, it does nothing, and I'm sure I'm probably just doing something dumb, but I am completely stuck...
public void actionPerformed(ActionEvent e){
UI.getInstance().sS++;
if((UI.getInstance().sS %2) != 0){
UI.getInstance().startStop.setName("STOP");
UI.getInstance().change.setEnabled(false);
}else if(UI.getInstance().sS%2 == 0){
UI.getInstance().startStop.setName("START");
UI.getInstance().change.setEnabled(true);
}
}
public void setStartListener(StartHandler e){
this.startStop.addActionListener(e);
}
sS is an int that increments every time the button startStop is clicked. change is also a button.
not really an answer, but I think your code would be simpler if you used a boolean instead of an int, something like:
public void actionPerformed(ActionEvent e){
final boolean isEnabled = UI.getInstance().change.isEnabled();
if(isEnabled){
UI.getInstance().startStop.setName("STOP");
}else{
UI.getInstance().startStop.setName("START");
}
UI.getInstance().change.setEnabled(!isEnabled);
}
Here's an example that shows a different approach to managing a Start/Stop button. It uses an instance of javax.swing.Timer to pace updates. Encapsulating the control button and display label may simplify maintenance. This variation illustrates adding a third command to pause updates.
private static final String Start = "Start";
private static final String Stop = "Stop";
…
private static void create() {
…
final JButton button = new JButton(Stop);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (Stop.equals(cmd)) {
jtl.stop();
button.setText(Start);
} else {
jtl.start();
button.setText(Stop);
}
}
});
…
}
More generally, use Action to encapsulate functionality for use elsewhere in your program. This example "exports several actions that make it easy to use them in a control panel."

Categories

Resources