How can i have FileDialog.SAVE????
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   john_lucas
Posted On:   Friday, September 7, 2001 07:02 AM

I am trying to create a FileDialog and trying to use to it for saving a file.
What are the different methods for that?
can any one show me some sample code.
I will appreciate it.

Re: How can i have FileDialog.SAVE????

Posted By:   Virgilio_Garcia  
Posted On:   Tuesday, September 18, 2001 04:09 PM


import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;

public class FileChooserDemo extends JFrame {

private JTextArea log;
private JFileChooser fc = new JFileChooser();

private String newline = System.getProperty("line.separator");

public FileChooserDemo() {
super("FileChooserDemo");

JButton openButton = new JButton("Open", new ImageIcon("images/open.gif"));
openButton.addActionListener(new OpenListener());

JButton saveButton = new JButton("Save", new ImageIcon("images/save.gif"));
saveButton.addActionListener(new SaveListener());

JPanel buttonPanel = new JPanel();
buttonPanel.add(openButton);
buttonPanel.add(saveButton);

log = new JTextArea(5,20);
log.setMargin(new Insets(5,5,5,5));
JScrollPane logScrollPane = new JScrollPane(log);

Container contentPane = getContentPane();
contentPane.add(buttonPanel, BorderLayout.NORTH);
contentPane.add(logScrollPane, BorderLayout.CENTER);
}

private class OpenListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);

if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//this is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
}
}
}

private class SaveListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showSaveDialog(FileChooserDemo.this);

if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//this is where a real application would save the file.
log.append("Saving: " + file.getName() + "." + newline);
} else {
log.append("Save command cancelled by user." + newline);
}
}
}

public static void main(String s[]) {
JFrame frame = new FileChooserDemo();

frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});

frame.pack();
frame.setVisible(true);
}
}

And:


import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.*;

public class ImageFileView extends FileView {
ImageIcon jpgIcon = new ImageIcon("images/jpgIcon.gif");
ImageIcon gifIcon = new ImageIcon("images/gifIcon.gif");
ImageIcon tiffIcon = new ImageIcon("images/tiffIcon.gif");

public String getName(File f) {
return null; // let the L&F FileView figure this out
}

public String getDescription(File f) {
return null; // let the L&F FileView figure this out
}

public Boolean isTraversable(File f) {
return null; // let the L&F FileView figure this out
}

public String getTypeDescription(File f) {
String extension = getExtension(f);
String type = null;

if (extension != null) {
if (extension.equals("jpeg") ||
extension.equals("jpg")) {
type = "JPEG Image";
} else if (extension.equals("gif")){
type = "GIF Image";
} else if (extension.equals("tiff") ||
extension.equals("tif")) {
type = "TIFF Image";
}
}
return type;
}

public Icon getIcon(File f) {
String extension = getExtension(f);
Icon icon = null;
if (extension != null) {
if (extension.equals("jpeg") ||
extension.equals("jpg")) {
icon = jpgIcon;
} else if (extension.equals("gif")) {
icon = gifIcon;
} else if (extension.equals("tiff") ||
extension.equals("tif")) {
icon = tiffIcon;
}
}
return icon;
}

// Get the extension of this file. Code is factored out
// because we use this in both getIcon and getTypeDescription
private String getExtension(File f) {

String ext = null;
String s = f.getName();
int i = s.lastIndexOf('.');

if (i > 0 && i < s.length() - 1) {
ext = s.substring(i+1).toLowerCase();
}
return ext;
}
}
About | Sitemap | Contact