Java: Easier way to implement delay into a chat bot response system?

Viewed 93

I'm trying to get just a couple seconds of delay between user input and bot output; this is what I've got currently.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;


import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ChatBot extends JFrame {
    private JTextArea Chatarea = new JTextArea();
    private JTextField chatbox = new JTextField();
    
    public ChatBot () {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setLayout(null);
        frame.setSize(600, 600);
        frame.setTitle("ChatBot");
        frame.add(Chatarea);
        frame.add(chatbox);
        
        //Text Area
        Chatarea.setSize(500 , 450);
        Chatarea.setLocation(10, 5);
        
        //Text Field
        chatbox.setSize(510 , 30);
        chatbox.setLocation(10 , 500);
        
        chatbox.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String gtext = chatbox.getText();
                Chatarea.append("YOU: " + gtext + "\n");
                chatbox.setText("");
                
                if(gtext.contains("Hi")) {
                    new Timer().schedule(
                        new TimerTask() {
                            
                            @Override
                            public void run() {
                                     bot("Hello.");
                                 }   
                             }, 2000);
                        }   
                else {
                    new Timer().schedule(
                            new TimerTask() {
                                
                                @Override
                                public void run() {
                                    bot("What was that?");
                                }
                            }, 2000);
                        }
                    }
                }
            );}
    
    private void bot(String string) {
        Chatarea.append("Bot: " + string + "\n");
    }
    

    public static void main(String[] args) {
        new ChatBot();

    }

}

It works fine, but I'd rather have delay commands in one location and call it when needed, or just put the delay immediately after user input. I tried putting the timer into it's own function, but then I'm not sure how to get the chatbot's response into the public void run() {}, or if I'm even going about this the right way.

1 Answers

So you need a function void respondDelayed(String message, int delayMillis) ?

What about:

void respondDelayed(String message, int delayMillis) {
  new Timer().schedule(
    new TimerTask() {
      @Override
      public void run() { bot(message); }   
    }, delayInMillis);
  );
}
Related