Search code examples
javaswingjframepasswordsnotepad

Issue with password protected notepad in Java


I'm having issues understanding why my code won't run.

As it stands, Eclipse isn't showing me any errors, yet the code will not launch when I try to run it.

The idea of my program is a password-protected notepad using JFrame.

This is the code :

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.*;

public class notepad extends JFrame implements ActionListener {
    public static void main(String args[]){}
    private TextArea textArea = new TextArea("", 0,0, TextArea.SCROLLBARS_VERTICAL_ONLY);
    private MenuBar menuBar = new MenuBar();
    private Menu file = new Menu();
    private MenuItem openFile = new MenuItem(); 
    private MenuItem saveFile = new MenuItem();
    private MenuItem close = new MenuItem(); 
    public notepad() {
        this.setSize(700, 500); 
        this.setTitle("Projet Java"); 
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.textArea.setFont(new Font("Helvetica", Font.ROMAN_BASELINE, 12));
        this.getContentPane().setLayout(new BorderLayout()); 
        this.getContentPane().add(textArea);
        this.setMenuBar(this.menuBar);
        this.menuBar.add(this.file); 
        this.file.setLabel("File");
        this.openFile.setLabel("Open");
        this.openFile.addActionListener(this);
        this.file.add(this.openFile); 
        this.saveFile.setLabel("Save");
        this.saveFile.addActionListener(this);
        this.file.add(this.saveFile);
    }

    public void actionPerformed (ActionEvent e) {
        if (e.getSource() == this.close)
            this.dispose(); 
        else if (e.getSource() == this.openFile) {
            JFileChooser open = new JFileChooser(); 
            int option = open.showOpenDialog(this); 
            if (option == JFileChooser.APPROVE_OPTION) {
                this.textArea.setText(""); 
                try {
                    Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
                    while (scan.hasNext())
                        this.textArea.append(scan.nextLine() + "\n"); 
                } catch (Exception ex) { 
                    System.out.println(ex.getMessage());
                }
            }
        }
        else if (e.getSource() == this.saveFile) {
            JFileChooser save = new JFileChooser(); 
            int option = save.showSaveDialog(this); 
            if (option == JFileChooser.APPROVE_OPTION) {
                try {
                    BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
                    out.write(this.textArea.getText());
                    out.close(); 
                } catch (Exception ex) { 
                    System.out.println(ex.getMessage());
                }
            }
        }
    }
        static class password{ 
        public static String password = "password";

        public static void main(String args[]) {
                JFrame box = new JFrame("Password");
                box.setVisible(true);
                box.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                box.setSize(400,100);

                JPasswordField pass = new JPasswordField(10);
                pass.setEchoChar('*');  
            }
                static class AL implements ActionListener{
                    public void actionPerformed(ActionEvent e){
                    JPasswordField input = (JPasswordField) e.getSource();
                    char[] pass = input.getPassword();
                    String yes = new String(pass);

                    if (yes.equals(password)){ 
                        notepad app = new notepad();
                        app.setVisible(true);
                    }else{
                        System.exit(0);
                    }


                    }
                }
        }
}

Solution

    • Your first main method is public but is empty so nothing will be executed.
    • You have your second main method inside non public class, so the JVM will never found it to start your program.
    • You mix AWT graphic components with Swing ones, the rule is to choose one kind and never mix the both.
    • All your Swing application should be started within SwingUtilities.invokeLater to ensure that all threads that modify GUI are executed by the Event Dispatcher Thread (EDT).
    • When using JTextArea always put it inside JScrollPane to be able to reach text displayed outside the component size.

    Here's modified code for testing :

    import java.awt.Font;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    @SuppressWarnings("serial")
    public class Notepad extends JFrame
    {
        public static void main(String args[])
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    try
                    {
                        UIManager.setLookAndFeel(
                                "javax.swing.plaf.nimbus.NimbusLookAndFeel");
                    }
                    catch (ClassNotFoundException | InstantiationException
                            | IllegalAccessException
                            | UnsupportedLookAndFeelException e)
                    {
                        e.printStackTrace();
                    }
                    new Notepad();
                }
            });
        }
    
        private JTextArea textArea = new JTextArea("", 0, 0);
        private JMenuBar menuBar = new JMenuBar();
        private JMenu file = new JMenu();
        private JMenuItem openFile = new JMenuItem();
        private JMenuItem saveFile = new JMenuItem();
        private JMenuItem close = new JMenuItem();
        private JFileChooser open, save;
    
        public Notepad()
        {
            setBounds(100, 100, 700, 500);
            setTitle("Projet Java");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            textArea.setFont(new Font("Helvetica", Font.ROMAN_BASELINE, 12));
            add(new JScrollPane(textArea));
    
            open = new JFileChooser();
            save = new JFileChooser();
    
            setJMenuBar(menuBar);
            menuBar.add(file);
            file.setText("File");
    
            openFile.setText("Open");
            openFile.addActionListener(e -> {
                if (open.showOpenDialog(
                        Notepad.this) == JFileChooser.APPROVE_OPTION)
                {
                    textArea.setText("");
                    try (BufferedReader br = new BufferedReader(
                            new FileReader(open.getSelectedFile())))
                    {
                        String s = null;
                        while ((s = br.readLine()) != null)
                        {
                            textArea.append(s + "\n");
                        }
                    }
                    catch (Exception ex)
                    {
                        System.out.println(ex.getMessage());
                    }
                }
            });
            file.add(openFile);
    
            saveFile.setText("Save");
            saveFile.addActionListener(e -> {
                if (save.showSaveDialog(
                        Notepad.this) == JFileChooser.APPROVE_OPTION)
                {
                    try (BufferedWriter out = new BufferedWriter(
                            new FileWriter(save.getSelectedFile())))
                    {
                        out.write(textArea.getText());
                    }
                    catch (Exception ex)
                    {
                        System.out.println(ex.getMessage());
                    }
                }
            });
            file.add(saveFile);
    
            close.setText("Close");
            close.addActionListener(e -> {
                System.exit(0);
            });
            file.add(close);
    
            setVisible(true);
        }
    }