addActionListener to save file

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 843 Views - Last Post: 03 May 2011 - 02:47 PM Rate Topic: -----

#1 ralzari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-October 10

addActionListener to save file

Posted 25 April 2011 - 01:47 PM

Hi All,

I'm new to Java but i'm writing a complex code for my final university project which consists of multiple tabs using JTabbedPane, each tab has a different application.

One of the applications is a simple text editor which can load files and save to file. Loading a file is working properly but when i try to save to file, it doesn't seem to save anything.

Can anyone expert please look into the actionListener code and tell me what's wrong with it?

		jp3_button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				JFileChooser chooser = new JFileChooser();
				int option = chooser.showSaveDialog(myFinalPro.this);
				if (option == JFileChooser.APPROVE_OPTION) {
					input.setText("You saved " + ((chooser.getSelectedFile()!=null)?chooser.getSelectedFile().getName():"nothing"));
				}
				else {
					input.setText("You canceled.");
				}
			}
		});



Thanx

This post has been edited by ralzari: 25 April 2011 - 01:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: addActionListener to save file

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,840
  • Joined: 05-April 11

Re: addActionListener to save file

Posted 25 April 2011 - 02:21 PM

I dont see you trying to save anything. All you are doing is checking if he clicked the save button?
Was This Post Helpful? 0
  • +
  • -

#3 ralzari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-October 10

Re: addActionListener to save file

Posted 25 April 2011 - 02:26 PM

what shall i add to this code to be able to save a file?

i know i should include
write();
close(file);



but i'm very comfused and don't know exactley how and where to write them

This post has been edited by ralzari: 25 April 2011 - 02:30 PM

Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,840
  • Joined: 05-April 11

Re: addActionListener to save file

Posted 25 April 2011 - 02:31 PM

View Postralzari, on 25 April 2011 - 02:26 PM, said:

what shall i add to this code to be able to save a file?


You have a File named myFile, and then you will need a FileWriter to be able to write to that file.
Was This Post Helpful? 0
  • +
  • -

#5 ralzari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-October 10

Re: addActionListener to save file

Posted 25 April 2011 - 02:36 PM

View PostCasiOo, on 25 April 2011 - 02:31 PM, said:

View Postralzari, on 25 April 2011 - 02:26 PM, said:

what shall i add to this code to be able to save a file?


You have a File named myFile, and then you will need a FileWriter to be able to write to that file.



Thank you CasiOo, i will give it a try and see what i come up with.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,611
  • Joined: 27-December 08

Re: addActionListener to save file

Posted 25 April 2011 - 02:37 PM

Check out this File I/O tutorial.
Was This Post Helpful? 0
  • +
  • -

#7 ralzari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-October 10

Re: addActionListener to save file

Posted 25 April 2011 - 03:02 PM

View Postmacosxnerd101, on 25 April 2011 - 02:37 PM, said:




Thank you macosxnerd101, i'm reading through the tutorial now and will see what i come up with. :whistling:
Was This Post Helpful? 0
  • +
  • -

#8 ralzari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-October 10

Re: addActionListener to save file

Posted 01 May 2011 - 04:38 PM

Ok i was able to put some code together, now my application is generating the file. But how can i modify the code to get whatever is written in the JTextField into the file?

		jp3_button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				JFileChooser chooser = new JFileChooser();
				
				chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				
				int intReturnFromJFCWrite = chooser.showSaveDialog(null);
				
				if (intReturnFromJFCWrite == JFileChooser.APPROVE_OPTION)
					try{
						{
							fileNameOfWrite = chooser.getSelectedFile();
							input.setText("You chose " + fileNameOfWrite);

							FileOutputStream fosOut = new FileOutputStream(fileNameOfWrite, true);
							PrintWriter pwOut = new PrintWriter(fosOut);            
					                      
							pwOut.println(input.getText());
					           
							input.append("\nYou are saving the following fields:" + "\n" + input.getText());  
					          
							pwOut.close();
						}
					}
					catch (IOException ioEx) {
							JOptionPane.showMessageDialog(null, "Error!");
						}
					
					if(intReturnFromJFCWrite == JFileChooser.CANCEL_OPTION || intReturnFromJFCWrite == JFileChooser.ERROR_OPTION) 
					{ 
						input.setText("You either cancelled " + "or had an error, " + "\nwith a Save JFileChooser");
					}
			}
		});


Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,611
  • Joined: 27-December 08

