Temporary text in JLabel

4

I would like to change the text of a JLabel and leave it for just a few seconds, then return the previous text. I have to run a method that takes a few seconds to get ready and would like to notify the user that it is running and this way it seemed simpler to me.

The form below does not work as expected:

jLabel1.setText("texto);
    
asked by anonymous 07.12.2015 / 23:49

1 answer

7

The question is old, but since nobody answered, I decided to leave a solution that I developed by chance.

You can do this text restoration using SwingWorker . . Remember that if this timeout is to run something slower, you can delegate to SwingWorker this activity too, within the doInBackground() method. Here's an example below, where I've 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);
    }
}

Running:

The SetTransientText() method receives temporary text and duration, in milliseconds. In the example, the text lasts 2 seconds.

Remembering that because SwinWorker runs on a Thread, the Thread.sleep does not crash the graphical interface.

    
26.10.2017 / 18:28