473,394 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

regarding opening a save as file dialog window using javascript

Hello Friends
I want to open a Save as File Dialog Window by clicking on a button.
Sep 19 '06 #1
5 21201
Hello Friends
I want to open a Save as File Dialog Window by clicking on a button.


Expand|Select|Wrap|Line Numbers
  1. import java.awt.*; 
  2. import java.awt.event.*; 
  3. import java.io.*; 
  4.  
  5. public class RunEditor extends java.applet.Applet{ 
  6.   public void init(){
  7.     Editor editor = new Editor();
  8.     editor.show();
  9.   }
  10.  
  11. class Editor extends Frame implements ActionListener { 
  12.   TextArea textArea = new TextArea(); 
  13.  
  14.   Editor() { 
  15.     super("Editor"); 
  16.     setLayout( new BorderLayout() ); 
  17.     // Use a TextArea object as the editor tool 
  18.     add("Center", textArea); 
  19.     // Create a menubar with basic commands 
  20.     Menu menu = new Menu ("File"); 
  21.     menu.add ( makeMenuItem ("Load") ); 
  22.     menu.add ( makeMenuItem ("Save") ); 
  23.     menu.add ( makeMenuItem ("Quit") ); 
  24.     MenuBar menuBar = new MenuBar(); 
  25.     menuBar.add ( menu ); 
  26.     setMenuBar( menuBar ); 
  27.     pack(); 
  28.   }
  29.   // Item buttons send actions here.
  30.   public void actionPerformed( ActionEvent e ) { 
  31.     String command = e.getActionCommand(); 
  32.     if ( command.equals("Quit") ) 
  33.       dispose(); 
  34.     else if ( command.equals("Load") ) 
  35.       loadFile(); 
  36.     else if ( command.equals("Save") ) 
  37.       saveFile(); 
  38.   } 
  39.   // Use the FileDialog class to load files
  40.   private void loadFile () { 
  41.     FileDialog fd = new 
  42.        FileDialog(this, "Load File", FileDialog.LOAD ); 
  43.     fd.show(); 
  44.     String file = fd.getFile(); 
  45.     if ( file == null ) // Cancel 
  46.       return; 
  47.     try { 
  48.       FileInputStream fis = new 
  49.         FileInputStream ( fd.getFile() ); 
  50.       byte [] data = new byte [ fis.available() ]; 
  51.       fis.read( data ); 
  52.       textArea.setText( new String( data ) ); 
  53.     } catch ( IOException e ) { 
  54.       textArea.setText( "Could not load file..." ); 
  55.     } 
  56.   }
  57.   // Use the FileDialog class to save files
  58.   private void saveFile() { 
  59.     FileDialog fd = new 
  60.        FileDialog( this, "Save File", FileDialog.SAVE ); 
  61.     fd.show(); 
  62.     // Save file data... 
  63.   } 
  64.  
  65.   private MenuItem makeMenuItem( String name ) { 
  66.     MenuItem m = new MenuItem( name ); 
  67.     m.addActionListener( this ); 
  68.     return m; 
  69.   } 
  70.  
  71.   public static void main(String[] s) { 
  72.     new Editor().show(); 
  73.   } 
  74. }
  75.  
Edited by iam_clint -- Reason ("This is Java not javascript")
Oct 23 '06 #2
r035198x
13,262 8TB
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class RunEditor extends java.applet.Applet{
public void init(){
Editor editor = new Editor();
editor.show();
}
}

class Editor extends Frame implements ActionListener {
TextArea textArea = new TextArea();

Editor() {
super("Editor");
setLayout( new BorderLayout() );
// Use a TextArea object as the editor tool
add("Center", textArea);
// Create a menubar with basic commands
Menu menu = new Menu ("File");
menu.add ( makeMenuItem ("Load") );
menu.add ( makeMenuItem ("Save") );
menu.add ( makeMenuItem ("Quit") );
MenuBar menuBar = new MenuBar();
menuBar.add ( menu );
setMenuBar( menuBar );
pack();
}
// Item buttons send actions here.
public void actionPerformed( ActionEvent e ) {
String command = e.getActionCommand();
if ( command.equals("Quit") )
dispose();
else if ( command.equals("Load") )
loadFile();
else if ( command.equals("Save") )
saveFile();
}
// Use the FileDialog class to load files
private void loadFile () {
FileDialog fd = new
FileDialog(this, "Load File", FileDialog.LOAD );
fd.show();
String file = fd.getFile();
if ( file == null ) // Cancel
return;
try {
FileInputStream fis = new
FileInputStream ( fd.getFile() );
byte [] data = new byte [ fis.available() ];
fis.read( data );
textArea.setText( new String( data ) );
} catch ( IOException e ) {
textArea.setText( "Could not load file..." );
}
}
// Use the FileDialog class to save files
private void saveFile() {
FileDialog fd = new
FileDialog( this, "Save File", FileDialog.SAVE );
fd.show();
// Save file data...
}

private MenuItem makeMenuItem( String name ) {
MenuItem m = new MenuItem( name );
m.addActionListener( this );
return m;
}

public static void main(String[] s) {
new Editor().show();
}
}
Unfortunately the solution given above is a java solution not a javascript solution
@OP: Here is a link which should give you what you want
http://4umi.com/web/javascript/filewrite.htm
Oct 24 '06 #3
Hello Friends
I want to open a Save as File Dialog Window by clicking on a button.
// Show save dialog; this method does not return until the dialog is closed
fc.showSaveDialog(frame);
selFile = fc.getSelectedFile();
Jul 11 '07 #4
Expand|Select|Wrap|Line Numbers
  1.  private void jmiOpenActionPerformed(java.awt.event.ActionEvent evt) {                                        
  2. // TODO add your handling code here:
  3.  
  4.  
  5.         //jfc=new JFileChooser();
  6.        //jfc.showOpenDialog(this);
  7.         // Choose only files, not directories
  8.        boolean status=true;
  9.        String command=evt.getActionCommand();
  10.        if(command.equals("Open"))
  11.        {
  12.            status=openFile();
  13.            if(!status)
  14.                JOptionPane.showMessageDialog (
  15.             null,
  16.             "Error opening file!", "File Open Error",
  17.             JOptionPane.ERROR_MESSAGE);
  18.             }
  19.        else if(command.equals("quit"))
  20.        {
  21.            dispose();
  22.        }
  23.  }
  24.  boolean openFile()
  25.  {
  26.  
  27.  JFileChooser fc = new JFileChooser ();
  28.     fc.setDialogTitle ("Open File");
  29.  
  30.     // Choose only files, not directories
  31.     fc.setFileSelectionMode (JFileChooser.FILES_ONLY);
  32.  
  33.     // Start in current directory
  34.     fc.setCurrentDirectory (new File ("."));
  35.  
  36.     // Set filter for web pages.
  37.    fc.setFileFilter (fTextFilter);
  38.  
  39.     // Now open chooser
  40.     int result = fc.showOpenDialog (this);
  41.  
  42.     if (result == JFileChooser.CANCEL_OPTION)  {
  43.        return true;
  44.  
  45.     } else if (result == JFileChooser.APPROVE_OPTION) {
  46.       fFile = fc.getSelectedFile();
  47.       String file_string = readFile (fFile);
  48.  
  49.      if (file_string != null)
  50.           jTextArea1.setText (file_string);
  51.       else
  52.          return false;
  53.  
  54.     } else {
  55.          return false;
  56.     }
  57.     return true;
  58.  
  59.   } // openFile
  60.   public String readFile (File file) {
  61.              StringBuffer file_buffer;
  62. String file_string=null;
  63. String line;
  64.  
  65.     try {
  66.         FileReader in = new FileReader ( file);
  67.         BufferedReader dis =new BufferedReader (in);
  68.         file_buffer = new StringBuffer  () ;
  69.  
  70.         while  ((line = dis.readLine ()) != null) {
  71.             file_buffer.append (line + "\n");
  72.         }
  73.  
  74.         in.close ();
  75.         file_string = file_buffer.toString ();
  76.     } catch (IOException e) {
  77.         return null;
  78.     }
  79.  
  80.      return file_string;
  81.  
  82.  
  83.     }                                       
  84.  public static boolean writeFile (File file, String data_string) {
  85.  
  86.     try {
  87.          PrintWriter out = new PrintWriter (new BufferedWriter(new FileWriter (file)));
  88.          out.print (data_string);
  89.          out.flush ();
  90.          out.close ();
  91.     } catch  (IOException e) {
  92.          return false;
  93.     }
  94.     return true;
  95.  
  96.   } 
  97.  //public void actionPerformed (ActionEvent e) {
  98.       // readFile
  99.  
  100.  class TextFilter extends javax.swing.filechooser.FileFilter {
  101.  
  102.   public boolean accept (File f) {
  103.     return f.getName ().toLowerCase ().endsWith (".nhx")|| f.isDirectory ();
  104.   }
  105.  
  106.   public String getDescription () {
  107.     return "Web pages  (*.nhx)";
  108.   }
  109.  
  110. }
Please use code tags when posting code
Jul 19 '07 #5
acoder
16,027 Expert Mod 8TB
[code=java] private void jmiOpenActionPerformed(java.awt.event.ActionEvent evt) {
The solution you have provided is a Java solution, not JScript/ActiveX.
Jul 19 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Bob Murdoch | last post by:
I've got an intranet application that presents a list of files in sort of a 'central repository' web page. Each file is an href in the form <a href=file://server/share/path/filename.ext>. When...
17
by: SK | last post by:
I am calling an exe thru' href, but when it executes, I get the message if I want to open the file(exe file). Is there any way I can suppress this from appearing and open the program? Thank...
3
by: Clinton Goff | last post by:
I am attempting to write a javascript app that will open a second browser window, load a url, such as www.google.com (foreign url) and perform a <File-Save As> function on that window. I am able...
5
by: REB | last post by:
Can someone help me with adding some javascript functionality to a button? What would the proper syntax for for (please help with the javascript I am rusty) for doing this: ...
1
by: Ryan Moore | last post by:
I'm trying to make a page that, when opened, displays a file save dialog, then closes itself (essentially just leaving the file save dialog). Is this possible? I've tried this: ...
4
by: Richard | last post by:
Hi I'm new to ASP/Web programming so any help would be appreciated... Situation: On my web page I would like to present a link {or button} that would allow the user to download a large file. ...
4
by: Satya | last post by:
I am trying to display a PDF file (which I am being passed from a web service as a binary stream) in a browser, but I am being prompted to save the file instead. I don't want the user to be...
5
by: Neil Rossi | last post by:
I have an issue with a particular ASP page on two web servers. Let's call these servers Dev1 and Beta1. Both Servers are running IIS 5, Windows 2000 SP4 with "almost" all of the latest patches. ...
0
by: aarthiraaj | last post by:
In my user screen I have view link, if I click this link it will go to servlet and open one word document. It is opening properly, but while opening the attachment it generate one dialog box like...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.