So I'm new to java and I'm currently working on my first java program which is a copy of a Notepad.
I am currently having problem with my "New" functionality, so basically my goal is if the user changed something on the document then press "New" on a new document it will show dialogbox to save then clear the textarea (this is my askToSave() method), if its an existing document then it will just save the file then clear the textarea. But if there is no changes to the document then it will just clear the textarea.
My problem is that I created a DocumentLisnter to listen to my textarea if there is changes, I have Instance Variable statusTextArea set to false then if there is changes happend on my textare it will set this value to true then if this is true it will run my save method else then just clear textarea. but it seems like even though I did not change anything on the text area it always run my askToSave() method.
This is my "New" Listener
public class NewMenuListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (statusTextArea == true) {
askToSave();
} else {
editorTextArea.setText("");
}
}
}
This is my askToSave Method:
private void askToSave() {
JOptionPane message = new JOptionPane();
int x = message.showConfirmDialog(frame, "Do you want to save changes?", "DVTextEditor", message.YES_NO_CANCEL_OPTION);
if (x == message.YES_OPTION) {
if(chooser.getSelectedFile() == null) {
chooser.showSaveDialog(frame);
saveFile(chooser.getSelectedFile());
} else {
saveFile(chooser.getSelectedFile());
}
editorTextArea.setText("");
} else if(x == message.NO_OPTION) {
editorTextArea.setText("");
} else {
//meh
}
}
and this is the DocumentListener:
public class DocListener implements DocumentListener {
public void changedUpdate(DocumentEvent e) {
statusTextArea = true;
}
public void insertUpdate(DocumentEvent e) {
statusTextArea = true;
}
public void removeUpdate(DocumentEvent e) {
statusTextArea = true;
}
}
Full Code Below:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class DVTextEditor {
private JTextArea editorTextArea;
private JFrame frame;
private JFileChooser chooser;
private boolean statusTextArea;
public DVTextEditor() {
chooser = new JFileChooser();
statusTextArea = false;
}
public void go() {
// make frame
JFrame frame = new JFrame("DVTextEditor");
//make menu bar and menu
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu formatMenu = new JMenu("Format");
//make menu items for file menu
JMenuItem newMenuItem = new JMenuItem("New");
JMenuItem openMenuItem = new JMenuItem("Open");
JMenuItem saveMenuItem = new JMenuItem("Save");
//add menu items in file menu
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
//action listeners for New and Save menu items
newMenuItem.addActionListener(new NewMenuListener());
openMenuItem.addActionListener(new OpenMenuListener());
saveMenuItem.addActionListener(new SaveMenuListener());
//make menu items for format menu
JCheckBoxMenuItem wrapMenuItem = new JCheckBoxMenuItem("Word Wrap ");
//add menu items in format menu
formatMenu.add(wrapMenuItem);
//action listener for word Wrap item menu
wrapMenuItem.addItemListener(new WordWrapListener());
// add menu in menu bar
menuBar.add(fileMenu);
menuBar.add(formatMenu);
// make text area and put inside scrollpane
editorTextArea = new JTextArea(15, 50);
editorTextArea.setMargin(new Insets(5, 5, 5, 5));
Font defaulFont = new Font("sanserif", Font.PLAIN, 15);
editorTextArea.setFont(defaulFont);
// add listener for JTextArea
editorTextArea.getDocument().addDocumentListener(new DocListener());
// make scrollpane then add textarea to scrollpane
JScrollPane eScrollBar = new JScrollPane(editorTextArea);
eScrollBar.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
eScrollBar.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
// add menu bar and text area in frame
frame.setJMenuBar(menuBar);
frame.getContentPane().add(BorderLayout.CENTER, eScrollBar);
// enable frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setVisible(true);
}
public class NewMenuListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (statusTextArea == true) {
askToSave();
statusTextArea = false;
System.out.println("Status is false");
}
else {
editorTextArea.setText("");
statusTextArea = false;
System.out.println("Status is false");
}
}
} //end New Menu Inner Class
private void askToSave() {
JOptionPane message = new JOptionPane();
int x = message.showConfirmDialog(frame, "Do you want to save changes?", "DVTextEditor", message.YES_NO_CANCEL_OPTION);
if (x == message.YES_OPTION) {
if(chooser.getSelectedFile() == null) {
chooser.showSaveDialog(frame);
saveFile(chooser.getSelectedFile());
} else {
saveFile(chooser.getSelectedFile());
}
editorTextArea.setText("");
}
else if (x == message.NO_OPTION) {
editorTextArea.setText("");
}
else {
//meh
}
}
public class DocListener implements DocumentListener {
public void changedUpdate(DocumentEvent e) {
statusTextArea = true;
System.out.println("Status is true!");
}
public void insertUpdate(DocumentEvent e) {
statusTextArea = true;
System.out.println("Status is true!");
}
public void removeUpdate(DocumentEvent e) {
statusTextArea = true;
System.out.println("Status is true!");
}
}
public class OpenMenuListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
chooser.showOpenDialog(frame);
loadFile(chooser.getSelectedFile());
}
} // end Open Menu Inner Class
private void loadFile(File file) {
StringBuilder myDoc = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
while((line = reader.readLine()) != null) {
myDoc.append(line + "\n");
}
reader.close();
} catch (Exception ex) {
System.out.println("Could not read file");
ex.printStackTrace();
}
editorTextArea.setText(myDoc.toString());
statusTextArea = false;
System.out.println("Status is false");
}
public class SaveMenuListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
chooser.showSaveDialog(frame);
saveFile(chooser.getSelectedFile());
}
} // end Save Menu Inner Class
private void saveFile(File file) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
//writer.write(editorTextArea.getText()); <- does not work write only one line
//instead of using bufferedWriter's method to write use textarea's method to write
editorTextArea.write(writer);
writer.close();
} catch (IOException ex) {
System.out.println("Could not save file!");
ex.printStackTrace();
}
statusTextArea = false;
System.out.println("Status is false");
}
public class WordWrapListener implements ItemListener {
public void itemStateChanged (ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
editorTextArea.setLineWrap(true);
editorTextArea.setWrapStyleWord(true);
frame.repaint();
} else {
editorTextArea.setLineWrap(false);
editorTextArea.setWrapStyleWord(false);
frame.repaint();
}
}
} // end inner class
public static void main(String[] args) {
DVTextEditor x = new DVTextEditor();
x.go();
}
}