Java program to create Notepad application

Creating a complete notepad application involves building a graphical user interface (GUI) and handling various functionalities like opening, saving, and editing text files. 


Here's a simplified example of a notepad application using Java Swing:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class NotepadApplication extends JFrame {
    private JTextArea textArea;
    private JFileChooser fileChooser;

    public NotepadApplication() {
        setTitle("Notepad Application");
        setSize(800, 600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        // Create a text area
        textArea = new JTextArea();
        textArea.setFont(new Font("Arial", Font.PLAIN, 14));

        // Create a scroll pane for the text area
        JScrollPane scrollPane = new JScrollPane(textArea);

        // Create a file chooser
        fileChooser = new JFileChooser();

        // Create a menu bar
        JMenuBar menuBar = new JMenuBar();

        // Create the "File" menu
        JMenu fileMenu = new JMenu("File");
        JMenuItem openMenuItem = new JMenuItem("Open");
        JMenuItem saveMenuItem = new JMenuItem("Save");
        JMenuItem exitMenuItem = new JMenuItem("Exit");

        // Add action listeners to the menu items
        openMenuItem.addActionListener(new OpenActionListener());
        saveMenuItem.addActionListener(new SaveActionListener());
        exitMenuItem.addActionListener(new ExitActionListener());

        // Add the menu items to the file menu
        fileMenu.add(openMenuItem);
        fileMenu.add(saveMenuItem);
        fileMenu.addSeparator();
        fileMenu.add(exitMenuItem);

        // Add the file menu to the menu bar
        menuBar.add(fileMenu);

        // Set the menu bar for the frame
        setJMenuBar(menuBar);

        // Add the scroll pane to the frame
        add(scrollPane);

        setVisible(true);
    }

    private class OpenActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            int returnValue = fileChooser.showOpenDialog(null);
            if (returnValue == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                try (BufferedReader reader = new BufferedReader(new FileReader(selectedFile))) {
                    StringBuilder content = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        content.append(line);
                        content.append("\n");
                    }
                    textArea.setText(content.toString());
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    private class SaveActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            int returnValue = fileChooser.showSaveDialog(null);
            if (returnValue == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                try (BufferedWriter writer = new BufferedWriter(new FileWriter(selectedFile))) {
                    writer.write(textArea.getText());
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    private class ExitActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new NotepadApplication());
    }
}


In this example, we create a NotepadApplication class that extends JFrame to create the main frame of the notepad application. The GUI is built using Java Swing components. We have a JTextArea to display and edit the text, a JScrollPane to provide scrolling capabilities, and a JMenuBar with a "File" menu that includes options to open, save, and exit.


The "Open" menu item uses a JFileChooser to let the user select a file and then reads the content of the selected file and displays it in the JTextArea.


The "Save" menu item uses a JFileChooser to let the user choose the file to save the text content from the JTextArea. The content is then written to the selected file.


The "Exit" menu item simply exits the application when clicked.


You can run the NotepadApplication class to launch the notepad application.


Please note that this is a basic example with limited functionality. In a real-world notepad application, you may need to handle additional features such as file management, formatting options, and more.