Search code examples
javaswingmethodsjapplet

Initializing another method


I have a JSwing app that asks a user a question, responds to that question, and then asks another question. My issue is that after responding to the first question, the second question appears (from the actionPerformed method), but the next method (checker method), which is required to assign the new answer to the response variable and begin the if else statement, does not seem to initialize. Here is the full code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;

public class hello extends JApplet implements ActionListener{
JTextArea questions;
JTextField answers;
JPanel panel;
String response;

public void init(){

  questions = new JTextArea("Hello. State your name: ", 15, 65);
  questions.setEditable(false);
  questions.setLineWrap(true);
  questions.setWrapStyleWord(true);
  questions.setBackground(Color.black);
  questions.setForeground(Color.green);
  questions.setFont(new Font("Monaco", Font.PLAIN, 12));

  answers = new JTextField("Type here", 65);
  answers.setBackground(Color.black);
  answers.setForeground(Color.green);
  answers.setFont(new Font("Monaco", Font.PLAIN, 12));
  answers.addActionListener(this);

  panel = new JPanel();
  panel.add(questions);
  panel.add(answers);
  panel.setSize(480, 280);
  panel.setBackground(Color.black);

  getContentPane().add(panel, BorderLayout.CENTER);

  answers.addMouseListener(new MouseAdapter(){
        public void mouseClicked(MouseEvent e){
            answers.setText("");
        }
    });

}
  public void actionPerformed(ActionEvent e){
      response = answers.getText();
      questions.setText("How are you " + response + "?");
      answers.setText("");
  }
  public void checker(ActionEvent f){
    response = answers.getText();
    if(response.equals("well")){
    questions.setText("glad to hear it");
    }
    else{
      questions.setText("i'm sorry to hear that");
     }
    }
  }

Any suggestions would be much appreciated.


Solution

  • It seems you are confused as to how actionPerformed is being called and created a checker method that never really gets called.

    You've registered your hello class to implement the ActionListener interface.
    That is, by calling setActionListener(this) on your JTextField, actionPerformed will be called when the user presses enter.

    I'm assuming that you want the user to press enter a second time after entering "well" for instance and that checker will be called. However, your JTextField doesn't recognize checker nor is it interested in calling it.
    You could check to see if the user is answering the second question in your actionPerformed method; perhaps even create an enum to check the state of the current question.

    So something like:

    private enum Question { FIRST, SECOND };
    private Question current = FIRST; //can also initiate (but not declare) in init()
    ...
    public void actionPerformed(ActionEvent e) {
        if(current == FIRST) {
            questions.setText("How are you " + answers.getText() + "?");
            answers.setText("");
            current = SECOND;
        } else if(current == SECOND) {
            if(answers.getText().equals("well"))
                questions.setText("glad to hear it");
            else
                questions.setText("i'm sorry to hear that");
        }
    }