0

In trying to read the text that is entered into a textField, I used the actionlistener for a button right next to it. In this actionlistener class, I had an action performed method in which I created a string that was set equal to the textField.getText();. This class however has a problem recognizing textField variable from the previous class.

It is necessary for the .getText() or reading of the textField entry to be in the actionlistener class. I do not know what to try besides the code that I have listed down below.

public class MainClass {    
public static void main(String args[]) {
    JFrame frame = new JFrame ("Welcome");
    frame.setVisible(true);
    frame.setSize(500, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    frame.add(panel);

            JLabel label = new JLabel("...");
    panel.add(label);

    JTextField text = new JTextField(20);
    panel.add(text);

    JButton SubmitButton = new JButton("Analyze");
    panel.add(SubmitButton);
    SubmitButton.addActionListener(new Action1());
}
static class Action1 implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

    JFrame frame1 = new JFrame("Word Commonality");
    frame1.setVisible(true);
    frame1.setSize(500,200);
    String ReceivedPath = text.getText(); 
            System.out.println(ReceivedPath);

Error is present at second to bottom line of code. The error is "text cannot be resolved"

I expect that the text can be read and printed out in the console.

Abhir K
  • 81
  • 5
  • 1
    You're not creating your `text` variable anywhere in the `actionPerformed` method before trying to use it. Where is `text` supposed to be coming from? – Jordan Apr 17 '19 at 14:18
  • The text variable is for the TextField that is declared in the main method. If I am referring to that variable, how would I do it in the actionPerformed method? – Abhir K Apr 17 '19 at 14:20
  • You need to pass the text component as a parameter to your ActionListener since that class can't "See" the text component. –  Apr 17 '19 at 14:31

2 Answers2

0

If you place the getText() outside the ActionListener, it will be read immediately after creating the panel. That is why it is empty. You can make the ActionListener assign a value to a variable, but it will be empty until the action is performed.

Also see here: Swing GUI doesn't wait for user input

Mr. Wrong
  • 500
  • 7
  • 21
  • In response to Mr. Wrong - If I am to use the getText() outside the ActionListener, would I do it right under where I declare the textField that I am reading from? – Abhir K Apr 17 '19 at 14:24
  • Yes, but it will be empty when you call it. – Mr. Wrong Apr 17 '19 at 14:26
0

Your problem is revolved around function scoping to fix it you need a direct access to the JTextField object you can do so by instantiating a new action performed straight in the MainClass like this:

public class Main {
  public static void main(String args[]) {
    new MainClass();
  }
}

Here I created a class only used to instantiate the window class For the main class I suggest extending JFrame so you can inherit all of it methods.

//Imports
public class MainClass extends JFrame {
  private JPanel panel;
  private JLabel label;
  private JTextField text;
  private JButton SubmitButton;

  public MainClass(){
    super("Welcome");
    setSize(500, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel = new JPanel();
    add(panel);

    label = new JLabel("...");
    panel.add(label);

    text = new JTextField(20);
    panel.add(text);

    SubmitButton = new JButton("Analyze");
    panel.add(SubmitButton);
    SubmitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String ReceivedPath = text.getText(); 
                System.out.println(ReceivedPath);
            }
        });

   setVisible(true);
  }
}

This is how your class should look like. Side notes: Set visible is at the end otherwise the items will not be see able. The MainClass is inheriting from JFrame so it can use all its methods without instatiating it look at inheritance(https://www.w3schools.com/java/java_inheritance.asp) The action performed now can acces the text JTextField because it is a class attribute.

If the solution is correct please think of marking this answer as final. Thank you

Leonardo Drici
  • 749
  • 3
  • 11
  • 32
  • I understand the logic behind this approach. There seems to be a problem in the SubmitButton.addActionListener lines. I wrote it the same way that you have listed but i am getting this error: "The constructor JButton(new ActionListener(){}) is undefined" – Abhir K Apr 17 '19 at 15:13
  • The constructor JButton(new ActionListener(){}) is undefined – Abhir K Apr 17 '19 at 15:18
  • The above statement is the error that I got, do you need any other information? – Abhir K Apr 17 '19 at 15:28
  • Did you put the button listener in the addAction listener or in the constructor – Leonardo Drici Apr 17 '19 at 15:39
  • I put it under the constructor: public MainClass(). The addActionListener is also under this constructor. I have followed the code that you have posted. – Abhir K Apr 17 '19 at 15:46
  • All should be inside the constructor like I posted. Otherwise I need some time to run it. – Leonardo Drici Apr 17 '19 at 16:21
  • I ran how it was set up in the code above. Everything is under the constructor I think. – Abhir K Apr 17 '19 at 16:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191993/discussion-between-leonardo-drici-and-abhir-k). – Leonardo Drici Apr 17 '19 at 16:23