简体   繁体   中英

How to detect if a action listener has been triggered from another class?

I have a method which asks the user a question and in my GUI I have a JTextArea where they type in the answer. Since the method which asks the question is in a different class, how would I get the users answer to return to the method?

GUI ( JFrame class):

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GUI extends JFrame{

public GUI(String name) {
    super(name);

    //setSize(700,700);
    setLayout(new BorderLayout());
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    JPanel myPanel1 = new GUIPanel("Center");
    add(myPanel1, BorderLayout.CENTER);

    pack();
    setVisible(true);
    setResizable(true);
}

public static void main(String[] args) {
    GUI frame = new GUI("Game");
}
}

GUIPanel ( JPanel class):

package javaapplication2;

import java.awt.*;   
import javax.swing.*;

public class GUIPanel extends JPanel {

JLabel label1;
JTextField textField1;
JLabel label2;
JTextArea textArea1;

public GUIPanel(String position) {
    if (position.equals("Center")) {
        createCenterPanel();
    }
}

public void createCenterPanel() {
    setLayout(new GridLayout(2,2,10,10));

    label1 = new JLabel("Type Here: ");
    textField1 = new JTextField();
    textField1.setPreferredSize(new Dimension(110, 25));
    label2 = new JLabel("Game Output: ");
    textArea1 = new JTextArea();

    add(label1);
    add(textField1);
    add(label2);
    add(textArea1);
}
}

I have not yet added the method call or ActionListener class yet but basically it will call a method which will ask a question and then the user has to answer. This is what I am stuck on. I do not know how I can get the method to "detect" that the user has entered something and then retrieve the data.

I thought about making the textfield static and making a static boolean which gets changed to true when the action performed (user presses enter) gets triggered but that would mean the method would have to have a while loop to check so there must be a better way out there.

I'm not sure if this is what are you looking for because I don't know if i understood right what do you need but I tried

The only code i added was the method getTextField1Text() and called it inside createCenterPanel(), if you have anything to add please let me know, I hope this helps you a bit

    public void createCenterPanel() {
            setLayout(new GridLayout(2, 2, 10, 10));

            label1 = new JLabel("Type Here: ");
            textField1 = new JTextField();
            textField1.setPreferredSize(new Dimension(110, 25));
            label2 = new JLabel("Game Output: ");
            textArea1 = new JTextArea();
            getTextField1Text();

            add(label1);
            add(textField1);
            add(label2);
            add(textArea1);
        }

        public void getTextField1Text() {
            textField1.addActionListener(e -> {
                textArea1.setText(e.getActionCommand());
                textField1.setText("");
            });
        }

-------------------------------------------------------------

After discussing in comment section i tried to come up with something new(which i hope i did) to help you I added 2 methods to get the answer (NOTE: I used System.out.println(); only for testing)

These methods are added on GUIPanel class

public void getAnswer() {
    textArea1.addActionListener(e -> {
        textArea1.setText(e.getActionCommand());
        testAnswer(textArea1.getText());
    });
}

public String testAnswer(String s) {
    String saveParam = s;
    System.out.println(saveParam);
    return saveParam;
}

On GUI class i did some small changes so i can test if the methods is printing the answer,

            GUIPanel myPanel1 = new GUIPanel("Center");
            public GUI(String name) {
                super(name);

                //setSize(700,700);
                setLayout(new BorderLayout());
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                setLocationRelativeTo(null);

                add(myPanel1, BorderLayout.CENTER);


                pack();
                setVisible(true);
                setResizable(true);
            }

            public GUIPanel getMyPanel1() {
                return myPanel1;
            }

            public static void main(String[] args) {
                GUI frame = new GUI("Game");
                frame.getMyPanel1().getAnswer();

            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM