I decided on making a small notepad editor in Java using Swing and I think it functions pretty well. Unfortunately, my skills in making my project tidy and organized has abandoned me; it looks like spaghetti code, which many renowned programmers discourage from doing. So all I ask the fellow community of Code Review is what can I do to make my code simpler.
Note: The JFontChooser.java
does not belong to me. The entire file belongs to ZoeloeSoft.
Project : https://www.tusfiles.net/t1m4pwu23ebd
FramePanel.java - Adds all editable JTextArea
// events and actions
import java.awt.*;
import java.awt.event.*;
import javax.swing.JCheckBoxMenuItem;
import java.io.File;
// for gui-related
import javax.swing.*;
// for opening/saving files
import javax.swing.filechooser.*;
public class FramePanel implements ActionListener
{
private Frame window;
private JPanel panel;
private JTextArea edit;
private File openFile;
private JScrollPane scroll;
private JFileChooser dialog;
private Utilities util;
// instantiate the panel
public FramePanel(Frame caller)
{
// instantiate the utilites class and filechooser for function
util = new Utilities();
dialog = new JFileChooser();
openFile = null;
// instantiate the panel with borderlayout and frame
panel = new JPanel();
window = caller;
panel.setLayout(new BorderLayout());
// instantiate the scrollpanel and editarea
edit = new JTextArea();
scroll = new JScrollPane(edit);
// customize editarea
edit.setFont(new Font("Courier New", Font.PLAIN, 12));
edit.setTabSize(4);
// add the scrollpane with editarea in the panel
panel.add(scroll, BorderLayout.CENTER);
}
// pass on panel to frame
public JPanel getPanel() { return panel; }
// listen for actions/clicks
public void actionPerformed(ActionEvent e)
{
String query = e.getActionCommand();
// if new was clicked > erase document to null
if (query.equals("New")) {
edit.setText(null);
window.setName(null);
openFile = null;
}
// if open was clicked > open new file
else if (query.equals("Open"))
{
int result = dialog.showOpenDialog(dialog);
if (result == JFileChooser.APPROVE_OPTION) {
String fileName = util.openFile(edit, dialog.getSelectedFile());
openFile = dialog.getSelectedFile();
window.setName(fileName);
}
}
// if save was clicked > save if new file; overwrite if existing
else if (query.equals("Save"))
{
if (openFile != null && openFile.exists())
util.saveFile(edit, openFile, true);
else
{
int ret = dialog.showSaveDialog(dialog);
if (ret == JFileChooser.APPROVE_OPTION) {
String name = util.saveFile(edit, dialog.getSelectedFile(), false);
openFile = dialog.getSelectedFile();
}
window.setName(openFile.getName());
}
}
// if exit was clicked > exit
else if (query.equals("Exit"))
window.exit();
// if select all was clicked > select all text
else if (query.equals("Select All"))
edit.selectAll();
// if time/date was clicked > append time and date.
else if (query.equals("Time/Date"))
edit.append(util.getDate());
// if word wrap was selected > set editable to word wrap t/f
else if (query.equals("Word Wrap"))
{
JCheckBoxMenuItem opt = (JCheckBoxMenuItem)e.getSource();
edit.setLineWrap(opt.isSelected());
}
// if font was selected > set editable.font to selected font
else if (query.equals("Font"))
util.changeFont(edit);
}
}
Utilities - Adds functionality to FramePanel.java
// for editable
import javax.swing.JTextArea;
import java.awt.Font;
// for opening a file
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
import javax.swing.JCheckBoxMenuItem;
// for writing a file
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
// for appending dates
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class Utilities
{
// open file; should only be called from framepanel and requires
// jfilechooser and editable
public String openFile(JTextArea editable, File file)
{
editable.setText(null);
// instantiate the scanner and make string pointing to filepath
Scanner fileReader = null;
String filePath = file.getPath();
// try to open file
try
{
fileReader = new Scanner(new FileReader(filePath));
// loop until all files are appended to editable
while (fileReader.hasNextLine())
editable.append(fileReader.nextLine() + "\n");
}
// catch any errors and exceptions
catch (Exception except) {
except.printStackTrace();
}
// close the reader
finally {
fileReader.close();
}
return file.getName();
}
// save file; should only be called from framepanel and requires
// jfilechooser and editable
public String saveFile(JTextArea editable, File file, boolean overWrite)
{
String fileName = file.getName();
String text = editable.getText();
BufferedWriter writer = null;
if (overWrite)
{
try
{
writer = new BufferedWriter(new FileWriter(file.getAbsolutePath()));
writer.write(""); // to clear out text
writer.append(text);
}
catch (Exception e) { e.printStackTrace(); }
finally
{
try
{
writer.flush();
writer.close();
}
catch (Exception e) { e.printStackTrace(); }
}
fileName += "0";
}
else
{
try
{
writer = new BufferedWriter(new FileWriter(file.getAbsolutePath()));
writer.write(text);
}
catch (Exception e) { e.printStackTrace(); }
finally
{
try
{
writer.flush();
writer.close();
}
catch (Exception e) { e.printStackTrace(); }
}
fileName += "1";
}
return fileName;
}
// gives date to caller
public String getDate()
{
DateFormat dateList = new SimpleDateFormat("hh:mm MM/dd/yyyy");
Date date = new Date();
return dateList.format(date);
}
// changes font of Jtextarea
public void changeFont(JTextArea editable)
{
Font current = editable.getFont();
JFontChooser dialog = new JFontChooser(null);
dialog.setFont(current);
int ret = dialog.showDialog();
if (ret == dialog.OK_OPTION)
editable.setFont(dialog.getFont());
}
}