Re: addActionListener to save file

Posted 01 May 2011 - 09:10 PM

What's going on with these two lines? Don't you do that here?
pwOut.println(input.getText());  

input.append("\nYou are saving the following fields:" + "\n" + input.getText());  


Was This Post Helpful? 0
  • +
  • -

#10 ralzari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-October 10

Re: addActionListener to save file

Posted 02 May 2011 - 11:27 AM

View Postmacosxnerd101, on 01 May 2011 - 09:10 PM, said:

What's going on with these two lines? Don't you do that here?
pwOut.println(input.getText());  

input.append("\nYou are saving the following fields:" + "\n" + input.getText());  




I tried playing around with the above code but what i'm getting in the generated file is from this code. Something should be removed and re-ordered but what? that's what i'm trying to figure out.
input.append("\nYou are saving the following fields:" + "\n" + input.getText());  


Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,611
  • Joined: 27-December 08

Re: addActionListener to save file

Posted 02 May 2011 - 11:34 AM

Why don't you just write to the File: ("You are saving the following fields:" + "\n" + input.getText()? Why do you need to update the JTextArea?
Was This Post Helpful? 0
  • +
  • -

#12 ralzari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-October 10

Re: addActionListener to save file

Posted 02 May 2011 - 12:34 PM

I need to make sure that my file is written, that's why i'm updating the TextField with a statement.
What i don't understand is how to grab whatever is written in a textfield into a file.. that's my only issue. Everyone is doing it on their own way, that's why it got a little bit confusing. I'm taking the challenge to figure it out myself. Let me give it another try and see where i reach.

Thanx macosxnerd101
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2556
  • View blog
  • Posts: 10,664
  • Joined: 20-September 08

Re: addActionListener to save file

Posted 02 May 2011 - 12:48 PM

You can simplify that a bit

Writer out = new FileWriter(fileNameOfWrite);
input.write(out);
out.close();

Was This Post Helpful? 0
  • +
  • -

#14 IamTw_  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 238
  • Joined: 25-February 11

Re: addActionListener to save file

Posted 02 May 2011 - 01:12 PM

Hi ralzari

Some time ago I found a tutorial about how to read and write to a file. I thought I had put it into my favorites, but it's not there, and I can't find it. But it sure would had been a very good one for you to read, it was short and precise.

Anyway. I have made an example about your issue. This writes to a file, this will give you an idea of what's going on.

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class WriteToFile {

	private BufferedWriter writer = null;
	
	public WriteToFile()
	{
		try{
			writer = new BufferedWriter(new FileWriter("FileName.txt", true));
			writer.write("This is a test");

			writer.newLine();
			writer.close();
			
		} catch(IOException e){
			System.out.println("Could not write to file");
		}
		
	}
	
	public static void main(String[] args) {
		
		WriteToFile wtf = new WriteToFile();
	}

}


Was This Post Helpful? 1
  • +
  • -

#15 ralzari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 18-October 10

Re: addActionListener to save file

Posted 03 May 2011 - 10:31 AM

View PostIamTw_, on 02 May 2011 - 01:12 PM, said:

Hi ralzari

Some time ago I found a tutorial about how to read and write to a file. I thought I had put it into my favorites, but it's not there, and I can't find it. But it sure would had been a very good one for you to read, it was short and precise.

Anyway. I have made an example about your issue. This writes to a file, this will give you an idea of what's going on.

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class WriteToFile {

	private BufferedWriter writer = null;
	
	public WriteToFile()
	{
		try{
			writer = new BufferedWriter(new FileWriter("FileName.txt", true));
			writer.write("This is a test");

			writer.newLine();
			writer.close();
			
		} catch(IOException e){
			System.out.println("Could not write to file");
		}
		
	}
	
	public static void main(String[] args) {
		
		WriteToFile wtf = new WriteToFile();
	}

}




Thanks a million IamTw, really appreciate it. i'll implement what you wrote into my program. I'm taking my second class in Java but because i fell in love with the langauge i'm reading out of the supposed material and learning more what's even being taught in that class.
Thanks again and god bless


Thank you all guys, i'll implement your suggestions and hopefully get it done.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2