Question

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.

Was it helpful?

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");
    }
}

OTHER TIPS

[...] 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.

Well you never call checker() method in your code then it's never executed :)

Some off-topic tips:

  • checker() method doesn't need an ActionEvent as argument, does it?
  • Maybe you would like to append the answers to the text area instead of override its text in every input from the user. Take a look to JTextArea.append() method.
  • About MouseListener attached to answers, if the goal is clear the text when user focuses this text field then I would suggest you attach a FocusListener and clear its text inside focusGained() method instead.
  panel = new JPanel();
  panel.setLayout(new BorderLayout(0, 0));
  panel.add(questions, BorderLayout.CENTER);
  panel.add(answers, BorderLayout.SOUTH);
  panel.setSize(480, 280);
  panel.setBackground(Color.black);

try this. The default layout of the panel is flow layout. And your answers and questions are too big to be shown. By setting the layout to borderLayout would adjust their sizes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top