Results 1 to 8 of 8
  1. #1
    munish is offline Member
    Join Date
    Jul 2009
    Posts
    37
    Rep Power
    0

    Default problem with notepad

    i am having problem with this notepad i am not been able to open an dialog box when i click
    open or save menuitems>
    Java Code:
    package eventhandling;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    public class notepad {
        public static void main(String s[]){
            myframe f1=new myframe("Notepad");
            f1.setSize(400,400);
            f1.setVisible(true);
            f1.setLocation(400,400);
        }
    }
    class myframe extends Frame implements ActionListener{
        TextArea t1;
        String arg;
        myframe(String s){
            super(s);
            t1=new TextArea(20,30);
            setLayout(new BorderLayout());
            add(t1,BorderLayout.CENTER);
            MenuBar mb1=new MenuBar();
            setMenuBar(mb1);
            Menu file=new Menu("File");
            MenuItem open =new Menu("Open");
            MenuItem save=new Menu("Save");
            file.add(open);
            file.add(new MenuItem("_"));
            file.add(save);
            Menu edit=new Menu("Edit");
            MenuItem cut=new Menu("Cut");
            file.add(new MenuItem("_"));
            MenuItem copy=new Menu("Copy");
            MenuItem paste=new Menu("Paste");
            open.addActionListener(this);
            save.addActionListener(this);
            edit.add(cut);
            edit.add(copy);
            edit.add(paste);
            mb1.add(file);
            mb1.add(edit);
            
        }
        public void actionPerformed(ActionEvent e)
        {
            String command=e.getActionCommand();
            System.out.println(command);
            arg="you selected"+command;
            t1.append(arg + "\n");
            if(command.equals("Open"))
            {
                FileDialog fd;
                Frame f1=new Frame();
                fd=new FileDialog(f1,"open the file");
                fd.setSize(300,300);
                fd.setVisible(true);
                fd.setLocation(400,400);
                t1.append("you selected"+fd.getDirectory()+fd.getFile());
                try
                {
                    FileReader fr=new FileReader(fd.getDirectory()+fd.getFile());
                    String msg="";
                    int i;
                    while((i=fr.read())!=-1)
                    {
                        msg=msg+(char)i;
                    }
                    fr.close();
                    t1.append(msg);
                }catch(Exception ex){}
            }
            if(command.equals("Save")){
                FileDialog fd;
                Frame f1=new Frame();
                fd=new FileDialog(f1,"save the file",FileDialog.SAVE);
                fd.setSize(300,300);
                fd.setVisible(true);
                fd.setLocation(400,400);
                try{
                    FileWriter fr=new FileWriter(fd.getDirectory()+fd.getFile());
                    fr.write(t1.getText());
                    fr.close();
                }catch(Exception ex){}
    
            }
        }
    }
    Last edited by munish; 08-29-2009 at 12:45 PM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Don't do
    Java Code:
     catch(Exception ex){}
    that just hides the possible exceptions.
    At least do a
    Java Code:
    catch(Exception ex){ex.printStackTrace();}
    so you know what exceptions if any have occurred.
    Also add a window listener for your frame so that users can close it if they wish to.
    Format your code and post it wrapped under code tags.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    What (if anything) is being printed on System.out when you select the save or open menu items?

    Your code would be easier to read if you put [code] at the start of the code and [/code] at the end. Or select your code and click the # button.

  4. #4
    munish is offline Member
    Join Date
    Jul 2009
    Posts
    37
    Rep Power
    0

    Default

    thanks for rply >>>.

    nothing prints on system .out when i click open or save..................

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    The reason why the actions don't fire is because you created new Menu objects not MenuItem objects. The references are type MenuItem but the object itself is a Menu.
    Menu will only send the event to the frame when one of its subitems is selected.

    Change the lines
    Java Code:
    MenuItem open =new Menu("Open");
    to
    Java Code:
    MenuItem open =new MenuItem ("Open");

  6. #6
    munish is offline Member
    Join Date
    Jul 2009
    Posts
    37
    Rep Power
    0

    Default

    thank u that was very silly mistake???????????????

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Welcome, it helps to read the API specs for those classes as well.
    Any particular reason why you are using awt instead of swing.

  8. #8
    munish is offline Member
    Join Date
    Jul 2009
    Posts
    37
    Rep Power
    0

    Default

    i just started with java few months ago i am going step by step to higher concepts

Similar Threads

  1. Load file in GUI instead of notepad
    By Kruptein in forum AWT / Swing
    Replies: 1
    Last Post: 08-08-2009, 05:57 PM
  2. NotePad
    By whosadork in forum New To Java
    Replies: 10
    Last Post: 10-03-2008, 07:44 PM
  3. Notepad or IDE?
    By Eranga in forum Other IDEs
    Replies: 30
    Last Post: 09-22-2008, 04:20 AM
  4. need help for my PROJECT(notepad system)
    By hrithik4568 in forum New To Java
    Replies: 0
    Last Post: 05-06-2008, 10:36 PM
  5. Creating a notepad application with java
    By Daniel in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 05:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •