title: Reading a value from Textfield
body: <p>I want to read a value from textfied in Java but I am not able to read it
  Here is my code</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*; 
  import java.awt.event.ActionListener;
  import java.awt.event.*;
  
  public class TextField extends JDialog {
    TextField() {
      JFrame frm = new JFrame("SAMPLE PROGRAM");
      frm.setBounds(150,150,420,400);
      frm.setLayout(null);
      Container content = frm.getContentPane();
      content.setBackground(Color.cyan);
      JTextField text = new JTextField();
      text.setBounds(70,25,100,30);
      JButton button1, button2; 
      button1 = new JButton("PROGRAMMER");
      button2 = new JButton("USER");
      button1.setBounds(270,25,120,50);
      button2.setBounds(270,90,120,50);
      button1.addActionListener(new ButtonHandler());
      button2.addActionListener(new ButtonHandler());
      frm.add(button1);
      frm.add(button2);
      frm.add(text);
      frm.setVisible(true);
      frm.setResizable(false);
    }
  
    public static void main(String[] args) {
      new TextField();
    }
    class ButtonHandler implements ActionListener { 
      public void actionPerformed(ActionEvent e) {     
        String str = new String();     
        str = e.getActionCommand();    
        System.out.println(" " + str);
      }
    }  
  }
  </code></pre>
  
  <p>I tried the following methods</p>
  
  <p>1.In the Class Textfield i used this method under button2.addactionlistener.It gave an error</p>
  
  <p>Cannot refer to a non-final variable text inside an inner class defined in a different method</p>
  
  <pre><code>button1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {  
      if(text.getText().equals("joe")) 
  </code></pre>
  
  <p>2.In the class ButtonHandler</p>
  
  <p>it says that text cannot be resolved</p>
  
  <p><strong>What Method should i use to read the textfield and in which class should in read it</strong> </p>