0

I'm looking for an simple example to explain how read a simple value from a text field for using in my program. I saw a lot of complicate examples with a lot of many lines but, unfortunately, I haven't found a simple example to clear me. I have an simple user interface: a text field and a button. I type something in text field and after click on button I want that variable "a" declared first of program to have value from text field. I wrote a program but I got an error massage "local variables from an inner class must be final", please can somebody to help me?

package arbitru;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Listen1 {
    public static void main(String[] args) {
        
        String a; //variable 
        
        JFrame f=new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(new Dimension(300,300));
        
        JPanel panou=new JPanel();
        panou.setLayout(new FlowLayout());
        
        JLabel leb=new JLabel("Message");
        JButton butt=new JButton("Record");
        JTextField text=new JTextField(20);
        
        panou.add(leb);
        panou.add(text);
        panou.add(butt);
        
        butt.addActionListener(new ActionListener(){
        @Override
            public void actionPerformed(ActionEvent e){
                a=text.getText();
            }
            });
        
        System.out.println(a);
        
        f.getContentPane().add(panou); 
        f.setVisible(true);
        
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    The start to this problem is trying to shove everything into the `main` method. Make a constructor for the bulk of it. Make the `a` field a class attribute. – Andrew Thompson Jul 22 '20 at 13:13
  • *"I can't explain why?"* Adding a '?' to a statement does not make it a question, in much the same way that expanding on your question does not make it an answer! Use the [edit] link (below left of the actual question area) for updates. – Andrew Thompson Jul 23 '20 at 01:42

2 Answers2

-1
  1. you cant use 20 like this

     JTextField text=new JTextField(20);
    

you need first to cal the constructor and then set the text field to "20"

        JTextField text=new JTextField();
        text.setText("20");

apprently you need to make a into String a into a final one element array heres an explantion:

Why should I "Transform myVariable to final one element array"?

Lambdas: local variables need final, instance variables don't

   public static void main(String[] args) {

             final String[] a = new String[1]; //variable

            JFrame f=new JFrame("Test");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setSize(new Dimension(300,300));

            JPanel panou=new JPanel();
            panou.setLayout(new FlowLayout());

            JLabel leb=new JLabel("Message");
            JButton butt=new JButton("Record");
            JTextField text=new JTextField();
            text.setText("20");

            panou.add(leb);
            panou.add(text);
            panou.add(butt);

            butt.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e){
                   a[0] =  text.getText();
                    System.out.println( text.getText());

                }
            });


            f.getContentPane().add(panou);
            f.setVisible(true);

        }
    }

p.s: If you are new to java gui,I would reccomnd using netBeans

Omer T
  • 1
  • 1
  • Don't work, after closing Swing windows I want to appear new value for a variable, I give System.out.println(a) command, but don't appear a value in Output of my program. – Mitu Gabriel Jul 22 '20 at 13:01
  • 3
    *"you cant use 20 like this"* Of course you [can](https://docs.oracle.com/en/java/javase/13/docs/api/java.desktop/javax/swing/JTextField.html#(int))! *"If you are new to java gui,I would reccomnd using netBeans"* If you're new to (programming or) answering questions, I suggest you use the API docs. – Andrew Thompson Jul 22 '20 at 13:01
-1

Your code looks fine, but you can not assign the variable a in the scope of that action listener.

A quick fix:

public class Listen1 {

    private static String a; // moved the variable here 

    public static void main(String[] args) {
        
       // ... same code as before apart from the variable
        
    }
}

Also that system.out is gonna print null always. If you want to print out the value when pressing the button; then move the System.print to the actionlistener

  • 1
    *`private static String a;`* Unless you can explain the design logic (/ necessity) of using `static` you're *misusing* it. Don't. Do. That. – Andrew Thompson Jul 23 '20 at 01:45
  • Like I said, the code is a quick workaround, to access the variable from within the main block which is static, the variable needs to be static. I'm not saying this is a good design, that's a different story... – LasagnaCode Jul 30 '20 at 17:53