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

/** 
   ReadTextArea reads text put into a box with a submit button 
   @author Fred Kral
   @version 1.0
*/
/* Help:
    http://www.tutorialspoint.com/swing/swing_jtextarea.htm
*/
 
public class ReadTextArea {
    
   public JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;
   
   private String userInput = "";


   public ReadTextArea(String title){
   
      mainFrame = new JFrame(title);
      mainFrame.setSize(400,300);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(
            new WindowAdapter() {
               public void windowClosing(WindowEvent windowEvent){
                  System.exit(0);
               }        
            });    
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("", JLabel.CENTER);    
   
      statusLabel.setSize(350,100);
   
      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   public void showTextArea(String header){
      headerLabel.setText(header); 
      final JTextArea commentTextArea = new JTextArea("",5,10);// int rows, int columns
      JScrollPane scrollPane = new JScrollPane(commentTextArea);     
      JButton showButton = new JButton("Submit");
      showButton.addActionListener(
            new ActionListener() {
               public void actionPerformed(ActionEvent e) { 
                  userInput = commentTextArea.getText() ;
                  statusLabel.setText( userInput );      
               }
            }); 
      controlPanel.add(scrollPane);        
      controlPanel.add(showButton);
      mainFrame.setVisible(true);  
   }
   /** 
     getUserInput gets the latest user text input after most recent submit 
   */
   public String getUserInput(){
      return userInput;
   }
   
}