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