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

GUI 'simple notepad'

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

Problem

Please give feedback on all aspects of the code, whether it be the commenting, usage of methods, semantics or anything that can be improved.

This code looks flawless to me, so any suggestions for improvements will make a difference.

```
// Imports packages for AWT, Events, Scanner, File, PrintWriter,
// exceptions and Swing components.
import java.util.Scanner;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
* This class extends JFrame to inherit all methods for active use.
* This class implements the interface ActionListener to handle user actions
*/
public class SchultzTobias extends JFrame implements ActionListener {

// Declaring all swing components for access throughout multiple methods
private JMenuBar menuBar;
private JTextArea txtArea;
private final JMenu[] menu = new JMenu[4];
private final JMenuItem[] menuItem = new JMenuItem[7];
private JMenu fontMenu;
private final JMenuItem[] fontItem = new JMenuItem[10];

/**
* This constructor initializes all components, sets the workspace
* preferences and connects everything to the JFrame superclass
*/
public SchultzTobias() {

// Assigns a title to the notepad
super("Simple Notepad");

// Initializes the text area for user input
txtArea = new JTextArea(24, 54);

// Sets formatting to break line at whole words, and adding
// some border to the JTextArea.
txtArea.setLineWrap(true);
txtArea.setWrapStyleWord(true);
txtArea.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

// If content exceeds size of viewport, make scrolling through the
// content possible
JScrollPane scrollPane = new JScrollPane(txtArea);

// initializes all menus, menuItems and corresponding key bindings
initializeMenus();
initializeKeyBindings();

//

Solution

give feedback on all aspects of the code

so, here is a short feedback:

few things i've noticed,

  • almost all methods are public (is this needed?)



  • e.g. initializeMenus() is called from within ctor, but is is not final



  • class SchultzTobias can be subclassed (it's not final)



  • most of your methods can be overridden (when subclassed...)



  • what is menuItem[3] and menu[2] ? (you only have few static menu-items, maybe you should consider using mnuItmSave,mnuItmOpen,... for better reading)



  • exitFile(boolean clear) can be shortend (look at it, you have duplicate code inside!)



  • exitFile(boolean clear) is only called by MenuItem-Action, not when closing the window (e.g. with windows X-Button)



  • calling setVisible() inside of ctor works, but it's unusual (and maybe unexpected when subclassed)



  • you don't close Closeables in case of exception (like: PrintWriter)



Cheers!

Context

StackExchange Code Review Q#91911, answer score: 4

Revisions (0)

No revisions yet.