"New" not clearing textarea!
When I click on "new" the text in the textarea is suppose to be cleared but nothing is happening
Code :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.*;
public class MemoPad extends JFrame implements ActionListener {
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 newFile = new MenuItem();
private MenuItem saveFile = new MenuItem();
private MenuItem close = new MenuItem();
public MemoPad() {
this.setSize(600, 300);
this.setTitle("Memo Pad");
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(textArea);
this.setMenuBar(this.menuBar);
this.menuBar.add(this.file);
this.file.setLabel("File");
this.newFile.setLabel("New");
this.file.add(this.newFile);
this.openFile.setLabel("Open");
this.file.add(this.openFile);
this.saveFile.setLabel("Save");
this.file.add(this.saveFile);
this.file.addSeparator();
this.close.setLabel("Exit");
this.close.addActionListener(this);
this.file.add(this.close);
}
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());
}
}
}
if (e.getSource() == this.newFile) {
this.textArea.setText("");
} 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());
}
}
}
}
}
Re: "New" not clearing textarea!
Add some println's or send the code through a debugger. You can use this method to determine if your ActionListener gets called (which I suspect is not) and the conditionals evaluated correctly
Re: "New" not clearing textarea!
I have another class to test the code I just dont have it included in here
idk if thats what you was asking
Re: "New" not clearing textarea!
I'm not really asking anything, I am suggesting that you partake in a little debugging. You can print out values to the command line to debug the flow of your program. You can ask your code questions such as 'is the actionPerformed method being called as I would expect?'
See http://www.javaprogrammingforums.com...t-println.html
Re: "New" not clearing textarea!
I tried a lot of things i keep getting nothing
any other advises
--- Update ---
Got it!!
I forgot to add:
Code :
this.newFile.addActionListener(this);
Thanks!
Re: "New" not clearing textarea!
That was my point above. If you added println's to the action listener, you would see they would not print anything, indicating that the listener is not being fired (and hence you did not add it). This is the process of debugging and it is an important skill