title: Saving JTextArea as a txt file - final variable
body: <p>I need to save what the user enters into the JTextArea as a txt file when they press the save button but I keep getting an error message when I try to compile it, hope someone can help. Here's my code:</p>
  
  <pre><code>import java.awt.*;
  import javax.swing.*;
  import java.io.*;
  
  import java.awt.Dimension;
  import java.awt.Toolkit;
  import java.awt.Color;
  import java.awt.Font;
  import java.awt.Image;
  import java.awt.event.ActionListener;
  import java.awt.event.ActionEvent;
  import java.awt.event.*;
  
  class InputRoute extends JFrame
  {   
  InputRoute()
  {
      Container c = getContentPane();
      c.setLayout ( null );
  
      Color b = new Color(100,200,255); // set colour of JFrame
      c.setBackground( b );
  
      JLabel title = new JLabel("Input Route"); // new label
      title.setBounds(250, 20, 500, 30); // set position and size
      title.setForeground(Color.BLUE); // set colour of text to blue
      title.setFont(new Font("Time", Font.BOLD, 18)); // change font and size of text
      c.add(title);//adds object
  
      JLabel todo = new JLabel("Please enter your route below:"); // new label
      todo.setBounds(200, 40, 500, 30); // set position and size
      todo.setForeground(Color.BLUE); // set colour of text to blue
      todo.setFont(new Font("Time", Font.BOLD, 12)); // change font and size of text
      c.add(todo);//adds object
  
      JLabel eastmid = new JLabel("East Midlands Bus");//creates label
      eastmid.setBounds(245,400,500,30);//label location and size
      eastmid.setForeground(Color.BLUE);//colour of text
      eastmid.setFont(new Font("Time", Font.BOLD, 12));//font of text
      c.add(eastmid);//adds object
  
      JTextArea inputroute=new JTextArea("");//creates text field to enter route
      inputroute.setBounds(50, 75, 500, 250);//sets location and size
      inputroute.setEditable(true);//makes the field editable
      inputroute.setFont(new Font("Time", Font.PLAIN,12));//sets font of text
      inputroute.setBackground(null);//makes background transparents
      inputroute.setForeground(Color.BLUE);//sets colour of text
      inputroute.setBorder(BorderFactory.createEtchedBorder(3, Color.BLUE, Color.BLUE)); //sets border so text field can be seen
  
      JScrollPane scrollPane = new JScrollPane(inputroute);
      scrollPane.setBounds(50,75,500,250);
  
      JButton save = new JButton("SAVE ROUTE");//creates buttons
      save.addActionListener(new ActionListener()
      {
          public void actionPerformed(ActionEvent e)
          {
              String fileName = JOptionPane.showInputDialog("Enter file name");//String finalFileName = fileName.getText();
              FileWriter outFile = new FileWriter(fileName +".txt",true);
              outFile.write(inputroute.getText());
              outFile.close();
          }
      });
  
      JButton exit = new JButton("EXIT");
      exit.addActionListener(new ActionListener() 
      {
          public void actionPerformed(ActionEvent e)
          {
              dispose();
          }
      });
  
      exit.setForeground(Color.BLUE);//edits button
      exit.setFont(new Font("Time", Font.BOLD, 12));
      save.setForeground(Color.BLUE);//edits button
      save.setFont(new Font("Time", Font.BOLD, 12));
  
      c.add(exit);//adds objects
      c.add(save);
      c.add(scrollPane);
  
      exit.setBounds(250, 375, 90, 30);//sets location of button
      save.setBounds(230,340, 150,30);
  
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      this.setBounds((int) screenSize.getWidth()/2 - 370, (int) screenSize.getHeight()/2 - 300, 600, 450);  // set position and size
      this.setResizable(false);
      this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  
      this.setTitle("Admin");
      this.setVisible(true);
      this.setResizable(false);
  }
   }
  </code></pre>
  
  <p>this is the error that I'm getting:</p>
  
  <p>local variable inputroute is accessed from within inner class; needs to be declared final
  outFile.write(inputroute.getText());</p>
  
  <p>and I've looked on google as to how to do this but I've had no luck.</p>