0
lblCompThoughts.setText("this is the first text");
lblCompThoughts.setText("this is the second text");
lblCompThoughts.setText("this is the third text");

What happening in my code is that the third text is the only one printing. How to make the first text appear and then pause for 2 seconds and then the second text will appear then pause again for 2 seconds then display the third text.

Digvijaysinh Gohil
  • 1,367
  • 2
  • 15
  • 30
picolo
  • 41
  • 5

1 Answers1

1

A possible way to do that goes something like this:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.*;
import java.util.Timer;

import javax.swing.*;

public class StackOverflow {

private final List<JLabel> arrayList = Arrays.asList(new JLabel("This is the first text."), new JLabel("This is the second text."), new JLabel("And this is the third and last text."));
private JFrame frame;
private JButton button;

public static void main(final String[] arguments) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {    
    StackOverflow clazz = new StackOverflow();
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    clazz.create("Stackoverflow | Question", 350, 200);
    clazz.doLayout();
}

public void create(final String title, final int width, final int height) {
    this.frame = new JFrame(title);
    EventQueue.invokeLater(() -> {
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(width, height);
        frame.setLocationRelativeTo(null);
        frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
        frame.getContentPane().setBackground(Color.WHITE);
        frame.setVisible(true);
    });
}

public void doLayout() {
    this.button = new JButton("Answer question");
    EventQueue.invokeLater(() -> {
        button.addActionListener(new ActionListener() {
            int index = 0;
            @Override
            public void actionPerformed(final ActionEvent action) {
                // Important: This is the java.util.Timer and not the javax.swing.Timer class.
                Timer timer = new Timer();
                timer.scheduleAtFixedRate(new TimerTask() {
                    @Override
                    public void run() {
                        if (index + 1 >= arrayList.size()) {
                            timer.cancel();
                        }
                        EventQueue.invokeLater(() -> {
                            frame.getContentPane().add(arrayList.get((index += 1) - 1));
                            frame.getContentPane().validate();
                        });
                    }
                }, 0l, 2000l);
            }
        });
        frame.add(button);
    });
}
}

Hopefully, this answers your question. If it did, Show it to me with an upvote.

  • *"Show it to me with an upvote"* 1) The OP does not have enough reputation points to up-vote. 2) Swing components should be updated on the Event Dispatch Thread. By using a `javax.sing.Timer`, that happens automatically. – Andrew Thompson Dec 02 '17 at 13:12
  • @AndrewThompson You didn't even read my code. I am using the java.util.Timer and not the javax.swing.Timer class. –  Dec 02 '17 at 13:17
  • I did read your code before commenting & noticed that you were using the `java.util.Timer` that **does not** update Swing components on the EDT. That's why I ***made the comment!*** – Andrew Thompson Dec 02 '17 at 13:23
  • BTW - correcting a typo. in my first comment *"By using a `javax.sing.Timer`.."* should read *"By using a `javax.swing.Timer`.."* – Andrew Thompson Dec 02 '17 at 13:25
  • @AndrewThompson I put the `add(Component)` and `validate()` method in the Event Queue. –  Dec 02 '17 at 15:01
  • Yes, that's one way to fix the problem. (It's a shorter solution to use the Swing `Timer`.) Plus one for the edit. – Andrew Thompson Dec 02 '17 at 15:19
  • Oops! That **wasn't** an edit and I did not read your code **far enough.** My bad. – Andrew Thompson Dec 02 '17 at 15:21
  • 1
    No Problem, I misread your comment with the typo as well. I was thinking that you fixed a typo in **my** comment. (Did you vote for my answer?) °-° –  Dec 02 '17 at 15:23
  • *"I was thinking that you fixed a typo in my comment."* When you gain enough rep. it is possible to edit people's answers (without having to wait for others to agree to the edit). But nobody (to my knowledge) can **edit** other people's comments. We can only flag them as obsolete (or other reasons I forget because I never user them). *" (Did you vote for my answer?)"* Sure did! :) – Andrew Thompson Dec 02 '17 at 17:09