0

This code creates a small application which is a basic text editor initially and I have added a drop down file menu which is obvious in every editor, it includes features like 'new', 'open', 'save' and 'save as'.

I am not getting how to add the feature of saving in the existing file and a new file.

import java.io.*;  
import java.awt.event.*;  
import javax.swing.*;    
import java.io.IOException;
import java.nio.file.StandardOpenOption;

public class Editor implements ActionListener
{    

    boolean updateStatus;
    Editor Jedtr;
    boolean saved;  
    boolean newFileFlag;  
    String fileName;    
    static File fileRef;  


    JFrame f;    
    JMenuBar mb;    
    JMenu file,edit,help;    
    JMenuItem cut,copy,paste,selectAll,NewFile,Open,Save,SaveAs;    
    JTextArea ta;  


    Editor(){ 


        saved=true;  
        newFileFlag=true;  
        fileName=new String("Untitled");  
        fileRef=new File(fileName);  

        Open=new JMenuItem("Open File");    
        Open.addActionListener(this);            
        file=new JMenu("File");    
        file.add(Open);             
        mb=new JMenuBar();    
        f=new JFrame();    
        cut=new JMenuItem("Cut");    
        copy=new JMenuItem("Copy");    
        paste=new JMenuItem("Paste");    
        selectAll=new JMenuItem("Select All");    

        /////////////////////////////////////////////////////////////////////////////////

        NewFile =new JMenuItem("New");
        Open=new JMenuItem("Open");
        Save=new JMenuItem("Save");
        SaveAs=new JMenuItem("Save As");

        ///////////////////////////////////////////////////////////////////////////////////

        cut.addActionListener(this);    
        copy.addActionListener(this);    
        paste.addActionListener(this);    
        selectAll.addActionListener(this);

        /////////////////////////////////////////////////////////////////////////////////////

        NewFile.addActionListener(this);
        Open.addActionListener(this);
        Save.addActionListener(this);
        SaveAs.addActionListener(this);

        //////////////////////////////////////////////////////////////////////////////////

        mb=new JMenuBar();    
        file=new JMenu("File");    
        edit=new JMenu("Edit");    
        help=new JMenu("Help");     
        file.add(NewFile);file.add(Open);file.add(Save);file.add(SaveAs);    
        edit.add(cut);edit.add(copy);edit.add(paste);edit.add(selectAll);    
        mb.add(file);mb.add(edit);mb.add(help);    

        ///////////////////////////////////////////////////////////////////////////////////////

        ta=new JTextArea();    
        ta.setBounds(5,5,1200,1200);    
        f.add(mb);f.add(ta);    
        f.setJMenuBar(mb);  
        f.setLayout(null);    
        f.setSize(800,400);    
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }     




    /////////////////////////////Action performed/////////////////////////////////////////////////


    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource()==cut)                 
            ta.cut();                                // to cut text.
        else if(e.getSource()==paste)
            ta.paste();                             // to paste text. 
        else if(e.getSource()==copy)
            ta.copy();                              // to copy text.
        else if(e.getSource()==selectAll)
            ta.selectAll();                        // to select text. 
        else if(e.getSource()==Open)
        {
            JFileChooser fc=new JFileChooser();
            int i=fc.showOpenDialog(f);
            if(i==JFileChooser.APPROVE_OPTION)
            {
                File f=fc.getSelectedFile();
                String filepath=f.getPath();
                try
                {
                    BufferedReader br=new BufferedReader(new FileReader(filepath));
                    String s1="",s2="";
                    while((s1=br.readLine())!=null)
                    {
                        s2+=s1+"\n";
                    }
                    ta.setText(s2);
                    br.close();
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                }
            }
        }                                       //to open an existing file
        else if(e.getSource() == NewFile)
        {
            JFileChooser fc=new JFileChooser();
            int j=fc.showOpenDialog(f);
            if (j==JFileChooser.CUSTOM_DIALOG);

            System.out.println("New File");

            File file = new File("C:\\Android\\.txt");

            boolean result;
            try {
                // create a new file
                result = file.createNewFile();
                // test if successfully created a new file
                if(result){
                    System.out.println("Successfully created "+file.getAbsolutePath());

                }
                else{
                    System.out.println("Filed creating "+file.getAbsolutePath());
                }
            }
                catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
    }                                            // to create a new file.

            else if(e.getSource()== SaveAs)
            {                                  // what to specify here?
                }



            else if(e.getSource()==Save)
            {                                 // what to specify here?
            }
}




                /*private Object SaveAs(File temp)
{
    // TODO Auto-generated method stub
    return null;
}



///////////////////////////////////////////////////////////////////////////////////////////////


private void updateStatus(File temp, boolean b)
{
    // TODO Auto-generated method stub

}*/
                boolean isSave()
                {
                    return saved;}  

                void setSave(boolean saved)
                {
                    this.saved=saved;
                }

                String getFileName()
                {
                    return new String(fileName);
                }

                void setFileName(String fileName)
                {
                    this.fileName=new String(fileName);
                }  

                ////-------File-------////////

                //public class File implements ActionListener{}

                ////-------File-------////////

                /*public void actionPerformedf(ActionEvent O) {    
if(O.getSource()==Open){    
    JFileChooser fc=new JFileChooser();    
    int i=fc.showOpenDialog(f);    
    if(i==JFileChooser.APPROVE_OPTION){    
        File f=fc.getSelectedFile();    
        String filepath=f.getPath();    
        try{  
        BufferedReader br=new BufferedReader(new FileReader(filepath));    
        String s1="",s2="";                         
        while((s1=br.readLine())!=null){    
        s2+=s1+"\n";    
        }    
        ta.setText(s2);    
        br.close();    
        }catch (Exception ex) 
        {
            ex.printStackTrace();          
        }
    }
    }
    }*/    

                public static void main(String[] args) {    

                    new Editor(); 

                }    
            }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Feb 16 '17 at 19:41
  • .. 3) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 4) The way the editor is loading the text can be done more simply, see [`JTextComponent.read(Reader, Object)`](http://docs.oracle.com/javase/8/docs/api/javax/swing/text/JTextComponent.html#read-java.io.Reader-java.lang.Object-) for details, **5) For saving text to file, see [`JTextComponent.write(Writer)`](http://docs.oracle.com/javase/8/docs/api/javax/swing/text/JTextComponent.html#write-java.io.Writer-),** .. – Andrew Thompson Feb 16 '17 at 19:41
  • 6) For more help, you'll need to be more specific about where you get stuck, this question is 'too broad' as it stands. – Andrew Thompson Feb 16 '17 at 19:45

1 Answers1

0

Applets can't access the local file system. This will be fixed in the near future, when applets will not exist at all. Thanks Oracle!

ddyer
  • 1,792
  • 19
  • 26
  • While 'applet' is mentioned twice, the code shows an application. I've cleaned the question up a little (edited it) to make it more clear. – Andrew Thompson Feb 16 '17 at 19:38