F
Florence HENRY
Hello,
I'm trying to fire an ActionEvent since a secondary thread in a java
application. I tried to somplify the code as mush as possible, but I
can't figure out why it does not work.
Here is the code :
// SimulateButton.java
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class SimulateButton implements ActionListener {
public Component createComponents() {
JButton b1 = new JButton("Button 1");
b1.addActionListener(this);
b1.setActionCommand("button 1 pressed");
Simulateur simulateur = new Simulateur("test");
simulateur.start();
return b1;
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SimulateButton simul = new SimulateButton();
Component contents = simul.createComponents();
frame.getContentPane().add(contents);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public void actionPerformed(ActionEvent ae) {
System.err.println(ae.getActionCommand());
}
}
// Simulateur.java
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
public class Simulateur extends Thread {
public Simulateur(String str) {
super(str);
}
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println(i + " " + getName());
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(
new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
"simulated button"));
try {
sleep(1000);
} catch (InterruptedException e) {}
}
System.out.println("DONE! " + getName());
}
}
At the execution, I get :
$ java SimulateButton
0 test
unable to dispatch event:
java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=simulated
button,when=0,modifiers=] on Thread[test,6,main]
1 test
unable to dispatch event:
java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=simulated
button,when=0,modifiers=] on Thread[test,6,main]
2 test
unable to dispatch event:
java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=simulated
button,when=0,modifiers=] on Thread[test,6,main]
DONE! test
Could someone explain to me why I cannot fire this ActionEvent ?
Thanks
I'm trying to fire an ActionEvent since a secondary thread in a java
application. I tried to somplify the code as mush as possible, but I
can't figure out why it does not work.
Here is the code :
// SimulateButton.java
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class SimulateButton implements ActionListener {
public Component createComponents() {
JButton b1 = new JButton("Button 1");
b1.addActionListener(this);
b1.setActionCommand("button 1 pressed");
Simulateur simulateur = new Simulateur("test");
simulateur.start();
return b1;
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SimulateButton simul = new SimulateButton();
Component contents = simul.createComponents();
frame.getContentPane().add(contents);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public void actionPerformed(ActionEvent ae) {
System.err.println(ae.getActionCommand());
}
}
// Simulateur.java
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
public class Simulateur extends Thread {
public Simulateur(String str) {
super(str);
}
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println(i + " " + getName());
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(
new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
"simulated button"));
try {
sleep(1000);
} catch (InterruptedException e) {}
}
System.out.println("DONE! " + getName());
}
}
At the execution, I get :
$ java SimulateButton
0 test
unable to dispatch event:
java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=simulated
button,when=0,modifiers=] on Thread[test,6,main]
1 test
unable to dispatch event:
java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=simulated
button,when=0,modifiers=] on Thread[test,6,main]
2 test
unable to dispatch event:
java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=simulated
button,when=0,modifiers=] on Thread[test,6,main]
DONE! test
Could someone explain to me why I cannot fire this ActionEvent ?
Thanks