By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,385 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,385 IT Pros & Developers. It's quick & easy.

regarding opening a save as file dialog window using javascript

P: 1
Hello Friends
I want to open a Save as File Dialog Window by clicking on a button.
Sep 19 '06 #1
Share this Question
Share on Google+
5 Replies


P: 2
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

10K+
P: 13,264
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

P: 2
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

P: 2
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
Expert Mod 15k+
P: 16,027
[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

Post your reply

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