[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

Save as

Hi,

I have an option caled save as..

Wht i am looking for is that when user click save as , a dialog box should appear save the format as .cdf and the filename and path is saved .

Pls help

[filename,pathname] = uiputfile('*.cdf','Save file name');
0
Jasbir21
Asked:
Jasbir21
1 Solution
 
StillUnAwareCommented:
With some modifications, this should do the trick:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import javax.accessibility.*;

public class Z extends JFrame implements Runnable, ActionListener {
  JButton jb = new JButton("Save as...");
  public void init() {
    jb.addActionListener(this);
    add(jb);
    pack();
    center(this);
  }
  public void actionPerformed(ActionEvent ae) {
    JFileChooser jfc = new JFileChooser(".");
    jfc.setFileFilter(new javax.swing.filechooser.FileFilter() {
      public boolean accept(File f) {
        if(f.getName().toLowerCase().endsWith(".cdf"))
          return true;
        return false;
      }
      public String getDescription() {
        return "cdf file format. (*.CDF)";
      }
    });
    int ret = jfc.showSaveDialog(this);
    if(ret == jfc.APPROVE_OPTION) {
      File f = jfc.getSelectedFile();
      // do whatever You need here
      System.out.println(f);
    }
  }
  public void run() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    init();
    setVisible(true);
  }
  public void center(Component c) {
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation((dim.width - c.getWidth()) / 2, (dim.height - c.getHeight()) / 2);
  }
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Z());
  }
}
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now