This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Handling menu events Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Handling menu events" Watch "Handling menu events" New topic
Author

Handling menu events

Prudent Kay
Greenhorn

Joined: Nov 21, 2003
Posts: 5
Dear all,
i have created a text editor with menus as those given in a notepad application. i have created the following codes on how to save a file.
FileDialog fd2 = new FileDialog (this, "Save file", FileDialog.SAVE);
FileWriter fw;
if(ae.getActionCommand().equals("Save"))
{
fd2.setVisible(true);
String st,sn;
try
{
sn= nt.fd2.getDirectory()+"/"+nt.fd2.getFile()+".txt";
fw = new FileWriter(sn);
char ch;
String stt=nt.t.getText();
for(int i=0;i<stt.length();i++)
{
ch=stt.charAt(i);
fw.write(ch);

}
fw.close();
}
catch(FileNotFoundException ee){}
catch(IOException eee){}

}
but everytime i click on the save menu the save as dialog box appears even though the file has been saved already, can anyone help me out with the code so that if the file has been saved already the 'save as' dialog box should not reappear but just save the latest changes made.
Thanx.
Dushy Inguva
Ranch Hand

Joined: Jun 24, 2003
Posts: 264
Prudent,
Couple of things:
1. You can do it by saving the file name in a member variable in the class and modifying the code like:

In this case, the file dialog will not open if fileName is already set.
2. You might want to code a bit cleaner (Sorry if im being rude).
Dushy


SJCP, SCBCD, SJCD, SCDJWS, SCEA (Part I)
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Handling menu events
 
Similar Threads
How to program with File Dialog Box.
Save contents of text area to file
How to program with File Dialog Box.
Prompt for file name
Accessing File Dialog Box