Results 1 to 8 of 8
Thread: problem with notepad
- 08-29-2009, 10:53 AM #1
Member
- Join Date
- Jul 2009
- Posts
- 26
- Rep Power
- 0
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.
- 08-29-2009, 11:32 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Don't do
that just hides the possible exceptions.Java Code:catch(Exception ex){}
At least do aso you know what exceptions if any have occurred.Java Code:catch(Exception ex){ex.printStackTrace();}
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.
- 08-29-2009, 11:33 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 08-29-2009, 12:21 PM #4
Member
- Join Date
- Jul 2009
- Posts
- 26
- Rep Power
- 0
thanks for rply >>>.
nothing prints on system .out when i click open or save..................
- 08-29-2009, 04:12 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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
toJava Code:MenuItem open =new Menu("Open");
Java Code:MenuItem open =new MenuItem ("Open");
- 08-29-2009, 06:16 PM #6
Member
- Join Date
- Jul 2009
- Posts
- 26
- Rep Power
- 0
thank u that was very silly mistake???????????????
- 08-29-2009, 10:28 PM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Welcome, it helps to read the API specs for those classes as well.
Any particular reason why you are using awt instead of swing.
- 08-30-2009, 11:53 AM #8
Member
- Join Date
- Jul 2009
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
Load file in GUI instead of notepad
By Kruptein in forum AWT / SwingReplies: 1Last Post: 08-08-2009, 05:57 PM -
NotePad
By whosadork in forum New To JavaReplies: 10Last Post: 10-03-2008, 07:44 PM -
Notepad or IDE?
By Eranga in forum Other IDEsReplies: 30Last Post: 09-22-2008, 04:20 AM -
need help for my PROJECT(notepad system)
By hrithik4568 in forum New To JavaReplies: 0Last Post: 05-06-2008, 10:36 PM -
Creating a notepad application with java
By Daniel in forum AWT / SwingReplies: 2Last Post: 07-02-2007, 05:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks