In this project you will create a simple text editor. . This application will al
ID: 3591412 • Letter: I
Question
In this project you will create a simple text editor.
. This application will allow you to create new text files and open existing text files.
The file contents are to be displayed in a TextArea 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 file name 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 displayed in 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 file name 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.
There should be just three files that you do:
TextEditorController.java
TextEditor.java
TextEditor.fxm
//The have Image how the are look
Explanation / Answer
TextEditorController.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.TextArea;
import javafx.scene.control.ToggleGroup;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import static javax.swing.JOptionPane.WARNING_MESSAGE;
import static javax.swing.JOptionPane.YES_NO_CANCEL_OPTION;
public class TextEditorController {
@FXML
private MenuItem newFile;
@FXML
private MenuItem openFile;
@FXML
private MenuItem saveFile;
@FXML
private MenuItem saveFileAs;
@FXML
private MenuItem exitApp;
@FXML
private RadioMenuItem monoButton;
@FXML
private ToggleGroup fontSwitcherToggleGroup;
@FXML
private RadioMenuItem serifButton;
@FXML
private RadioMenuItem sansSerifButton;
@FXML
private CheckMenuItem italicFormatCheckBox;
@FXML
private CheckMenuItem boldFormatCheckBox;
@FXML
private TextArea workSpace;
private String fileName;
@FXML
public void initialize() {
workSpace.setWrapText(true);
workSpace.setFont(new Font("Monospaced", 12));
sansSerifButton.setSelected(true);
italicFormatCheckBox.setSelected(false);
boldFormatCheckBox.setSelected(false);
}
/**
*
* @param ae
*/
public void openMenuListener(ActionEvent ae) {
String content = "";
JFileChooser fileChooser = new JFileChooser();
int status = fileChooser.showOpenDialog(null);
if (status == JFileChooser.APPROVE_OPTION) {
try {
File selectedFile = fileChooser.getSelectedFile();
fileName = selectedFile.getPath();
try (Scanner inputFile = new Scanner(selectedFile)) {
do {
content += inputFile.nextLine();
} while (inputFile.hasNext());
workSpace.setText(content);
}
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(fileChooser, "There was an error opening the file", "Error", status);
}
}
}
/**
*
* @param ae
*/
public void saveMenuListener(ActionEvent ae) {
if (fileName != null) {
saveFile();
} else {
saveNewFile();
}
}
/**
*
* @param ae
*/
public void saveAsMenuListener(ActionEvent ae) {
saveNewFile();
}
/**
* saveNewFile() offers the user the option to name a new file before saving
* that file to the chosen directory
*/
public void saveNewFile() {
JFileChooser fileChooser = new JFileChooser();
int status = fileChooser.showSaveDialog(null);
if (status == JFileChooser.APPROVE_OPTION) {
fileName = fileChooser.getSelectedFile().toString();
saveFile();
}
}
/**
* saveFile() creates the file passing the text saved in the textArea
*/
public void saveFile() {
File savedFile = new File(fileName);
try (FileWriter fw = new FileWriter(savedFile)) {
fw.write(workSpace.getText());
fw.close();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "There was an error saving the file");
}
}
/*
File Menu Event Listeners
*/
public void newFileButtonListener() {
workSpace.setText(null);
fileName = null;
sansSerifButton.setSelected(true);
italicFormatCheckBox.setSelected(false);
boldFormatCheckBox.setSelected(false);
}
/**
*
*/
public void exitButtonListener() {
Platform.exit();
}
/*
Font menu action listener
*/
public void menuRadioButtonListener(ActionEvent ae) {
// Get the current font.
Font textFont = workSpace.getFont();
// Retrieve the font name and size.
String fontName = textFont.getName();
if (monoButton.isSelected()) {
fontName = "Monospaced";
} else if (serifButton.isSelected()) {
fontName = "Serif";
} else if (sansSerifButton.isSelected()) {
fontName = "SansSerif";
}
workSpace.setFont(Font.font(fontName));
}
/**
*
* @param ae
*/
public void italicFormatCheckBoxListener(ActionEvent ae) {
Font font = workSpace.getFont();
FontPosture posture = italicFormatCheckBox.isSelected()
? FontPosture.ITALIC : FontPosture.REGULAR;
workSpace.setFont(Font.font(font.getFamily(), posture, font.getSize()));
}
/**
*
* @param ae
*/
public void boldFormatCheckBoxListener(ActionEvent ae) {
Font font = workSpace.getFont();
workSpace.setFont(Font.font(font.getFamily(),
(boldFormatCheckBox.isSelected() ? FontWeight.BOLD : FontWeight.NORMAL),
font.getSize()));
}
}
TextEditor.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class TextEditor extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent parent = FXMLLoader.load(getClass().getResource("TextEditor.fxml"));
Scene scene = new Scene(parent);
stage.setTitle("Text Editor");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.