, JButton:
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
public class TypomaticButton extends JButton implements MouseListener {
private boolean autotype = false;
private static Thread theThread = null;
private String myName = "unknown";
private int
speed = 150,
wait = 300,
decrement = (wait - speed) / 10;
TypomaticButton(Action action){
super(action);
myName = action.getValue(Action.NAME).toString();
addMouseListener(this);
}
TypomaticButton(String text){
super(text);
setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
myName = text;
addMouseListener(this);
}
@Override
public void mouseClicked(MouseEvent arg0) {}
@Override
public void mouseEntered(MouseEvent arg0) { }
@Override
public void mouseExited(MouseEvent arg0) { }
@Override
public void mousePressed(MouseEvent arg0) {
autotype = true;
theThread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 10000; i > 0 && autotype; i--) {
try {
Thread.sleep(wait);
} catch (InterruptedException e) {
break;
}
if(wait != speed){
wait = wait - decrement;
if(wait < speed)
wait = speed;
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if(!autotype)
return;
ActionListener[] als = getActionListeners();
for(ActionListener al : als){
ActionEvent aevent = new ActionEvent(getClass(), 0, myName);
al.actionPerformed(aevent);
}
}
});
}
autotype = false;
}
});
theThread.start();
}
@Override
public void mouseReleased(MouseEvent arg0) {
autotype = false;
wait = 300;
}
void speed(int millisecs){
speed = millisecs;
decrement = (wait - speed) / 10;
}
void stop(){
autotype = false;
if(theThread != null){
theThread.interrupt();
}
}
}
.
, .