Temporary text in Jlabel

Asked

Viewed 645 times

4

I would like to exchange the text of a JLabel and leave it for just a few seconds, then go back to the previous text. I have to execute 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.

The way below does not work as expected:

jLabel1.setText("texto);
  • What do you mean? You want to change the text of a jlabel for a while and then go back to the previous text?

  • Exactly that !

  • 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.

  • 2

    Are you using swing? If so, you’ll probably need to use Swingworker

  • I’ll look into it, thanks for helping :D

  • It was a suggestion, but soon someone responds with a practical example.

  • 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)

  • 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.

Show 3 more comments

1 answer

6


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:

inserir a descrição da imagem aqui

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.