When a JButton
is clicked I need to empty the JLabel
using setText
. Inside actionPerformed
, I have another Java class call.
If it returns an exception, then I have to change the label to setText("Error occurred")
.
If it returns the result, I have to change the same label to setText("Process completed")
.
I tried to generate an exception and it worked fine, but when I click the button again the text is not replaced with the empty string. Instead, it still shows "error occurred".
Please see the code below and let me know what I need to change.
btnConvert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label1.setText("");
if(lblSource.getText().isEmpty()) {
label2.setText("You have missed to select!! Please select it");
}
else {
label1.setText("processing... ");
VelConverter v = new VelConverter(locations);
String response = v.convert();
if(response.startsWith("Exception")) {
//code
label1.setText("Error Occurred");
}
else {
//code
label1.setText("Completed");
}
}
}
Thanks Rocco and Andrew Thomson. Your suggestion about concurrency made me to research on Swing worker. I just separated the threads using Swing worker and I were able to achieve my requirement.
This link made me to understand Swing worked in simple terms. https://www.geeksforgeeks.org/swingworker-in-java/