0% found this document useful (0 votes)
11 views7 pages

Java Fin

The document describes a simple notepad application created in Java using the Swing library. It discusses how the application uses components like JFrame, JTextArea, JScrollPane and JMenuBar to provide a graphical user interface for writing and editing text. It also includes a JFileChooser for file handling operations like opening, saving and saving as files.

Uploaded by

shakaibbelal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

Java Fin

The document describes a simple notepad application created in Java using the Swing library. It discusses how the application uses components like JFrame, JTextArea, JScrollPane and JMenuBar to provide a graphical user interface for writing and editing text. It also includes a JFileChooser for file handling operations like opening, saving and saving as files.

Uploaded by

shakaibbelal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

INTRODUCTION

Java is a versatile, object-oriented programming language known for its platform


independence, allowing developers to write code that can run on any device with a Java Virtual
Machine (JVM). It is widely used for developing web, mobile, and desktop applications. Java
applications are typically structured around classes and objects, promoting modularity and code
reusability. The Swing framework, part of the Java Standard Edition, facilitates the creation of
graphical user interfaces (GUIs).

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 class Notepad extends JFrame {


private JTextArea textArea;
private JFileChooser fileChooser;

public Notepad() {
setTitle("Notepad");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

textArea = new JTextArea();


JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane, BorderLayout.CENTER);

fileChooser = new JFileChooser();

JMenuBar menuBar = new JMenuBar();


setJMenuBar(menuBar);

JMenu fileMenu = new JMenu("File");


menuBar.add(fileMenu);

JMenuItem newMenuItem = new JMenuItem("New");


JMenuItem openMenuItem = new JMenuItem("Open");
JMenuItem saveMenuItem = new JMenuItem("Save");
JMenuItem saveAsMenuItem = new JMenuItem("Save As");
JMenuItem exitMenuItem = new JMenuItem("Exit");

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);
}
});
}

private void newDocument() {


textArea.setText("");
}

private void openDocument() {


int returnVal = fileChooser.showOpenDialog(this);

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);
}
}
}

private void saveAsDocument() {


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);
}
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Notepad().setVisible(true);
}
});
}
}

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 integration of a `JFileChooser` adds a layer of sophistication, enabling users to


perform common file operations like opening, saving, and saving as. The careful
implementation of action listeners ensures responsive feedback to user interactions, enhancing
the overall user experience.

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.

In essence, this project serves as a foundational example of building a GUI-based


application in Java, illustrating the power and versatility of the Swing library in creating
intuitive and functional interfaces for end-users. It provides a starting point for developers to
explore and expand upon, incorporating additional features and refinements to meet specific
requirements.

7|Page

You might also like