Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: "New" not clearing textarea!

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default "New" not clearing textarea!

    When I click on "new" the text in the textarea is suppose to be cleared but nothing is happening
     
     
    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());
     
                    }
     
                }
     
            }
     
        }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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:
    this.newFile.addActionListener(this);
    Thanks!

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

Similar Threads

  1. "Program execution skips the "if" statement"!
    By antony1925 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2012, 07:15 AM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  4. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 07:13 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM