Today, I am going to make a Text Editor program using our Java. Yeah, Like notepad. I want to add some basic functionalities like Opening a file, Saving a file and option to change the font size. It will only take less than 100 lines of code to do this (Excluding User Interface).
I started with the toolbar javax.swing.JToolBar and added 4 buttons. Button with simple label logo and text for open,save and font size changing. The display area is simply a JTextArea with monotype font.
Opening a Text File
It is pretty straightforward to use JFileChooser to select a file. When I started thinking about opening a file, I wanted a File Chooser that filter and display only text files.Thanks to FileNameExtensionFilter from javax.swing.filechooser.FileNameExtensionFilter. I am able to display only text files on the JFileChooser using the code
FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES", "txt", "text");
fileOpener.setFileFilter(filter);
When the user selects a file and click open, The Scanner from java.util.Scanner is used to read the file.
Scanner scn = new Scanner(file);
String buffer = "";
while (scn.hasNext()) {
buffer += scn.nextLine() + "\n";
}
display.setText(buffer);
The “file” is nothing but the file taken from the JFileChooser and the while loop will continue until the end of file. “display” is the name of JTextArea, our display.
Saving a Text File
A global file variable called “currentEditingFile” is maintained to know whether we are working on a new, unsaved data or on an opened file. If “currentEditingFile” is null we have a new uncreated file. So the saving method will look to “currentEditingFile” and if it is null a JFileChooser will be opened to select the location for the new file. After selecting the directory, file name is taken from JOptionPane input dialog.
The file is then written onto the file Using PrintWriter from java.io.PrintWriter.
Changing Font Size
A global variable with default font size as 14 is maintained and increased or decreased depending upon the button pressed.
display.setFont(new java.awt.Font("Monospaced", 0, ++fontSize)); //Increase font size
display.setFont(new java.awt.Font("Monospaced", 0, --fontSize)); //Decrease font size
Extra Works
It is not acceptable to exit the program without any notice when the user clicks on the Close button. In order to add a save confirmation, i made an override on the Window Closing operating and added a confirmation message. It is done from the constructor itself
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e); //To change body of generated methods, choose Tools | Templates.
int ans = JOptionPane.showConfirmDialog(rootPane, "Save Changes ?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (ans == JOptionPane.YES_OPTION) {
//Write Changes
saveChanges();
}
}
});
So that’s it. Download the program / source code from below buttons. If this was helpful please like me on facebook and keep visiting for more cool programs and sources.
Recommended Read
[…] Источник […]
[…] Источник […]
[…] Источник […]
[…] Источник […]
… [Trackback]
[…] Here you can find 73630 additional Information on that Topic: genuinecoder.com/text-editor-java-with-source-notepad-html/ […]
… [Trackback]
[…] Info on that Topic: genuinecoder.com/text-editor-java-with-source-notepad-html/ […]
… [Trackback]
[…] Read More here on that Topic: genuinecoder.com/text-editor-java-with-source-notepad-html/ […]
… [Trackback]
[…] There you will find 51494 additional Information on that Topic: genuinecoder.com/text-editor-java-with-source-notepad-html/ […]
… [Trackback]
[…] Find More on to that Topic: genuinecoder.com/text-editor-java-with-source-notepad-html/ […]
… [Trackback]
[…] Read More Info here on that Topic: genuinecoder.com/text-editor-java-with-source-notepad-html/ […]
… [Trackback]
[…] Find More on to that Topic: genuinecoder.com/text-editor-java-with-source-notepad-html/ […]
… [Trackback]
[…] There you will find 16745 more Info on that Topic: genuinecoder.com/text-editor-java-with-source-notepad-html/ […]
… [Trackback]
[…] Here you will find 51155 more Information to that Topic: genuinecoder.com/text-editor-java-with-source-notepad-html/ […]
… [Trackback]
[…] Information on that Topic: genuinecoder.com/text-editor-java-with-source-notepad-html/ […]
… [Trackback]
[…] There you can find 54587 more Info to that Topic: genuinecoder.com/text-editor-java-with-source-notepad-html/ […]
… [Trackback]
[…] There you will find 37482 additional Info to that Topic: genuinecoder.com/text-editor-java-with-source-notepad-html/ […]
… [Trackback]
[…] Find More here on that Topic: genuinecoder.com/text-editor-java-with-source-notepad-html/ […]
… [Trackback]
[…] Read More to that Topic: genuinecoder.com/text-editor-java-with-source-notepad-html/ […]
… [Trackback]
[…] Read More on that Topic: genuinecoder.com/text-editor-java-with-source-notepad-html/ […]
… [Trackback]
[…] Here you can find 54248 more Information on that Topic: genuinecoder.com/text-editor-java-with-source-notepad-html/ […]
… [Trackback]
[…] Info to that Topic: genuinecoder.com/text-editor-java-with-source-notepad-html/ […]
… [Trackback]
[…] Information on that Topic: genuinecoder.com/text-editor-java-with-source-notepad-html/ […]
… [Trackback]
[…] Info to that Topic: genuinecoder.com/text-editor-java-with-source-notepad-html/ […]