Monday, March 4, 2013

Make a text editor

Making a text editor can be so simple still many people never think of customizing it according to their needs.Here i will tell you how to make a text editor in java.Well it is simple and why java,well that is because java works in Java Virtual Machine hence you can run your text editor in any of the OS regardless of anything.For the starters you need netbeans or any other IDE.My personal favourite being net beans i will be using that. Now create a new JFrame on the project and add four buttons and a textarea.now rename the four buttons as open,save,save as and exit . Put action listeners to the exit button and write System.exit(0); this would simply close the app when you press the button. Now in order to access the files first you must know how to read and write into them.Well go over BufferedReader and BufferedWriter as they are user to read and write to the file. for example to read a file with it path save in string Path write try{ BufferedReader io=new BufferedReader(new FileReader(Path)); String line; while((line=io.readLine())!=null){ jTextArea1.append(line); jTextArea1.append("\n"); } }catch(Exception e){System.out.print("Error while reading from the file");} the io is our buffered reader and will read the file line by line and print it to the textarea.Similar steps are to be taken in for writing to a file.suppose that you initialized a write variable as BufferedWriter the write.write(String); could be used to set input to the file.Now the last thing left to do is how do we get the path of the file to get the path of the file we can use JFileChooser(); this could be found in javax.swing . Now all that is left for you guys is to integrate this and get your own text editor get going