I have created a scientific calculator program. What my program does is take \"label\" which is the string inputted by the user using buttons and evaluate the expression. What m
The solution is to:
Use a separate Thread
and use SwingUtilities.inovkeLater()
to place the text field updates on the EDT, or
Use a SwingWorker
, which has a built in API to support the above to features.
Read the section from the Swing tutorial on Concurrency in Swing for complete information and working examples of a SwingWorker.
In the method where you call the calculations method, wrap the call in this:
public void actionPerformed(ActionEvent e){
Thread t = new Thread(new Runnable() {
public void run() {
try {
label2.setText(bracket(splitLabel(label.getText())));
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.start();
}
this should solve the problem of the program freezing. if not pleasetell me
In stead of wrapping the call in a regular Thread
, make use of Swingutilities. You will need to do this since Swing isn't threadsafe. This way you will still be working on the EDT.
SwingUtilities.invokeLater(() -> {
try {
label2.setText(bracket(splitLabel(label.getText())));
} catch (Exception e) {
e.printStackTrace();
}
});