Text editor project java In this project you will create a simple text editor. T
ID: 3681442 • Letter: T
Question
Text editor project java
In this project you will create a simple text editor. This will be similar to the NoteTaker editor of homework assignment number 5. This application will allow you to create new text files and open existing text files. The file contents are to be displayed in a JTextArea text area. The application will have menu items to change the font and style of the text that is displayed in the text area. Line wrapping is turned on, using word wrap style, and the text area is in a scroll pane. The applications window will display as shown:
This application uses a menu system to perform the following operations and will be displayed as shown:
Notice the items that have a letter underlined; these are hot keys. Each menu item generates an action event that is handled by an action listener. The following presents a summary of the actions performed by each menu item:
File Menu:
New
This menu item clears any text that is stored in the text area. In addition, the class's filename field is set to null. The filename field contains the path and name of the file that is currently displayed in the text area.
Open
This menu item displays a file chooser that allows the user to select a file, from any directory, to open. If the user selects a file, it is opened and its contents are read into the text area. The path and name of the file are stored in the filename field.
Save
This menu item saves the contents of the text area. The contents are saved to the file with the name and path stored in the filename field. If the filename field is set to null, which would indicate that the file has not been saved yet, then this menu item performs the same action as the Save As menu item.
Save As
This menu item displays a file chooser that allows the user to select a location and file name. The contents of the text area are written to the selected file, and the path and file name are stored in the filename field. (Be careful when using this menu item. As it will not warn you when you are about to overwrite an existing file)
Exit this menu item ends the application.
Font Menu:
Monospaced: This radio button menu item changes the text area's font to Monospaced.
Serif: This radio button menu item changes the text area's font to Serif.
SansSerif: This radio button menu item changes the text area's font to SansSerif.
Italic: This check box menu item changes the text area's style to italic.
Bold: This check box item changes the text area's style to bold.
You will need to use the API's Font class for the fonts and styles in the Font menu.
Look in the TextArea class in the javafx package API for the way to set word wrap to on.
Set the text area to display 20 lines and 40 characters per line.
Use JavaFX to produce this program. Download and install SceneBuilder to produce your program.
Follow the method explained in the KilometerConverter tutorial in chapter 15 in the textbook. T
here should be just three files that you submit:
TextEditorController.java
TextEditor.java
TextEditor.fxml
Make sure use live comment, good documentation help others to understand easily
Explanation / Answer
PROGRAM:
package com.schongeproductions.texteditor;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
public class EditorGUI extends JFrame implements ActionListener
{
public static void main(String[] args)
{
new EditorGUI();
}
private JMenu fileMenu;
private JMenu editMenu;
private JMenuItem newFile, openFile, saveFile, saveAsFile, pageSetup, printFile, exit;
private JMenuItem undoEdit, redoEdit, selectAll, copy, paste, cut;
private JFrame editorWindow;
private Border textBorder;
private JScrollPane scroll;
private JTextArea textArea;
private Font textFont;
private JFrame window;
private PrinterJob job;
public PageFormat format;
private boolean opened = false;
private boolean saved = false;
private File openedFile;
private UndoManager undo;
public EditorGUI()
{
super("JavaEdit");
fileMenu();
editMenu();
createTextArea();
undoMan();
createEditorWindow();
}
private JFrame createEditorWindow()
{
editorWindow = new JFrame("JavaEdit");
editorWindow.setVisible(true);
editorWindow.setExtendedState(Frame.MAXIMIZED_BOTH);
editorWindow.setDefaultCloseOperation(EXIT_ON_CLOSE);
editorWindow.setJMenuBar(createMenuBar());
editorWindow.add(scroll, BorderLayout.CENTER);
editorWindow.pack();
editorWindow.setLocationRelativeTo(null);
return editorWindow;
}
private JTextArea createTextArea()
{
textBorder = BorderFactory.createBevelBorder(0, Color.RED, Color.RED);
textArea = new JTextArea(30, 50);
textArea.setEditable(true);
textArea.setBorder(BorderFactory.createCompoundBorder(textBorder,
BorderFactory.createEmptyBorder(2, 5, 0, 0)));
textFont = new Font("Verdana", 0, 14);
textArea.setFont(textFont);
scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
return textArea;
}
private JMenuBar createMenuBar()
{
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(fileMenu);
menuBar.add(editMenu);
return menuBar;
}
private UndoManager undoMan()
{
undo = new UndoManager();
textArea.getDocument().addUndoableEditListener(new UndoableEditListener()
{
public void undoableEditHappened(UndoableEditEvent e)
{
undo.addEdit(e.getEdit());
}
} );
return undo;
}
private void fileMenu()
{
fileMenu = new JMenu("File");
fileMenu.setPreferredSize(new Dimension(40, 20));
newFile = new JMenuItem("New");
newFile.addActionListener(this);
newFile.setPreferredSize(new Dimension(100, 20));
newFile.setEnabled(true);
openFile = new JMenuItem("Open...");
openFile.addActionListener(this);
openFile.setPreferredSize(new Dimension(100, 20));
openFile.setEnabled(true);
saveFile = new JMenuItem("Save");
saveFile.addActionListener(this);
saveFile.setPreferredSize(new Dimension(100, 20));
saveFile.setEnabled(true);
saveAsFile = new JMenuItem("Save As...");
saveAsFile.addActionListener(this);
saveAsFile.setPreferredSize(new Dimension(100, 20));
saveAsFile.setEnabled(true);
pageSetup = new JMenuItem("Page Setup...");
pageSetup.addActionListener(this);
pageSetup.setPreferredSize(new Dimension(100, 20));
pageSetup.setEnabled(true);
printFile = new JMenuItem("Print...");
printFile.addActionListener(this);
printFile.setPreferredSize(new Dimension(100, 20));
printFile.setEnabled(true);
exit = new JMenuItem("Exit");
exit.addActionListener(this);
exit.setPreferredSize(new Dimension(100, 20));
exit.setEnabled(true);
fileMenu.add(newFile);
fileMenu.add(openFile);
fileMenu.add(saveFile);
fileMenu.add(saveAsFile);
fileMenu.add(pageSetup);
fileMenu.add(printFile);
fileMenu.add(exit);
}
private void editMenu()
{
editMenu = new JMenu("Edit");
editMenu.setPreferredSize(new Dimension(40, 20));
undoEdit = new JMenuItem("Undo");
undoEdit.addActionListener(this);
undoEdit.setPreferredSize(new Dimension(100, 20));
undoEdit.setEnabled(true);
redoEdit = new JMenuItem("Redo");
redoEdit.addActionListener(this);
redoEdit.setPreferredSize(new Dimension(100, 20));
redoEdit.setEnabled(true);
selectAll = new JMenuItem("Select All");
selectAll.addActionListener(this);
selectAll.setPreferredSize(new Dimension(100, 20));
selectAll.setEnabled(true);
copy = new JMenuItem("Copy");
copy.addActionListener(this);
copy.setPreferredSize(new Dimension(100, 20));
copy.setEnabled(true);
paste = new JMenuItem("Paste");
paste.addActionListener(this);
paste.setPreferredSize(new Dimension(100, 20));
paste.setEnabled(true);
cut = new JMenuItem("Cut");
cut.addActionListener(this);
cut.setPreferredSize(new Dimension(100, 20));
cut.setEnabled(true);
editMenu.add(undoEdit);
editMenu.add(redoEdit);
editMenu.add(selectAll);
editMenu.add(copy);
editMenu.add(paste);
editMenu.add(cut);
}
private void saveFile(File filename)
{
try
{
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
writer.write(textArea.getText());
writer.close();
saved = true;
window.setTitle("JavaText - " + filename.getName());
}
catch (IOException err)
{
err.printStackTrace();
}
}
private void quickSave(File filename)
{
try
{
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
writer.write(textArea.getText());
writer.close();
}
catch (IOException err)
{
err.printStackTrace();
}
}
private void openingFiles(File filename)
{
try
{
openedFile = filename;
FileReader reader = new FileReader(filename);
textArea.read(reader, null);
opened = true;
window.setTitle("JavaEdit - " + filename.getName());
}
catch (IOException err)
{
err.printStackTrace();
}
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == newFile)
{
new EditorGUI();
}
else if(event.getSource() == openFile)
{
JFileChooser open = new JFileChooser();
open.showOpenDialog(null);
File file = open.getSelectedFile();
openingFiles(file);
}
else if(event.getSource() == saveFile)
{
JFileChooser save = new JFileChooser();
File filename = save.getSelectedFile();
if(opened == false && saved == false)
{
save.showSaveDialog(null);
int confirmationResult;
if(filename.exists())
{
confirmationResult = JOptionPane.showConfirmDialog(saveFile, "Replace existing file?");
if(confirmationResult == JOptionPane.YES_OPTION)
{
saveFile(filename);
}
}
else
{
saveFile(filename);
}
}
else
{
quickSave(openedFile);
}
}
else if(event.getSource() == saveAsFile)
{
JFileChooser saveAs = new JFileChooser();
saveAs.showSaveDialog(null);
File filename = saveAs.getSelectedFile();
int confirmationResult;
if(filename.exists())
{
confirmationResult = JOptionPane.showConfirmDialog(saveAsFile, "Replace existing file?");
if(confirmationResult == JOptionPane.YES_OPTION)
{
saveFile(filename);
}
}
else
{
saveFile(filename);
}
}
else if(event.getSource() == pageSetup)
{
job = PrinterJob.getPrinterJob();
format = job.pageDialog(job.defaultPage());
}
else if(event.getSource() == printFile)
{
job = PrinterJob.getPrinterJob();
if(job.printDialog())
{
try
{
job.print();
}
catch (PrinterException err)
{
err.printStackTrace();
}
}
}
else if(event.getSource() == exit)
{
System.exit(0);
}
else if(event.getSource() == undoEdit)
{
try
{
undo.undo();
}
catch(CannotUndoException cu)
{
cu.printStackTrace();
}
}
else if(event.getSource() == redoEdit)
{
try
{
undo.redo();
}
catch(CannotUndoException cur)
{
cur.printStackTrace();
}
}
else if(event.getSource() == selectAll)
{
textArea.selectAll();
}
else if(event.getSource() == copy)
{
textArea.copy();
}
else if(event.getSource() == paste)
{
textArea.paste();
}
else if(event.getSource() == cut)
{
textArea.cut();
}
}
public JTextArea getTextArea()
{
return textArea;
}
public void setTextArea(JTextArea text)
{
textArea = text;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.