title: Java: Using ActionListener, I need to get user input from a Text Field and use it, I'm having problems
body: <p>so what I need to do is use a JTextField to get user input in the form of a String, whilst I can do this part, I'm having trouble getting the String from the method to analyse it elsewhere in the program, here is what I have so far: </p>
  
  <pre><code> import javax.swing.* ;
   import java.awt.event.* ;
   import java.util.* ;
   class Games extends JFrame implements ActionListener
  {
    JPanel pnl = new JPanel() ;
    JTextField input = new JTextField(38) ;
    JTextArea output = new JTextArea(6, 37) ;
    JScrollPane pane = new JScrollPane(output) ;
    String inputString ;
  
  public Games()
  {
      super("Text Based RPG") ;
      setSize(600,200) ;
      setDefaultCloseOperation(EXIT_ON_CLOSE) ;
      output.setLineWrap(true) ;
      output.setWrapStyleWord(true) ;
      pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS) ;
      output.setEditable(false) ;
      pane.getVerticalScrollBar().setValue(100); //scrollbar always at bottom
      input.requestFocus() ;
  
      /*Edit: Should I call for the input string here, e.g. output.append(inputString)
       i get the compilation error message:
      Games.java:29: error: cannot find symbol
      output.append(inputString) ;
                    ^
      symbol:   variable inputString
      location: class Games
      */
  
      add(pnl) ;
      pnl.add(pane) ;
      pnl.add(input) ;
  
      input.addActionListener(this) ;
  
      setVisible(true) ;
  }
  
  public void actionPerformed(ActionEvent event) 
  {
      inputString = input.getText() ;
      input.setText("") ;
  }         
  /*The string i need to analyse is inputString, but I cannot, get it out of this method
    any help you guys can give me would be greatly appreciated. */
  
  public static void main(String[] args) 
  {
      Games gui = new Games() ;
  }
  }
  
  **********************************
      try
      {
          a = reader.readLine() ;
      }
      catch (IOException e ) 
      {
          System.out.println("An Input Error Has Occured") ;
      }
      if (a.indexOf("hide") != -1 ) switchvariable = 'b' ;
      if  ((a.indexOf("exit") != -1 ) || (a.indexOf("leave") != -1) || (a.indexOf("out") != -1)) switchvariable = 'a' ;
      if ((a.indexOf("exit") == -1) &amp;&amp; (a.indexOf("hide") == -1) &amp;&amp; (a.indexOf("leave") == -1) &amp;&amp; (a.indexOf("out") == -1)) switchvariable= 'c' ;
      String outcome = "" ;
      switch (switchvariable) 
      {
      case 'a' : outcome = "You exit the tavern to be discovered by\na group of bandits!\n" ; i = -1 ; break ;
      case 'b' : outcome = "You hide behind the bar for 10 minutes until\nyou can no longer hear the group." ; i = -1 ; break ;
      case 'c' : outcome = "You must choose between the two given options, exit the tavern, or hide." ; break ; // the variable i will still be greater
      //than 1 so the loop will continue until you reach an agreeable option, which in this case is either hiding, or addressing the group of people
      }
      System.out.println(outcome) ;           
  </code></pre>
  
  <p>What I would ideally like to do is use the textfield for user input, so what I need to do is put text in the output textarea, and then this should change according to the user input, an example of the code I would like to use is shown below the asterixes</p>