Results 1 to 3 of 3
- 03-15-2010, 08:00 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 1
- Rep Power
- 0
Saving to file from JFileChooser SaveDialog
I am having a problem with a program i have to do for a project in college. its a text editor and everything works except for the save ActionListener. when i compile it, it says cannot find symbol method getTxtArea_log(). the code is down below. can anyone help where i'm going wrong?
Moderator Edit: Code tags addedJava Code:/** * @(#)javaAssignment3.java * * @Kieran Moroney * @version 1.00 2010/3/16 */ import java.awt.*; import java.awt.Toolkit; import java.awt.event.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedWriter; import javax.swing.JScrollPane; import javax.swing.JEditorPane; import java.io.*; import java.nio.charset.*; import javax.swing.filechooser.*; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import javax.swing.JToolBar; import javax.swing.JTextPane; public class Assignment3 extends JFrame implements ActionListener { JFileChooser fc; FileWriter myWriter; JScrollPane scrollPane; String dirName = null; String fileName = null; String directory; final JLabel statusbar = new JLabel("Output of your selection will go here"); JMenuItem quit = new JMenuItem("Quit"); JMenuItem abouttext = new JMenuItem("About Text Editor"); JMenuItem open = new JMenuItem("Open"); JMenuItem save = new JMenuItem("Save"); JMenu file = new JMenu("File"); JMenu edit = new JMenu("Edit"); JMenu about = new JMenu("About"); JMenuItem delete = new JMenuItem("Delete"); JMenuItem copy = new JMenuItem("Copy"); JMenuItem paste = new JMenuItem("Paste"); JMenuBar mbar = new JMenuBar(); JTextArea area = new JTextArea(20,120); JFileChooser dialog = new JFileChooser(System.getProperty("user.dir")); String currentFile = "Untitled"; boolean changed = false; JToolBar toolBar; String fonts[] = { "Serif", "SansSerif", "Monospaced", "Dialog", "DialogInput" }; public Assignment3() { super("Text Editor"); area.setFont(new Font("Monospaced",Font.PLAIN,12)); JScrollPane scroll = new JScrollPane(area,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); add(scroll,BorderLayout.CENTER); setJMenuBar(mbar); mbar.add(file); mbar.add(edit); mbar.add(about); about.add(abouttext); abouttext.addActionListener(this); file.add(open); open.addActionListener(this); file.add(save); save.addActionListener(this); file.add(quit); quit.addActionListener(this); edit.add(delete); edit.add(copy); edit.addSeparator(); edit.add(paste); } public void actionPerformed (ActionEvent e){ if (e.getSource() == open) { JFileChooser fc = new JFileChooser(); int returnVal = fc.showOpenDialog(Assignment3.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); try { // What to do with the file, e.g. display it in a TextArea area.read( new FileReader( file.getAbsolutePath() ), null ); } catch (IOException ex) { System.out.println("problem accessing file"+file.getAbsolutePath()); } } else { System.out.println("File access cancelled by user."); } } if (e.getSource() == save) { JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory( new File( "./") ); int actionDialog = chooser.showSaveDialog(this); if ( actionDialog == JFileChooser.APPROVE_OPTION ) { File fileName = new File( chooser.getSelectedFile( ) + ".log" ); if(fileName != null) { if(fileName.exists()) { actionDialog = JOptionPane.showConfirmDialog(this, "Replace existing file?"); if (actionDialog == JOptionPane.NO_OPTION) { chooser.showSaveDialog(this); BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) ); outFile.write( getTxtArea_log().getText( ) ); //put in textfile outFile.flush( ); // redundant, done by close() outFile.close( ); } if(actionDialog == JOptionPane.YES_OPTION) { BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) ); outFile.write( getTxtArea_log().getText( ) ); //put in textfile outFile.flush( ); // redundant, done by close() outFile.close( ); } return; //AttestDialog.getInstance( ).showErrorDialog(languageBundle.getString("LogFil eAlreadyExists")); } BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) ); outFile.write( getTxtArea_log().getText( ) ); //put in textfile outFile.flush( ); // redundant, done by close() outFile.close( ); } } } if (e.getSource() == abouttext) JOptionPane.showMessageDialog(null, "Kieran's Text Editor \n Created by Kieran Moroney"); if (e.getSource() == quit) { System.exit(0); } } public static void main (String args[]) { Assignment3 app = new Assignment3(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setSize( 400, 400); app.setVisible( true ); } }Last edited by Fubarable; 03-15-2010 at 09:09 PM. Reason: Moderator Edit: Code tags added
-
Where is the getTxtArea_log() method declaration and body in your program? If you haven't created this method, then your program will not be able to find it. Also, I've added code tags to your post above. To learn to do this yourself, please look at my signature below.
Much luck!
- 03-16-2010, 03:09 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I wonder is that the only error message you comes with. You are working on with streams and not handle any exceptions there. Isn't it?
Similar Threads
-
[SOLVED] jfilechooser for saving a file....?
By prabhurangan in forum AWT / SwingReplies: 9Last Post: 03-19-2012, 05:20 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 -
how to run file fron JFileChooser ? ( need small help)
By doha786 in forum New To JavaReplies: 3Last Post: 01-29-2010, 05:14 PM -
how to use JFileChooser to select file -> string
By gezzel in forum AWT / SwingReplies: 15Last Post: 10-23-2008, 05:34 AM -
how to use JFileChooser to select file -> string
By gezzel in forum New To JavaReplies: 9Last Post: 09-18-2008, 09:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks