0

I am stuck in building my Notepad. The problem is that when the user pushes the clear button, the program will ask the user if the user is sure to clear, if yes then the program will ask if the user wants to save the file before clearing, if yes then the savefilebox will open up, but if NO then I want the program to clear the text, but when I press NO it does the same as the YES button. Any suggestions? Please help.

public class NotePad extends JFrame implements ActionListener {

    private JTextArea txtArea;
    private JMenuBar mnuBar;
    private JMenu mnyFile, mnyFormat, mnyEdit, mnyHelp, fontSize;
    private JMenuItem openFile, saveFile, exit, textWrap, noTextWrap, font10, font12, font14, font16, font18, font20, clear, copy, cut, paste, abtNotepad;

    public NotePad() {

        setTitle("NOTEPAD");
        setSize(800, 500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setLayout(new BorderLayout());

        txtArea = new JTextArea();
        JScrollPane scrollArea = new JScrollPane(txtArea);

        //MenyBar
        mnuBar = new JMenuBar();

        mnyFile = new JMenu("File");
        mnyFormat = new JMenu("Format");
        mnyEdit = new JMenu("Edit");
        mnyHelp = new JMenu("Help");
        fontSize = new JMenu("Font Size");

        openFile = new JMenuItem("Open");
        openFile.setAccelerator(KeyStroke.getKeyStroke(
                java.awt.event.KeyEvent.VK_O,
                java.awt.Event.CTRL_MASK));

        saveFile = new JMenuItem("Save");
        saveFile.setAccelerator(KeyStroke.getKeyStroke(
                java.awt.event.KeyEvent.VK_S,
                java.awt.Event.CTRL_MASK));

        exit = new JMenuItem("Exit");
        exit.setAccelerator(KeyStroke.getKeyStroke(
                java.awt.event.KeyEvent.VK_E,
                java.awt.Event.CTRL_MASK));

        textWrap = new JMenuItem("Text Wrap");
        noTextWrap = new JMenuItem("No Text Wrap");
        font10 = new JMenuItem("10");
        font12 = new JMenuItem("12");
        font14 = new JMenuItem("14");
        font16 = new JMenuItem("16");
        font18 = new JMenuItem("18");
        font20 = new JMenuItem("20");
        clear = new JMenuItem("Clear");
        abtNotepad = new JMenuItem("About Notepad");

        copy = new JMenuItem(txtArea.getActionMap().get(DefaultEditorKit.copyAction));
        copy.setText("Copy");
        copy.setAccelerator(KeyStroke.getKeyStroke(
                java.awt.event.KeyEvent.VK_C,
                java.awt.Event.CTRL_MASK));

        cut = new JMenuItem(txtArea.getActionMap().get(DefaultEditorKit.cutAction));
        cut.setText("Cut");
        cut.setAccelerator(KeyStroke.getKeyStroke(
                java.awt.event.KeyEvent.VK_X,
                java.awt.Event.CTRL_MASK));

        paste = new JMenuItem(txtArea.getActionMap().get(DefaultEditorKit.pasteAction));
        paste.setText("Paste");
        paste.setAccelerator(KeyStroke.getKeyStroke(
                java.awt.event.KeyEvent.VK_V,
                java.awt.Event.CTRL_MASK));

        add(mnuBar);

        mnuBar.add(mnyFile);
        mnuBar.add(mnyFormat);
        mnuBar.add(mnyEdit);
        mnuBar.add(mnyHelp);

        mnyFile.add(openFile);
        mnyFile.add(saveFile);
        mnyFile.add(exit);

        mnyFormat.add(textWrap);
        mnyFormat.add(noTextWrap);
        mnyFormat.add(fontSize);

        mnyEdit.add(clear);
        mnyEdit.add(copy);
        mnyEdit.add(cut);
        mnyEdit.add(paste);

        mnyHelp.add(abtNotepad);

        fontSize.add(font10);
        fontSize.add(font12);
        fontSize.add(font14);
        fontSize.add(font16);
        fontSize.add(font18);
        fontSize.add(font20);

        exit.addActionListener(this);
        openFile.addActionListener(this);
        saveFile.addActionListener(this);
        clear.addActionListener(this);
        abtNotepad.addActionListener(this);
        fontSize.addActionListener(this);
        font10.addActionListener(this);
        font12.addActionListener(this);
        font14.addActionListener(this);
        font16.addActionListener(this);
        font18.addActionListener(this);
        font20.addActionListener(this);
        textWrap.addActionListener(this);
        noTextWrap.addActionListener(this);

        setJMenuBar(mnuBar);
        add(scrollArea);
        setVisible(true);

    }

//Main
    public static void main(String[] args) {

        new NotePad();

    }

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == this.exit) {

            int svar = JOptionPane.showConfirmDialog(null,
                    "Are you sure you want to quit?", "Exit", JOptionPane.YES_NO_OPTION);
            if (svar == JOptionPane.YES_OPTION) {
                this.dispose();
            }
        } else if (e.getSource() == this.openFile) {
            JFileChooser open = new JFileChooser();
            int option = open.showOpenDialog(this);

            if (option == JFileChooser.APPROVE_OPTION) {
                this.txtArea.setText("");

                try {
                    Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
                    while (scan.hasNext()) {
                        this.txtArea.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.txtArea.getText());

                    out.close();

                } catch (Exception ex) {

                    System.out.println(ex.getMessage());

                }

            }
        } else if (e.getSource() == this.clear) {

            int reply = JOptionPane.showConfirmDialog(null,
                    "Are you sure you want to clear the text ?", "Clear", JOptionPane.YES_NO_OPTION);

            if (reply == JOptionPane.YES_OPTION) {
                JOptionPane.showConfirmDialog(null,
                        "Do you want to save your document?", "Save", JOptionPane.YES_NO_OPTION);

                if (reply == JOptionPane.YES_OPTION) {

                    JFileChooser lagre = new JFileChooser();
                    int options = lagre.showSaveDialog(this);

                    if (options == JFileChooser.APPROVE_OPTION) {

                        try {

                            BufferedWriter ut = new BufferedWriter(new FileWriter(lagre.getSelectedFile().getPath()));

                            ut.write(this.txtArea.getText());

                            ut.close();

                        } catch (Exception x) {

                            System.out.println(x.getMessage());

                        }

                    }

                }
            } else if (e.getSource() == this.abtNotepad) {
                JOptionPane.showMessageDialog(null, "This Application is brought to you by a 1. Year student from Norwegian School of Information Technology."
                        + "\n Hope you enjoy it.", "About Notepad", JOptionPane.INFORMATION_MESSAGE);
            } else if (e.getSource() == this.font10) {
                Font str10 = new Font("Times New Roman", Font.BOLD, 10);
                txtArea.setFont(str10);
            } else if (e.getSource() == this.font12) {
                Font str12 = new Font("Times New Roman", Font.BOLD, 12);
                txtArea.setFont(str12);
            } else if (e.getSource() == this.font14) {
                Font str14 = new Font("Times New Roman", Font.BOLD, 14);
                txtArea.setFont(str14);
            } else if (e.getSource() == this.font16) {
                Font str16 = new Font("Times New Roman", Font.BOLD, 16);
                txtArea.setFont(str16);
            } else if (e.getSource() == this.font18) {
                Font str18 = new Font("Times New Roman", Font.BOLD, 18);
                txtArea.setFont(str18);
            } else if (e.getSource() == this.font20) {
                Font str20 = new Font("Times New Roman", Font.BOLD, 20);
                txtArea.setFont(str20);
            } else if (e.getSource() == this.textWrap) {
                txtArea.setLineWrap(true);
            } else if (e.getSource() == this.noTextWrap) {
                txtArea.setLineWrap(false);
            }

        }
    }
}
fvu
  • 32,488
  • 6
  • 61
  • 79
  • Have you tried running the program in a debugger, put a breakpoint on `int svar = JOptionPane.showConfirmDialog(null,"Are you sure you want to quit?", "Exit", JOptionPane.YES_NO_OPTION);` and stepped through the program to see what route it actually follows? Also, please format your code properly, and add the Java tag to get color coding. – fvu May 08 '14 at 12:14
  • Thanks for reply. I am really new on Stackoverflow so I dont know how to do this... Sorry – user3564474 May 08 '14 at 12:15
  • Copy-paste from your editor, select code and push the {} button. Did you already try to debug the code? – fvu May 08 '14 at 12:20
  • Thanks. The problem is somewhere in "Clear" button. Cant figure out what it is... – user3564474 May 08 '14 at 12:21
  • I dont know how to debug the code..:( – user3564474 May 08 '14 at 12:21
  • What IDE are you using? Because really, if you have no idea how to debug, I suggest you stop now and find some other hobby. – fvu May 08 '14 at 12:22
  • Why so rude? You got to help me man... – user3564474 May 08 '14 at 12:24
  • You don't seem to understand that I am actually helping you *a lot* here... Put that code aside for now, head over to [this excellent tutorial on how to debug with Eclipse](http://www.vogella.com/tutorials/EclipseDebugging/article.html) and apply what you learned to your program. You'll find your problem in no time. Btw, there are many more good Java related tutorials on [vogella.com](http://www.vogella.com/tutorials/), it may be worthwhile to have a look at others as well. – fvu May 08 '14 at 12:28
  • My eyes might be fooling me but it looks like `if (reply == JOptionPane.YES_OPTION) {` and `} else if (e.getSource() == this.abtNotepad) {` and all the other `else ifs` below are in the same logical `if/else if`, which just _seems_ wrong. Might be the cause of the problem, but you should check that. Also I recommend using a seperate listener for each `AbstractButton`, to avoid the `if/else if` over kill. – Paul Samsotha May 08 '14 at 12:29

1 Answers1

1
JOptionPane.showConfirmDialog(null,
"Do you want to save your document?", "Save", OptionPane.YES_NO_OPTION);

This line is your issue. Within the if statement. You're not actually setting the "reply" variable again, and are still looking at the "reply" variable from the first JOptionPane. Set this to...

reply = JOptionPane.showConfirmDialog(null,"Do you want to save your document?", 
"Save", OptionPane.YES_NO_OPTION);

Just like you did in the first iteration.

Happington
  • 454
  • 2
  • 8
  • Thanks for the reply. That works but another problem comes up... After the first question: "Are you sure you want to clear the text ?", even if the user presses "NO" the program goes on to the next question: "Do you want to save your document?". How to go back to writing if the user chooses "NO" on first question? – user3564474 May 08 '14 at 19:47
  • Take a look at your else-if's. I think your problem lies somewhere in there. (I believe that when you hit clear, it resorts to an else-if you may not intend it to.) Try to avoid that many else-if's, it makes tracking program flow difficult. – Happington May 08 '14 at 19:52
  • This is really confusing. I try to remove Else-if statement but that makes so many errors. Really Stuck – user3564474 May 08 '14 at 20:21
  • I figured it out! Thanks for the help bro! – user3564474 May 08 '14 at 20:35