Try this instead
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;
import javax.swing.*;
import java.util.*;
public class Game extends Applet implements ActionListener{
String textB="textB",output="",input="type here";
JTextField inputBox;
JTextArea mainBox;
public void actionPerformed(ActionEvent e) {
if (textB.equals(e.getActionCommand())) {
Typewriter type = new Typewriter(inputBox.getText());
type.start();
inputBox.setText(null);
}
}
class Typewriter extends Thread{
String text=null;
Random SEED = new Random();
Random rand = new Random(SEED.nextInt(999999));
public Typewriter(String text){
this.text = text;
}
public void run() {
char adder;
for(int i=0;i<text.length();i++){
adder=text.charAt(i);
//JOptionPane.showMessageDialog(null,adder, "Button",JOptionPane.PLAIN_MESSAGE);
output+=adder+"";
mainBox.setText(output);
try {
sleep(rand.nextInt(25)+100);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println(EventQueue.isDispatchThread());
}
output +="\n";
mainBox.setText(output);
}
}
public void init() {
inputBox = new JTextField();
mainBox = new JTextArea();
JScrollPane bottomScrollPane = new JScrollPane(mainBox);
mainBox.setEditable(false);
inputBox.setText(input);
//inputBox.select(0,input.length());
inputBox.setActionCommand(textB);
inputBox.addActionListener(this);
setLayout(new BorderLayout());
add(bottomScrollPane,BorderLayout.CENTER);
add(inputBox, BorderLayout.SOUTH);
}
}
I believe you were getting that behavior because you were sleeping directly in the AWT Event Dispatch thread.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Offline 6,761 posts
since May 2007