How to get string from JTextField and save it in variable?
<p>I am making a simple kid game that will ask the user to enter his/her name in a <code>JTextField</code> and that name will shown in other class after ending the game.</p>
<p>I made new object and used it to call the method <code>getName</code> but when I call the method it return <code>null</code></p>
<p>I want it to return the name that the user entered.</p>
<p>This is the code:</p>
<pre><code>package learn_englishTest;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Icon;
import javax.swing.ImageIcon;

public class Home extends JFrame{
    JTextArea welcome_txt,userName_txt;
    JTextField user_name;
    JLabel Background_lbl;
    JButton exit_btn,start_btn;
    JPanel panel;
    Icon Background_icon;
    String name;
    Font userName_font,welcome_font;

    public Home(){
        super(&quot;Easy Fun Learning&quot;);
        Container c =getContentPane();

        c.setLayout(new BorderLayout());

        panel =new JPanel(null);
        panel.setPreferredSize(new Dimension(650,470));
        
        welcome_txt=new JTextArea(&quot;Welcom to Easy Fun Learning &quot;);
        welcome_txt.setEditable(false);
        
         welcome_font = new Font(&quot;Verdana&quot;, Font.BOLD, 30);
         welcome_txt.setFont(welcome_font);
         welcome_txt.setForeground(Color.pink);
         welcome_txt.setBounds(80, 60, 500, 50);

        userName_font=new Font(&quot;Verdana&quot;,Font.BOLD,20);
        userName_txt=new JTextArea(&quot;Enter Your Name&quot;);
        userName_txt.setEditable(false);
        userName_txt.setFont(userName_font);
        userName_txt.setForeground(Color.BLUE);
        userName_txt.setBounds(350, 200, 200, 40);
        
        user_name=new JTextField(10);
        user_name.setBounds(400, 240, 100, 30);

        start_btn=new JButton(&quot;Start&quot;);
        start_btn.setBounds(480, 360, 100, 20);
        
        exit_btn=new JButton(&quot;Exit&quot;);
        exit_btn.setBounds(480, 390, 100, 20);
        
        Background_icon=new ImageIcon(getClass().getResource(&quot;art.png&quot;));
        Background_lbl=new JLabel(Background_icon);
        Background_lbl.setBounds(0, 80, 450, 450);
        
        panel.add(welcome_txt);
        panel.add(userName_txt);
        panel.add(user_name);
        panel.add(exit_btn);
        panel.add(start_btn);
        panel.add(Background_lbl);

        panel.setBackground(Color.WHITE);

    c.add(panel,BorderLayout.BEFORE_FIRST_LINE);
        
        ButtonHandler handler=new ButtonHandler();
        
        exit_btn.addActionListener(handler);
        start_btn.addActionListener(handler);
    }

    private class ButtonHandler implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
             if(e.getSource()==exit_btn)
                System.exit(0);
             if(e.getSource()==start_btn){
                 name=user_name.getText();
             
             List list=new List();
             list.setSize(700, 700);
             list.setVisible(true);
             list.setDefaultCloseOperation(EXIT_ON_CLOSE);
             Home.this.setVisible(false);
            }
        }
        }

    @Override
     public String getName(){
                  return name;
            }     
}
</code></pre>