Java Threading: make GUI wait before showing each message when receiving multiple messages instantly

Viewed 17

I would like to implement a frame where you can press a button to make a call, and then either the respondent, the manager or the director would respond to the call, and it would output on the screen who answered the call.

If an employee is not available, I would like to show on the console. So for example if respondent and manager are not available, I would like the output to look like this:

Respondent busy. Forwarding to manager.
Manager busy. Forwarding to Director.
Director answered the call.

With my implementations these messages are shown, but they are all showed instantly. I would like to set some waiting time between the messages, like:

shows message: "Manager is busy. Forwarding to director" waits 2 seconds shows message: "Director answered the call"

CallHandler.java:

import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public class CallHandler implements Runnable{

    private ArrayList<Employee> employees = new ArrayList<>();
    private MyFrame view;

    public CallHandler(){
        employees.add(new Respondent());

        employees.add(new Manager());

        employees.add(new Director());

        view = new MyFrame(this);
    }

    public void dispatchCall() throws InterruptedException {
        boolean dialing = true;
        while(dialing) {
            for (Employee employee : getAvailableEmployees()) {
                Status status = employee.call();
                if (status == Status.ANSWERED) {
                    (new Thread(employee)).start();
                    sendMessage(employee.getClass().getName() + " has answered the call.");
                    dialing = false;
                    break;
                }
                sendMessage(employee.getClass().getName() + " is busy. You will be redirected to the next available employee.");
            }
            if (dialing)
                sendMessage("No one is available at the moment. Please wait...");
        }
    }
    private void sendMessage(String msg) throws InterruptedException {
        view.addMessage(msg);
    }
    private ArrayList<Employee> getAvailableEmployees(){
        return employees.stream().filter(Employee::isFree).collect(Collectors.toCollection(ArrayList::new));
    }

    public static void main(String args[]){
        CallHandler handler = new CallHandler();
    }

    @Override
    public void run() {
        try{
            Thread.sleep(TimeUnit.SECONDS.toMillis(1));
        } catch (InterruptedException e) {
            System.out.println("Call got interrupted");
        }
    }
}

MyFrame.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class MyFrame extends JFrame implements Runnable {

    private static final int WIDTH = 800;
    private static final int HEIGHT = 500;
    private static final int PANEL_WIDTH = 600;
    private static final int PANEL_HEIGHT = 290;

    private final JPanel panel = new JPanel();
    private final ArrayList<JLabel> labels = new ArrayList<>();
    private int rowIndex = 0;
    private static final int ROWS = 17;
    private CallHandler handler = null;

    private Timer timer;
    MyFrame(CallHandler callHandler) {
        handler = callHandler;
        this.setLayout(null);
        setVisible(true);
        setSize(WIDTH, HEIGHT);

        add(panel);
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
        panel.setBackground(Color.GREEN);
        panel.setBounds(100, 50, PANEL_WIDTH, PANEL_HEIGHT);

        JButton btn = new JButton("Make call");
        btn.setBounds(350, 370, 100, 80);
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    handler.dispatchCall();
                } catch (InterruptedException ex) {
                    throw new RuntimeException(ex);
                }
            }

        });
        this.add(btn);
        initiateRows();

    }
    public void addMessage(String msg) throws InterruptedException {
        if (rowIndex < ROWS) {
            getLabel(rowIndex).setText(msg);
            rowIndex++;
        } else {
            System.out.println("removing first component");
            removeComponent(0);
            addRow(msg);
        }
        panel.updateUI();
//        Thread.sleep(1000);

    }

    private void initiateRows() {
        for (int i = 0; i < ROWS; i++) {
            addRow("");
        }
    }

    private void addRow(String msg) {
        JLabel label = new JLabel(msg);
        label.setAlignmentX(Component.CENTER_ALIGNMENT);
        labels.add(label);
        panel.add(label);
    }

    private JLabel getLabel(int i) {
        return (JLabel) panel.getComponent(i);
    }

    private void removeComponent(int i) {
        panel.remove(i);
    }

    public static void main(String args[]) {
        MyFrame f = new MyFrame(new CallHandler());
    }

    @Override
    public void run() {
        try{
            Thread.sleep(TimeUnit.SECONDS.toMillis(1));
        } catch (InterruptedException e) {
            System.out.println("Call got interrupted");
        }
    }
}

0 Answers
Related