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

/**
 * set up and display another frame playing with
 * JTextField
 * 
 * @author Sharon Tuttle
 * @version 2015-09-21
 */

public class TextFieldPlay2
{
    /**
     * creates a frame for textfield play
     * 
     * @param args not used here
     */
    
    public static void main(String[] args)
    {   
        EventQueue.invokeLater(
            new Runnable()
            {
                public void run()
                {
                    TextFieldPlay2Frame aFrame = new TextFieldPlay2Frame();            
                    aFrame.setDefaultCloseOperation(
                        JFrame.EXIT_ON_CLOSE);
                    aFrame.setVisible(true);
                }
            });
    }
}

/**
 * a frame with some text field play
 */

class TextFieldPlay2Frame extends JFrame
{
    public static final int DEFAULT_WIDTH = 300;
    public static final int DEFAULT_HEIGHT = 150;
    
    /**
     * construct a TextFieldPlay2-panel frame instance
     */
    
    public TextFieldPlay2Frame()
    {
        this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        this.setTitle("JTextField Playing 2");
        
        // create and add a TextFieldPlay2 panel to my frame

        TextFieldPlay2Panel aTextFieldPlay2Panel = new TextFieldPlay2Panel();
        this.add(aTextFieldPlay2Panel);
    }
}

/**
 * a panel with textfield experimentation
 */

class TextFieldPlay2Panel extends JPanel
{
    // data fields
    
    private JTextField  visitorGreeting;
    private JTextField  visitorNameField;
    private JTextField  desiredSizeField;
    
    /**
     * constructs a TextFieldPlay2 panel instance
     */
    
    public TextFieldPlay2Panel()
    {
        // allow user to enter their name, and see a personalized
        //     greeting when they then click the Submit button

        JLabel visitorPrompt = new JLabel("Type your name:");
        visitorNameField = new JTextField(10);
        
        // ...but now, using an uneditable JTextField instead of a
        //     JLabel - so doesn’t resize when contents change, for example

        visitorGreeting = new JTextField("No visitor has registered yet");
        visitorGreeting.setEditable(false);
        visitorGreeting.setBackground(Color.LIGHT_GRAY);

        // also giving user a chance to give the greeting’s font size
        
        JLabel sizePrompt = new JLabel("Enter font size:");
        desiredSizeField = new JTextField("15", 3);
        
        JButton submitName = new JButton("Submit");
        submitName.addActionListener(new SubmitAction());
        
        this.add(visitorPrompt);
        this.add(visitorNameField);
        this.add(visitorGreeting);
        this.add(submitName);
        this.add(sizePrompt);
        this.add(desiredSizeField);
    }
    
    /**
     * an action listener that changes the greeting
     *    field to reflect the entered user's name,
     *    and its font size to the entered size
     */
    
    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() );

            // getText returns a String - but if you expect digits
            //    to be entered, can try parsing that String
            //    and converting it to int using Integer.parseInt
            
            visitorGreeting.setFont(new Font("Display", Font.PLAIN,
                                    Integer.parseInt(desiredSizeField.getText())));
        }
    }
    
}