Results 1 to 3 of 3
  1. #1
    kieran82 is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default 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?

    Java 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 );
    		
    	}	    
    	    
    }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 03-15-2010 at 10:09 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. [SOLVED] jfilechooser for saving a file....?
    By prabhurangan in forum AWT / Swing
    Replies: 10
    Last Post: 10-06-2013, 11:17 PM
  2. Replies: 8
    Last Post: 02-18-2010, 01:54 PM
  3. Replies: 3
    Last Post: 01-29-2010, 06:14 PM
  4. how to use JFileChooser to select file -> string
    By gezzel in forum AWT / Swing
    Replies: 15
    Last Post: 10-23-2008, 06:34 AM
  5. how to use JFileChooser to select file -> string
    By gezzel in forum New To Java
    Replies: 9
    Last Post: 09-18-2008, 10:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •