Save/Open

Hi, I am making a small word editor, and am having trouble implementing the Save and Open features. Any ideas on how to make this work?Thanks
[155 byte] By [AdiVa] at [2007-11-27 3:09:04]
# 1
http://java.sun.com/docs/books/tutorial/essential/io/
CaptainMorgan08a at 2007-7-12 3:57:42 > top of Java-index,Java Essentials,New To Java...
# 2

Text components actually have read/write methods. I've built a small demo text editor that may help you out a little bit:

import javax.swing.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.event.KeyEvent;

import java.awt.Event;

import java.io.*;

public class TextEditor extends JFrame {

private JMenuBar menuBar;

private JMenu fileMenu;

private JMenuItem newItem;

private JMenuItem openItem;

private JMenuItem saveItem;

private JMenuItem exitItem;

private JTextArea textArea;

private JScrollPane jsp;

private JFileChooser jfc;

public TextEditor() {

initComponents();

}

private void initComponents() {

menuBar = new JMenuBar();

fileMenu = new JMenu("File");

fileMenu.setMnemonic((int)'F');

newItem = new JMenuItem("New Document");

newItem.setMnemonic((int)'N');

newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK));

openItem = new JMenuItem("Open Document");

openItem.setMnemonic((int)'O');

openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));

saveItem = new JMenuItem("Save Document");

saveItem.setMnemonic((int)'S');

saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK));

exitItem = new JMenuItem("Exit");

exitItem.setMnemonic((int)'X');

exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK));

fileMenu.add(newItem);

fileMenu.add(openItem);

fileMenu.add(saveItem);

fileMenu.addSeparator();

fileMenu.add(exitItem);

menuBar.add(fileMenu);

textArea = new JTextArea();

textArea.setLineWrap(true);

textArea.setWrapStyleWord(true);

jsp = new JScrollPane(textArea);

jfc = new JFileChooser();

this.getContentPane().add(jsp);

this.setJMenuBar(menuBar);

this.pack();

this.setSize(400, 400);

this.setTitle("Text Editor");

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

newItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

textArea.setText("");

}

});

openItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

openDocument();

}

});

saveItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

saveDocument();

}

});

exitItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

System.exit(0);

}

});

}

private void openDocument() {

jfc.setDialogTitle("Open Document");

int option = jfc.showOpenDialog(this);

if( option == JFileChooser.APPROVE_OPTION ) {

try {

textArea.read(new FileReader(jfc.getSelectedFile()), "Reader");

} catch (IOException ioe) { ioe.printStackTrace(); }

}

}

private void saveDocument() {

jfc.setDialogTitle("Save Document");

int option = jfc.showSaveDialog(this);

if( option == JFileChooser.APPROVE_OPTION ) {

try {

textArea.write(new FileWriter(jfc.getSelectedFile()));

} catch (IOException ioe) { ioe.printStackTrace(); }

}

}

public static void main(String[] argv) {

new TextEditor().setVisible(true);

}

}

Navy_Codera at 2007-7-12 3:57:42 > top of Java-index,Java Essentials,New To Java...
# 3
wow, thanks a lot, that helped.
AdiVa at 2007-7-12 3:57:42 > top of Java-index,Java Essentials,New To Java...