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