Results 1 to 5 of 5
  1. #1
    chirag123 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Post Saving JTextArea as a txt file

    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:

    Java 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);
    	}
    }
    this is the error that I'm getting:

    local variable inputroute is accessed from within inner class; needs to be declared final
    outFile.write(inputroute.getText());

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default

    That error is pretty self-descriptive. You can't access a variable in an outer class from an inner class unless that variable is declared final.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    chirag123 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    But how do you set a variable as final? had a look on google but couldn't find anything helpful

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    That's strange. I get all relevant hits on the first page when I search for 'java final variable'

    Additionally, JTextArea has a write(...) method inherited from JTextComponent which saves you some code and also makes the code more readable/maintainable.

    db

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

Similar Threads

  1. Saving a file in a gui
    By rav in forum AWT / Swing
    Replies: 9
    Last Post: 07-07-2010, 05:27 PM
  2. Saving JTextArea to .txt
    By AwesomeStorm in forum New To Java
    Replies: 8
    Last Post: 05-29-2010, 07:14 AM
  3. Saving to text file
    By Burton333 in forum New To Java
    Replies: 3
    Last Post: 04-28-2010, 08:05 PM
  4. Saving Several arraylists into one file?
    By Cemi in forum New To Java
    Replies: 2
    Last Post: 04-26-2010, 02:04 PM
  5. Replies: 8
    Last Post: 02-18-2010, 12:54 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •