'Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Blocking in Event Dispatch Thread not allowed
I'm trying to create a simple calculator that uses the MatLab engine to compute my operations. When i feed the program data from keyboard everything works fine, but when i feed numbers from a jButton it throws a "java.lang.IllegalStateException".
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import com.mathworks.engine.EngineException;
import com.mathworks.engine.MatlabEngine;
import java.io.StringWriter;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.awt.event.ActionEvent;
public class Grafica {
public JFrame frame;
public JTextField Display;
String a;
String b;
String ris;
String op;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Grafica window = new Grafica();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Grafica() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
public void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 521);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
Display = new JTextField();
Display.setBounds(0, 0, 432, 79);
frame.getContentPane().add(Display);
Display.setColumns(10);
JButton btn7 = new JButton("7");
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = Display.getText() + btn7.getText();
Display.setText(EnterNumber);
}
});
btn7.setBounds(12, 110, 97, 50);
frame.getContentPane().add(btn7);
JButton btn8 = new JButton("8");
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = Display.getText() + btn8.getText();
Display.setText(EnterNumber);
}
});
btn8.setBounds(167, 110, 98, 50);
frame.getContentPane().add(btn8);
JButton btn9 = new JButton("9");
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = Display.getText() + btn9.getText();
Display.setText(EnterNumber);
}
});
btn9.setBounds(323, 110, 97, 50);
frame.getContentPane().add(btn9);
JButton btn4 = new JButton("4");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = Display.getText() + btn4.getText();
Display.setText(EnterNumber);
}
});
btn4.setBounds(12, 173, 97, 50);
frame.getContentPane().add(btn4);
JButton btn5 = new JButton("5");
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = Display.getText() + btn5.getText();
Display.setText(EnterNumber);
}
});
btn5.setBounds(167, 173, 97, 50);
frame.getContentPane().add(btn5);
JButton btn6 = new JButton("6");
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = Display.getText() + btn6.getText();
Display.setText(EnterNumber);
}
});
btn6.setBounds(323, 173, 97, 50);
frame.getContentPane().add(btn6);
JButton btn1 = new JButton("1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = Display.getText() + btn1.getText();
Display.setText(EnterNumber);
}
});
btn1.setBounds(12, 236, 97, 50);
frame.getContentPane().add(btn1);
JButton btn2= new JButton("2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = Display.getText() + btn2.getText();
Display.setText(EnterNumber);
}
});
btn2.setBounds(167, 236, 97, 50);
frame.getContentPane().add(btn2);
JButton btn3 = new JButton("3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = Display.getText() + btn3.getText();
Display.setText(EnterNumber);
}
});
btn3.setBounds(323, 236, 97, 50);
frame.getContentPane().add(btn3);
JButton btnPlus = new JButton("+");
btnPlus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Display.getText();
op = "+";
Display.setText(null);
}
});
btnPlus.setBounds(12, 299, 97, 50);
frame.getContentPane().add(btnPlus);
JButton btn0 = new JButton("0");
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String EnterNumber = Display.getText() + btn0.getText();
Display.setText(EnterNumber);
}
});
btn0.setBounds(168, 299, 97, 50);
frame.getContentPane().add(btn0);
JButton btnSub = new JButton("-");
btnSub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Display.getText();
op = "-";
Display.setText(null);
}
});
btnSub.setBounds(323, 299, 97, 50);
frame.getContentPane().add(btnSub);
JButton btnMul = new JButton("*");
btnMul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Display.getText();
op = "*";
Display.setText(null);
}
});
btnMul.setBounds(12, 362, 97, 50);
frame.getContentPane().add(btnMul);
JButton btnDiv = new JButton("/");
btnDiv.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = Display.getText();
op = "/";
Display.setText(null);
}
});
btnDiv.setBounds(323, 362, 97, 50);
frame.getContentPane().add(btnDiv);
JButton btnEqual= new JButton("=");
btnEqual.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
b = Display.getText();
String eq = a.concat(op.concat(b));
StringWriter output = new StringWriter();
StringWriter errors = new StringWriter();
MatlabEngine engine = null;
try {
engine = MatlabEngine.startMatlab();
} catch (EngineException | IllegalArgumentException | IllegalStateException | InterruptedException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
try {
engine.eval(eq, output, errors);
} catch (InterruptedException | ExecutionException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
engine.close();
} catch (EngineException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btnEqual.setBounds(167, 362, 97, 50);
frame.getContentPane().add(btnEqual);
}
}
The console prints the following lines:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Blocking in Event Dispatch Thread not allowed
at com.mathworks.mvm.exec.FutureResult.waitInternal(FutureResult.java:316)
at com.mathworks.mvm.exec.FutureResult.get(FutureResult.java:261)
at com.mathworks.engine.FutureResult.get(FutureResult.java:44)
at com.mathworks.engine.MatlabEngine.eval(MatlabEngine.java:373)
at oop.Grafica$16.actionPerformed(Grafica.java:222)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I've tried to start MatLab asynchronously but I get the same result.
Solution 1:[1]
A single thread (the AWT Event Dispatch Thread) should handle all the AWT/Swing interactions for a Java application. This includes repainting windows. So it is important to ensure any code executing on it returns control promptly. Your library appears to be indicating that the function called may not do that.
The solution is to run that portion of code in a different thread, and then update the GUI within java.awt.EventQueue.invokeLater (as mentioned earlier this all needs to be done on the EDT).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tom Hawtin - tackline |
