Results 1 to 5 of 5
Thread: Saving JTextArea as a txt file
- 04-28-2011, 03:49 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
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:
this is the error that I'm getting: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); } }
local variable inputroute is accessed from within inner class; needs to be declared final
outFile.write(inputroute.getText());
- 04-28-2011, 04:01 PM #2
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!
- 04-28-2011, 04:07 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
But how do you set a variable as final? had a look on google but couldn't find anything helpful
- 04-28-2011, 04:37 PM #4
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
- 04-28-2011, 04:39 PM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Similar Threads
-
Saving a file in a gui
By rav in forum AWT / SwingReplies: 9Last Post: 07-07-2010, 05:27 PM -
Saving JTextArea to .txt
By AwesomeStorm in forum New To JavaReplies: 8Last Post: 05-29-2010, 07:14 AM -
Saving to text file
By Burton333 in forum New To JavaReplies: 3Last Post: 04-28-2010, 08:05 PM -
Saving Several arraylists into one file?
By Cemi in forum New To JavaReplies: 2Last Post: 04-26-2010, 02:04 PM -
Sending a File from Server to Client and saving the file to Clients computer
By al_Marshy_1981 in forum NetworkingReplies: 8Last Post: 02-18-2010, 12:54 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks