3 Replies - 1453 Views - Last Post: 07 December 2012 - 03:51 PM Rate Topic: -----

#1 3xce55  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-December 12

Text Editor Not Saving Multiple Lines

Posted 06 December 2012 - 10:18 PM

So I made this text editor that can open files with multiple lines correctly, but it can't save to multiple lines? This has bugged me for a while. Heres the code:

package com.excess.texteditor;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.Scanner;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Frame extends JFrame {

	public JMenuBar menu;

	public JMenu file;
	public JMenu edit;
	public JMenu font;
	public JMenu fontDecor;
	public JMenu fontSize;
	public JMenu help;

	public JMenuItem fileNew;
	public JMenuItem fileOpen;
	public JMenuItem fileSave;
	public JMenuItem fileSaveAs;
	public JMenuItem fileExit;

	public JMenuItem editToggleEditable;

	public JMenuItem fontArial;
	public JMenuItem fontSerif;
	public JMenuItem fontComicSansMS;
	public JMenuItem fontConsolas;

	public JMenuItem fontDecorPlain;
	public JMenuItem fontDecorBold;
	public JMenuItem fontDecorItalic;
	public JMenuItem fontDecorBoldItalic;

	public JMenuItem fontSizeSmall;
	public JMenuItem fontSizeNormal;
	public JMenuItem fontSizeBig;
	public JMenuItem fontSizeHuge;

	public JMenuItem helpAbout;
	public JMenuItem helpHelp;

	public JTextArea field;

	public JScrollPane scroller;

	public JLabel status;

	public String fontF = "Arial";
	public int fontD = Font.PLAIN;
	public int fontS = 14;

	public Font origFont;

	public JFileChooser fileSelect;

	public File currentFile;
	
	public AboutFrame aboutF;
	public HelpFrame helpF;

	public Frame() {
		menu = new JMenuBar();
		field = new JTextArea();
		status = new JLabel("Welcome to Tyler's Text Editor");
		fileSelect = new JFileChooser();

		origFont = (new Font(fontF, fontD, fontS));
		field.setFont(origFont);

		file = new JMenu("File");
		edit = new JMenu("Edit");
		font = new JMenu("Font");
		fontDecor = new JMenu("Font Decorations");
		fontSize = new JMenu("Font Size");
		help = new JMenu("Help");

		fileNew = new JMenuItem("New");
		fileOpen = new JMenuItem("Open...");
		fileSave = new JMenuItem("Save");
		fileSaveAs = new JMenuItem("Save As...");
		fileExit = new JMenuItem("Exit");

		file.add(fileNew);
		file.add(fileOpen);
		file.add(fileSave);
		file.add(fileSaveAs);
		file.add(fileExit);

		editToggleEditable = new JMenuItem("Toggle Editable...");

		edit.add(editToggleEditable);

		fontArial = new JMenuItem("Arial");
		fontSerif = new JMenuItem("Serif");
		fontComicSansMS = new JMenuItem("Comic Sans MS");
		fontConsolas = new JMenuItem("Consolas");

		font.add(fontArial);
		font.add(fontSerif);
		font.add(fontComicSansMS);
		font.add(fontConsolas);

		fontDecorPlain = new JMenuItem("Plain");
		fontDecorBold = new JMenuItem("Bold");
		fontDecorItalic = new JMenuItem("Italic");
		fontDecorBoldItalic = new JMenuItem("Bold & Italic");

		fontDecor.add(fontDecorPlain);
		fontDecor.add(fontDecorBold);
		fontDecor.add(fontDecorItalic);
		fontDecor.add(fontDecorBoldItalic);

		fontSizeSmall = new JMenuItem("Small");
		fontSizeNormal = new JMenuItem("Normal");
		fontSizeBig = new JMenuItem("Big");
		fontSizeHuge = new JMenuItem("Huge");

		fontSize.add(fontSizeSmall);
		fontSize.add(fontSizeNormal);
		fontSize.add(fontSizeBig);
		fontSize.add(fontSizeHuge);

		helpAbout = new JMenuItem("About...");
		helpHelp = new JMenuItem("Help...");

		help.add(helpAbout);
		help.add(helpHelp);

		menu.add(file);
		menu.add(edit);
		menu.add(font);
		menu.add(fontDecor);
		menu.add(fontSize);
		menu.add(help);

		add(menu, BorderLayout.NORTH);

		field.setLineWrap(true);
		field.setWrapStyleWord(true);
		scroller = new JScrollPane(field);
		add(scroller, BorderLayout.CENTER);
		
		add(status, BorderLayout.SOUTH);

		fileNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (JOptionPane.showConfirmDialog(null, "Are you sure you want to create a new file?") == 0) {
					field.setText("");
					status.setText("Generated New File...");
				}
			}
		});

		fileOpen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (JOptionPane.showConfirmDialog(null, "Are you sure you wish to trash the current file?") == 0) {
					int openResult = fileSelect.showOpenDialog(null);
					if (openResult == fileSelect.APPROVE_OPTION) {
						openFile(fileSelect.getSelectedFile());
						status.setText("Opened File...");
					}
				}
			}
		});
		
		fileSave.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(currentFile == null){
					int saveResult = fileSelect.showSaveDialog(null);
					if(saveResult == fileSelect.APPROVE_OPTION){
						saveFile(fileSelect.getSelectedFile(), field.getText());
					} 
				} else {
					saveFile(currentFile, field.getText());
				}
				status.setText("File Saved...");
			}
		});
		
		fileSaveAs.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int saveResult = fileSelect.showSaveDialog(null);
				saveFile(fileSelect.getSelectedFile(), field.getText());
				status.setText("File Saved...");
			}
		});
		
		fileExit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(JOptionPane.showConfirmDialog(null, "Are you sure you wish to exit without saving?") == 0){
					closeWindow();
				}
			}
		});
		
		editToggleEditable.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(field.isEditable()){
					field.setEditable(false);
					status.setText("Field is No Longer Editable...");
				} else if(!field.isEditable()){
					field.setEditable(true);
					status.setText("Field is Now Editable...");
				}
			}
		});
		
		fontArial.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				fontF = "Arial";
				field.setFont(new Font(fontF, fontD, fontS));
				status.setText("Font Changed to Arial...");
			}
		});
		
		fontSerif.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				fontF = "Serif";
				field.setFont(new Font(fontF, fontD, fontS));
				status.setText("Font Changed to Serif...");
			}
		});
		
		fontComicSansMS.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				fontF = "Comic Sans MS";
				field.setFont(new Font(fontF, fontD, fontS));
				status.setText("Font Changed to Comic Sans MS...");
			}
		});
		
		fontConsolas.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				fontF = "Consolas";
				field.setFont(new Font(fontF, fontD, fontS));
				status.setText("Font Changed to Consolas...");
			}
		});
		
		fontDecorPlain.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				fontD = Font.PLAIN;
				field.setFont(new Font(fontF, fontD, fontS));
				status.setText("Font Decoration Set to Plain...");
			}
		});
		
		fontDecorBold.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				fontD = Font.BOLD;
				field.setFont(new Font(fontF, fontD, fontS));
				status.setText("Font Decoration Set to Bold...");
			}
		});
		
		fontDecorItalic.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				fontD = Font.ITALIC;
				field.setFont(new Font(fontF, fontD, fontS));
				status.setText("Font Decoration Set to Italic...");
			}
		});
		
		fontDecorBoldItalic.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				fontD = Font.BOLD + Font.ITALIC;
				field.setFont(new Font(fontF, fontD, fontS));
				status.setText("Font Decoration Set to Bold & Italic...");
			}
		});
		
		fontSizeSmall.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				fontS = 7;
				field.setFont(new Font(fontF, fontD, fontS));
				status.setText("Font Size Set to Small...");
			}
		});
		
		fontSizeNormal.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				fontS = 14;
				field.setFont(new Font(fontF, fontD, fontS));
				status.setText("Font Size Set to Normal...");
			}
		});
		
		fontSizeBig.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				fontS = 21;
				field.setFont(new Font(fontF, fontD, fontS));
				status.setText("Font Size Set to Big...");
			}
		});
		
		fontSizeHuge.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				fontS = 28;
				field.setFont(new Font(fontF, fontD, fontS));
				status.setText("Font Size Set to HUGE (THAT'S WHAT SHE SAID)...");
			}
		});
		
		helpAbout.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				aboutF = new AboutFrame();
				
				aboutF.setSize(600, 400);
				aboutF.setVisible(true);
				aboutF.setResizable(false);
				aboutF.setLocationRelativeTo(null);
			}
		});
		
		helpHelp.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				helpF = new HelpFrame();
				
				helpF.setSize(600, 400);
				helpF.setVisible(true);
				helpF.setResizable(false);
				helpF.setLocationRelativeTo(null);
			}
		});
	}

	public void openFile(File file) {
		if (file.canRead()) {
			String filePath = file.getPath();
			String fileContents = "";

			if (filePath.endsWith(".txt")) {
				try {
					Scanner scan = new Scanner(new FileInputStream(file));
					while (scan.hasNextLine()) {
						fileContents += scan.nextLine();
					}

					scan.close();
				} catch (FileNotFoundException e) {

				}

				field.setText(fileContents);
				setTitle("Tyler's Text Editor - " + filePath);
				currentFile = file;
			} else {
				JOptionPane.showMessageDialog(null, "That file type is not supported!\nOnly .txt file type is supported!");
			}
		} else {
			JOptionPane.showMessageDialog(null, "Could not open file...");
		}
	}
	
	public void saveFile(File file, String contents){
		BufferedWriter writer = null;
		String filePath = file.getPath();
		if(!filePath.endsWith(".txt")){
			filePath += ".txt";
		}
		
		try{
			writer = new BufferedWriter(new FileWriter(filePath));
			writer.write(contents);
			writer.close();
			field.setText(contents);
			setTitle("Tyler's Text Editor - " + filePath);
			currentFile = file;
		} catch (Exception e){
			
		}
	}
	
	public void closeWindow(){
		WindowEvent close = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
		Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(close);
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Text Editor Not Saving Multiple Lines

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Text Editor Not Saving Multiple Lines

Posted 07 December 2012 - 02:21 AM

Way too much code with no comments.

If you can, isolate and post the code relative to your question. Which method saves the lines?

But first, ensure that method is appending the data being saved to wherever it's being saved.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4404
  • View blog
  • Posts: 12,260
  • Joined: 18-April 07

Re: Text Editor Not Saving Multiple Lines

Posted 07 December 2012 - 11:21 AM

The problem is that when you hit enter in the textarea you are putting in \n characters which windows will not see as a carriage return line feed (crlf) in the file. You can fix this by using a line like this in your safeFile() function...

writer.write(contents.replace("\n",System.getProperty("line.separator"));



Here we are replacing the \n newlines in the content with the system line separator (in the case of windows \r\n) which then will give you the newlines you are looking for in the file.

Hope this helps. :)
Was This Post Helpful? 2
  • +
  • -

#4 3xce55  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-December 12

Re: Text Editor Not Saving Multiple Lines

Posted 07 December 2012 - 03:51 PM

View PostMartyr2, on 07 December 2012 - 11:21 AM, said:

The problem is that when you hit enter in the textarea you are putting in \n characters which windows will not see as a carriage return line feed (crlf) in the file. You can fix this by using a line like this in your safeFile() function...

writer.write(contents.replace("\n",System.getProperty("line.separator"));



Here we are replacing the \n newlines in the content with the system line separator (in the case of windows \r\n) which then will give you the newlines you are looking for in the file.

Hope this helps. :)/>


Thanks! I'll get on to adding that bit of code once I have some extra free time. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1