HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Small notepad editor

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
smalleditornotepad

Problem

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.

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

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"

Solution

General

-
Java puts the { opening brace on the same line as the control-block, not the next line:

if (overWrite)
    {
        try
        {
            ....


should be:

if (overWrite) {
        try {
            ....


-
Always use braces even for simple '1-liner' blocks (this is what apple did not do recently and caused massive problems when a but was introduced.... with a small change):

while (fileReader.hasNextLine())
            editable.append(fileReader.nextLine() + "\n");


should be:

while (fileReader.hasNextLine()) {
            editable.append(fileReader.nextLine() + "\n");
        }


-
FileReader can take a File as input. You don't need to convert the File, to a Path, and to a String for FileReader.

-
when reading a bunch of text from a file, and dumping it unchanged to an editable panel, there is no need to read it line at a time... you can read it in a bulk format which is faster.

Utilities

You have done things really well, in one sense, your use of try-catch is extensive, and you are closing streams in the appropriate places ... but, (this one will make you cringe)... have you read up on the try-with-resources functionality in java7? Actually, in this particular case, you will likely be well served with the new-in-Java7 Files class (Javadoc)

As far as I can tell, the overwrite flag makes no difference in the saveFile....

I tend to force a charset for all files. This may not work for you, in which case you can remove the StandardCharsets.UTF_8 arguments, and rely on the platform default, but this may mean a file you save on one computer creates garbage on another.

Rewriting your Utilities class:

public String openFile(JTextArea editable, File file) {
    try {
        editable.setText(new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8));
    } catch (IOException e) {
        e.printStackTrace();
    }
    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();

    try {
        Files.write(file.toPath(), editable.getText().getBytes(StandardCharsets.UTF_8));
        if (overWrite) {
            fileName += "0";
        } else {
            fileName += "1";
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    return fileName;
}


FramePanel

Going to leave this class for someone who knows Swing better than I do.

Code Snippets

if (overWrite)
    {
        try
        {
            ....
if (overWrite) {
        try {
            ....
while (fileReader.hasNextLine())
            editable.append(fileReader.nextLine() + "\n");
while (fileReader.hasNextLine()) {
            editable.append(fileReader.nextLine() + "\n");
        }
public String openFile(JTextArea editable, File file) {
    try {
        editable.setText(new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8));
    } catch (IOException e) {
        e.printStackTrace();
    }
    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();

    try {
        Files.write(file.toPath(), editable.getText().getBytes(StandardCharsets.UTF_8));
        if (overWrite) {
            fileName += "0";
        } else {
            fileName += "1";
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    return fileName;
}

Context

StackExchange Code Review Q#51871, answer score: 5

Revisions (0)

No revisions yet.