import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

/**
  a GUI application including a first example of a JTextField!

  @author Sharon Tuttle
  @version 2021-09-27
*/

public class TextField1
{
    /**
        creates a simple frame with a panel that includes a JTextField
        (along with a few other components)

        @param args not used here
     */

    public static void main(String[] args)
    {
	EventQueue.invokeLater(
	    () ->
	       {
		   TextField1Frame mainFrame = new TextField1Frame();
		   mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		   mainFrame.setVisible(true);
	       } );
    }
}

/**
     A frame with a panel that includes a JTextField
 */

class TextField1Frame extends JFrame
{
    // data fields

    private static final int DEFAULT_WIDTH = 435;
    private static final int DEFAULT_HEIGHT = 220;

    
    /**
        construct a TextField1Frame instance
    */

    public TextField1Frame()
    {
	setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
	setTitle("First JTextField Example");

	TextField1Panel tPanel = new TextField1Panel();
	add(tPanel);
    }
}

/**
    A panel containing several elements, including a JTextField
*/

class TextField1Panel extends JPanel
{
    // data fields!

    private JLabel     visitorGreeting;
    private JTextField visitorNameField;

    private static final Font BIG_FONT =
        new Font("SanSerif", Font.PLAIN, 30);

    /**
        constructs a TextField1Panel instance
    */

    public TextField1Panel()
    {
	// prompt the user for a name

	JLabel visitorPrompt = new JLabel("Type your name: ");
	visitorPrompt.setForeground(Color.BLUE);
	visitorPrompt.setFont(BIG_FONT);
	visitorPrompt.setBorder(new EtchedBorder());

	visitorNameField = new JTextField(10);
	visitorNameField.setFont(BIG_FONT);

	visitorGreeting = new JLabel("No visitor has registered yet");
	visitorGreeting.setForeground(Color.RED);
	visitorGreeting.setFont(BIG_FONT);

	JButton submitMe = new JButton("Submit");
	submitMe.setFont(BIG_FONT);

	// this JPanel is using the default layout for JPanels
	//    of FlowLayout -- add components in the order you
	//    want them to appear

	add(visitorPrompt);
	add(visitorNameField);
	add(visitorGreeting);
	add(submitMe);

	// oh yes, create a submission action!
        //   and add it to my submit button's list of listeners
	
	submitMe.addActionListener(new SubmitAction());
    }

    /**
        an action listener that changes the greeting label
        to reflect the entered visitor's name
    */

    private class SubmitAction implements ActionListener
    {
	// default constructor will suffice, in this case

        /**
            changes the greeting label to reflect the entered
            visitor's name

            @param event a push of the Submit button
	*/

	public void actionPerformed(ActionEvent event)
        {
	    visitorGreeting.setText("Hi, " +
				    visitorNameField.getText() );
	    visitorNameField.setColumns(2);
	    //visitorNameField.setEditable(false);
	}
    }
}