0

I am fairly new to Java and just wanted to ask about ActionListener method. I've created a GUI and in one panel I want to ask the user to input values of x and press submit. It looks like this: f(x)= [input field] - [input field] ^2 (submit button) I am lost and don't know what to put in ActionPerformed method to get the values that user inputs (also the method in which I created the panel, text fields etc is private if that's relevant )

I have already tried x1.getText(), but it seems like it can't access the variable as the JPanel method is private, and ActionPerformed is public


private JPanel panel2() 
    {    
        inputPanel.setLayout(new FlowLayout());

        JTextField  x1 = new JTextField();
        JTextField  x2 = new JTextField();

        JLabel f = new JLabel ("F(x)= ");
        JLabel f2= new JLabel (" - ");
        JLabel f3 = new JLabel (" ^2 ");
        JButton submit1 = new JButton("Submit values");

        submit1.addActionListener(this);

        inputPanel.add(f);
        inputPanel.add(x1);
        inputPanel.add(f2);
        inputPanel.add(x2);
        inputPanel.add(f3);
        inputPanel.add(submit1);
      }
    {
        if("submit1".equals(e.getActionCommand()))
        {
           // and that's where I get lost

        }
    } 
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
guilt
  • 49
  • 7
  • Does it need to be private? – Thiago E S Oct 21 '19 at 20:42
  • @ThiagoEliasSantos I've made it private because I call it straight from constructor to create panel1 (one of the panels in tabbed pane ) I've wanted to create a different class for each pane but didn't know how to do it as I would get the error about static, non-static context. So I thought I'll get the input values in main class and do the calculation in a different class, at least it sounded easier in my head! – guilt Oct 21 '19 at 20:49
  • Are both your code snippets from the same class? If so, the `private` access modifier doesn't matter. Your problem description is vague. Don't guess about what's causing the problem when you're describing it to others. Instead, *just describe what happens*. When you try `x1.getText()`, what happens *exactly*. – MarsAtomic Oct 21 '19 at 20:51
  • @MarsAtomic I get the error that symbol cannot be found and yes both snippets are from the same class – guilt Oct 21 '19 at 20:54
  • *What* symbol? Please don't beat around the bush and just post the error message in full. When you're asking people for help, it's best to anticipate what they're going to need to help you. Be complete and be precise. Remember, you're not paying us to do this -- don't make it harder than it needs to be. – MarsAtomic Oct 21 '19 at 20:57
  • the message says: Cannot find symbol Symbol: variable x1 – guilt Oct 21 '19 at 21:03
  • Just declare your textfields outside the panel, just inside the class declaration. If you'd posted the entire class from the start, your structure would have been clear and the answer would have been immediate without all the back and forth. – MarsAtomic Oct 21 '19 at 21:04

2 Answers2

0

I am inferring by your discription that panel2 is a method and both JTextfields x1 and x2 are local variables of method panel2 which won't be accessible outside it.

You will need to declare the x1 and x2 globally and if you want them to be private, associate getters and setters with them and use it in the actionperformed method.

Shubham Saraswat
  • 559
  • 4
  • 11
0
  1. {} signs represent code blocks and if you define any object in the inside-block you could not directly access it.
  2. Also, you could not extract the value of x1 or x2 directly from the event of a button.

If you not gonna change No.1, you can define an inline function inside of the method, as you can see below:

submit1.addActionListener(e -> {
        System.err.println("x1 : " + x1.getText());
        System.err.println("x2 : " + x2.getText());
    });

Requires Java 1.8+

Onurus
  • 54
  • 1
  • 4