Java Fin
Java Fin
INTRODUCTION
A Java "frame" refers to a top-level window used in GUI applications. Frames serve as
containers for other GUI components, such as buttons, text fields, and menus. They provide the
structure and layout for a user interface, allowing developers to design interactive and visually
appealing applications.
The Simple Notepad application exemplifies the use of Java and frames in creating a functional
desktop tool. With a clean and intuitive interface, it allows users to perform basic text editing
tasks. The application employs the Swing library to design the graphical elements, including
menus and a text area. The menu bar offers essential functionalities like creating a new
document, opening existing files, saving documents, and exiting the application.
The text area serves as the workspace for users to input and edit text, while a scroll pane
accommodates longer documents. The inclusion of a file chooser enhances user interaction by
simplifying file management operations. The "New," "Open," "Save," and "Save As" options
provide a comprehensive set of features for document manipulation. Error-handling
mechanisms, implemented through dialogs, enhance the robustness of the application.
In conclusion, the Simple Notepad application showcases the power and flexibility of Java in
creating practical desktop applications. Through the Swing framework, developers can
efficiently design user-friendly interfaces, demonstrating Java's commitment to cross-platform
compatibility and ease of use. This notepad application serves as a testament to Java's
capabilities in delivering reliable and intuitive software solutions.
1|Page
2. THOERY CONCEPTS
To create the simple notepad project in Java, I employed the Swing library, a robust
toolkit for building graphical user interfaces in Java. The crux of the project revolves around a
main class that extends the `JFrame` class, which serves as the primary container for the
graphical components of the application. This class encapsulates the main window of the
notepad.
Within this JFrame class, I instantiated a `JTextArea` to provide a platform for users to input
and edit text. This text area was further embedded in a `JScrollPane` to facilitate smooth
scrolling when the content exceeds the visible area. Additionally, a `JFileChooser` component
was integrated into the application to manage file-related operations such as opening, saving,
and saving as.
The user interface was enriched with a `JMenuBar`, a fundamental component for organizing
menus in Swing applications. The menu bar included a "File" menu, housing items like "New,"
"Open," "Save," "Save As," and "Exit." Each menu item was equipped with an action listener
to respond to user interactions. For example, selecting "New" triggers the clearing of the text
area, while "Open" opens a file dialog, enabling users to select an existing file to load into the
text area. Similarly, "Save" and "Save As" engage file dialogs for saving the current content.
The application's lifecycle is initiated through the `main` method, adhering to the Swing best
practice of running GUI-related code on the Swing event dispatch thread. This ensures the
proper synchronization of GUI components, mitigating potential concurrency issues.
In essence, the project involves the orchestration of Swing components, such as JFrame,
JTextArea, and JMenuBar, coupled with the incorporation of a JFileChooser for efficient file
handling. The action listeners establish a seamless connection between user interactions and the
underlying functionality, resulting in a functional and user-friendly notepad application.
2|Page
3. SOURCE CODE
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public Notepad() {
setTitle("Notepad");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(saveAsMenuItem);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
newMenuItem.addActionListener(new ActionListener()
{ @Override
public void actionPerformed(ActionEvent e)
{ newDocument();
}
});
openMenuItem.addActionListener(new ActionListener() {
3|Page
@Override
public void actionPerformed(ActionEvent e) {
openDocument();
}
});
saveMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
saveDocument();
}
});
saveAsMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{ saveAsDocument();
}
});
exitMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try (BufferedReader reader = new BufferedReader(new
FileReader(file))) {
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
textArea.setText(content.toString());
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error reading the file",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
}
4|Page
private void saveDocument() {
int returnVal = fileChooser.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try (BufferedWriter writer = new BufferedWriter(new
FileWriter(file))) {
writer.write(textArea.getText());
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error saving the file",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
}
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try (BufferedWriter writer = new BufferedWriter(new
FileWriter(file))) {
writer.write(textArea.getText());
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error saving the file",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
}
5|Page
4. OUTPUT
6|Page
5. CONCLUSION
In conclusion, the development of this simple notepad project in Java demonstrates the
effective use of the Swing library to create a functional and user-friendly graphical user
interface. By extending the `JFrame` class and incorporating components such as `JTextArea`,
`JScrollPane`, and `JMenuBar`, the project successfully provides a platform for users to input,
edit, and manage text seamlessly.
The project adheres to Swing best practices, initiating the application on the Swing
event dispatch thread to ensure proper synchronization and mitigate potential concurrency
issues. This adherence contributes to the stability and reliability of the notepad application.
7|Page