The question is old, but since no one answered, I decided to leave a solution that I developed by chance.
It is possible to restore this text using SwingWorker
. Remembering that, in case this waiting time is to perform something more time-consuming, you can delegate to SwingWorker
this activity also, within the method doInBackground()
. See an example below, where I customized a class that extends JLabel
, setting up a unique method for this purpose:
import java.awt.*;
import javax.swing.*;
public class JLabelWithTimerTest extends JFrame {
private static final long serialVersionUID = 1L;
private CustomLabel label;
private JButton btn;
private JPanel pane;
public JLabelWithTimerTest() {
setTitle("JLabel with Timer");
setPreferredSize(new Dimension(300, 200));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane = new JPanel(new GridBagLayout());
setContentPane(pane);
label = new CustomLabel("Texto inicial...");
GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.gridx = 1;
gbc1.gridy = 0;
gbc1.weightx = 1.0;
gbc1.weighty = 1.0;
pane.add(label, gbc1);
btn = new JButton("disparar");
btn.addActionListener(e -> label.setTransientText("texto temporario", 2000));
GridBagConstraints gbc2 = (GridBagConstraints) gbc1.clone();
gbc2.gridx = 1;
gbc2.gridy = 3;
pane.add(btn, gbc2);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
class CustomLabel extends JLabel {
private static final long serialVersionUID = 1L;
private SwingWorker<Void, Void> worker;
public CustomLabel() {
super();
}
public CustomLabel(String text) {
super.setText(text);
}
public void setTransientText(String text, long duration) {
if (worker != null && !worker.isDone()) return;
final String originalText = getText();
worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
setText(text);
Thread.sleep(duration);
return null;
}
@Override
protected void done() {
setText(originalText);
}
};
worker.execute();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(JLabelWithTimerTest::new);
}
}
Working:
The method SetTransientText()
receives the temporary text and the duration, in milliseconds. In the example, the text lasts 2 seconds.
Remembering that, by the fact of SwinWorker
rotate a part, the Thread.sleep
does not lock graphical interface.
What do you mean? You want to change the text of a jlabel for a while and then go back to the previous text?
– user28595
Exactly that !
– Prostetnic Vogon Jeltz
In case I have to execute a method that takes a few seconds to get ready and I would like to notify the user that it is running and this way it seemed simpler. If you know any other simpler or more efficient way to make this happen I would also be pleased to know.
– Prostetnic Vogon Jeltz
Are you using swing? If so, you’ll probably need to use Swingworker
– user28595
I’ll look into it, thanks for helping :D
– Prostetnic Vogon Jeltz
It was a suggestion, but soon someone responds with a practical example.
– user28595
Why not change the text normally (the way you are doing it) and - when this time-consuming method is over - do you change it back exactly the same way? (of course taking into account the thread right, be it via
SwingWorker
or any other means)– mgibsonbr
So, I’m new and I don’t know how to do this. I’m going to look for some material on Thread to see.
– Prostetnic Vogon Jeltz