List of usage examples for javax.swing JFileChooser getFileFilter
public FileFilter getFileFilter()
From source file:SciTK.Plot.java
/** * Save displayed chart as vector graphics *//*from ww w .j av a 2 s.c om*/ public void saveVectorGraphics() { Rectangle r = chart_panel.getBounds(); // get size of chart // file selection: JFileChooser fc = new JFileChooser(); fc.addChoosableFileFilter(new ExtensionFileFilter("SVG", new String[] { "svg,SVG" })); fc.addChoosableFileFilter(new ExtensionFileFilter("PS", new String[] { "ps,PS" })); fc.addChoosableFileFilter(new ExtensionFileFilter("EPS", new String[] { "eps,EPS" })); // remove default option: fc.setAcceptAllFileFilterUsed(false); // Select a file using the JFileChooser dialog: int fc_return = fc.showSaveDialog(Plot.this); // if the user actually wants to save: if (fc_return == JFileChooser.APPROVE_OPTION) { //get the file: File save_file = fc.getSelectedFile(); // and the type of extension (from file chooser) String extension = fc.getFileFilter().getDescription(); try // try/catch for IO errors { // For each type of extension, check to see if the name includes the // correct extension and then save the image to file. // Default extension is EPS if (extension == "SVG") { // sanity check extension: if (!save_file.getName().endsWith(".svg") && !save_file.getName().endsWith(".SVG")) save_file = new File(save_file.getAbsolutePath() + ".svg"); exportChartAsSVG(chart, r, save_file); } else if (extension == "PS") { // sanity check extension: if (!save_file.getName().endsWith(".ps") && !save_file.getName().endsWith(".PS")) save_file = new File(save_file.getAbsolutePath() + ".ps"); exportChartAsPS(chart, r, save_file, "ps"); } else { // sanity check extension: if (!save_file.getName().endsWith(".eps") && !save_file.getName().endsWith(".EPS")) save_file = new File(save_file.getAbsolutePath() + ".eps"); exportChartAsPS(chart, r, save_file, "eps"); } } catch (IOException e) { DialogError emsg = new DialogError(this, " There was an error saving the file." + System.getProperty("line.separator") + e.getMessage()); } } }
From source file:com.att.aro.ui.model.diagnostic.GraphPanelHelper.java
public void SaveImageAs(JViewport pane, String graphPanelSaveDirectory) { JFileChooser fc = new JFileChooser(graphPanelSaveDirectory); // Set up file types String[] fileTypesJPG = new String[2]; String fileDisplayTypeJPG = ResourceBundleHelper.getMessageString("fileChooser.contentDisplayType.jpeg"); fileTypesJPG[0] = ResourceBundleHelper.getMessageString("fileChooser.contentType.jpeg"); fileTypesJPG[1] = ResourceBundleHelper.getMessageString("fileChooser.contentType.jpg"); FileFilter filterJPG = new ExtensionFileFilter(fileDisplayTypeJPG, fileTypesJPG); fc.addChoosableFileFilter(fc.getAcceptAllFileFilter()); String[] fileTypesPng = new String[1]; String fileDisplayTypePng = ResourceBundleHelper.getMessageString("fileChooser.contentDisplayType.png"); fileTypesPng[0] = ResourceBundleHelper.getMessageString("fileChooser.contentType.png"); FileFilter filterPng = new ExtensionFileFilter(fileDisplayTypePng, fileTypesPng); fc.addChoosableFileFilter(filterPng); fc.setFileFilter(filterJPG);//from w ww.j a v a 2 s .co m File plotImageFile = null; boolean bSavedOrCancelled = false; while (!bSavedOrCancelled) { if (fc.showSaveDialog(pane) == JFileChooser.APPROVE_OPTION) { String strFile = fc.getSelectedFile().toString(); String strFileLowerCase = strFile.toLowerCase(); String fileDesc = fc.getFileFilter().getDescription(); String fileType = ResourceBundleHelper.getMessageString("fileChooser.contentType.jpg"); if ((fileDesc.equalsIgnoreCase( ResourceBundleHelper.getMessageString("fileChooser.contentDisplayType.png")) || strFileLowerCase.endsWith(ResourceBundleHelper.getMessageString("fileType.filters.dot") + fileTypesPng[0].toLowerCase()))) { fileType = fileTypesPng[0]; } if (strFile.length() > 0) { // Save current directory graphPanelSaveDirectory = fc.getCurrentDirectory().getPath(); if ((fileType != null) && (fileType.length() > 0)) { String fileTypeLowerCaseWithDot = ResourceBundleHelper .getMessageString("fileType.filters.dot") + fileType.toLowerCase(); if (!strFileLowerCase.endsWith(fileTypeLowerCaseWithDot)) { strFile += ResourceBundleHelper.getMessageString("fileType.filters.dot") + fileType; } } plotImageFile = new File(strFile); boolean bAttemptToWriteToFile = true; if (plotImageFile.exists()) { if (MessageDialogFactory.showConfirmDialog(pane, MessageFormat.format( ResourceBundleHelper.getMessageString("fileChooser.fileExists"), plotImageFile.getAbsolutePath()), ResourceBundleHelper.getMessageString("fileChooser.confirm"), JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) { bAttemptToWriteToFile = false; } } if (bAttemptToWriteToFile) { try { if (fileType != null && fileType.equalsIgnoreCase(fileTypesPng[0])) { BufferedImage bufImage = ImageHelper.createImage(pane.getBounds().width, pane.getBounds().height); Graphics2D g = bufImage.createGraphics(); pane.paint(g); ImageIO.write(bufImage, "png", plotImageFile); } else { BufferedImage bufImage = ImageHelper.createImage(pane.getBounds().width, pane.getBounds().height); Graphics2D g = bufImage.createGraphics(); pane.paint(g); ImageIO.write(bufImage, "jpg", plotImageFile); } bSavedOrCancelled = true; } catch (IOException e) { MessageDialogFactory.showMessageDialog(pane, ResourceBundleHelper .getMessageString("fileChooser.errorWritingToFile" + plotImageFile.toString())); } } } } else { bSavedOrCancelled = true; } } }
From source file:electroStaticUI.ElectroStaticUIContainer.java
private void buildFileMenu() { //create Exit menu item exitItem = new JMenuItem("Exit"); exitItem.setMnemonic(KeyEvent.VK_X); exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0);// www. j av a2s . c om } }); //open open = new JMenuItem("Open"); open.setMnemonic(KeyEvent.VK_P); open.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser fChooser = new JFileChooser(); int status = fChooser.showSaveDialog(null); } }); //close close = new JMenuItem("Close"); close.setMnemonic(KeyEvent.VK_C); //export export = new JMenuItem("Export"); export.setMnemonic(KeyEvent.VK_E); export.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser fChooser = new JFileChooser(); fChooser.setMultiSelectionEnabled(false); fChooser.setAcceptAllFileFilterUsed(false); FileNameExtensionFilter svgFilter = new FileNameExtensionFilter("Save Vector Plot(svg)", ".svg"); FileNameExtensionFilter pngFilter = new FileNameExtensionFilter("Save Voltage Surface Plot(png)", ".png"); fChooser.addChoosableFileFilter(svgFilter); fChooser.addChoosableFileFilter(pngFilter); fChooser.setFileFilter(svgFilter); int status = fChooser.showSaveDialog(rootPane); if (status == JFileChooser.APPROVE_OPTION) { if (fChooser.getFileFilter() == svgFilter) { try { saveAsName = fChooser.getSelectedFile().getCanonicalPath(); Save.saveChartToSVG(DefaultValues.getChartToSave(), saveAsName, displayPanel.getWidth(), displayPanel.getHeight()); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else if (fChooser.getFileFilter() == pngFilter) { try { saveAsName = fChooser.getSelectedFile().getCanonicalPath(); Save.saveChart3dToPNG(DefaultValues.get3dChartToSave(), saveAsName); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } } }); //save save = new JMenuItem("Save"); save.setMnemonic(KeyEvent.VK_S); save.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser fChooser = new JFileChooser(); int status = fChooser.showSaveDialog(null); if (status == JFileChooser.APPROVE_OPTION) { try { System.out.println("Height: " + displayPanel.getWidth()); System.out.println("Width: " + displayPanel.getHeight()); saveAsName = fChooser.getSelectedFile().getCanonicalPath(); Save.saveChartToSVG(DefaultValues.getChartToSave(), saveAsName, displayPanel.getWidth(), displayPanel.getHeight()); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } }); //saveAs saveAs = new JMenuItem("Save As"); saveAs.setMnemonic(KeyEvent.VK_A); saveAs.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser fChooser = new JFileChooser(); int status = fChooser.showSaveDialog(null); if (status == JFileChooser.APPROVE_OPTION) try { System.out.println("Height: " + displayPanel.getWidth()); System.out.println("Width: " + displayPanel.getHeight()); saveAsName = fChooser.getSelectedFile().getCanonicalPath(); System.out.println(saveAsName); Save.saveChartToSVG(DefaultValues.getChartToSave(), saveAsName, displayPanel.getWidth(), displayPanel.getHeight()); } catch (IOException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } try { Save.saveChartToSVG(UserInput.getElectricFieldChart(), saveAsName, displayPanel.getWidth(), displayPanel.getHeight()); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); //JMenu object for file menu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); //fileMenu.add(newFile); //fileMenu.add(open); //fileMenu.add(close); fileMenu.add(export); //fileMenu.add(save); //fileMenu.add(saveAs); fileMenu.add(exitItem); }
From source file:com.igormaznitsa.zxpoly.MainForm.java
private File chooseFileForOpen(final String title, final File initial, final AtomicReference<FileFilter> selectedFilter, final FileFilter... filter) { final JFileChooser chooser = new JFileChooser(initial); for (final FileFilter f : filter) { chooser.addChoosableFileFilter(f); }// w w w . j a v a 2 s. co m chooser.setAcceptAllFileFilterUsed(false); chooser.setMultiSelectionEnabled(false); chooser.setDialogTitle(title); chooser.setFileFilter(filter[0]); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); final File result; if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { result = chooser.getSelectedFile(); if (selectedFilter != null) { selectedFilter.set(chooser.getFileFilter()); } } else { result = null; } return result; }
From source file:org.gumtree.vis.awt.CompositePanel.java
/** * Opens a file chooser and gives the user an opportunity to save the chart * in PNG format./*w ww . j a v a 2 s . c o m*/ * * @throws IOException if there is an I/O error. */ @Override public void doSaveAs() throws IOException { JFileChooser fileChooser = new JFileChooser(); String currentDirectory = System.getProperty(StaticValues.SYSTEM_SAVE_PATH_LABEL); if (currentDirectory != null) { File savePath = new File(currentDirectory); if (savePath.exists() && savePath.isDirectory()) { fileChooser.setCurrentDirectory(savePath); } } ExtensionFileFilter pngFilter = new ExtensionFileFilter("PNG_Image_Files", ".png"); ExtensionFileFilter jpgFilter = new ExtensionFileFilter("JPG_Image_Files", ".jpg"); fileChooser.addChoosableFileFilter(pngFilter); fileChooser.addChoosableFileFilter(jpgFilter); int option = fileChooser.showSaveDialog(this); if (option == JFileChooser.APPROVE_OPTION) { String filename = fileChooser.getSelectedFile().getPath(); String selectedDescription = fileChooser.getFileFilter().getDescription(); String fileExtension = StaticValues.DEFAULT_IMAGE_FILE_EXTENSION; if (selectedDescription.toLowerCase().contains("png")) { fileExtension = "png"; if (!filename.toLowerCase().endsWith(".png")) { filename = filename + ".png"; } } else if (selectedDescription.toLowerCase().contains("jpg")) { fileExtension = "jpg"; if (!filename.toLowerCase().endsWith(".jpg")) { filename = filename + ".jpg"; } } File selectedFile = new File(filename); int confirm = JOptionPane.YES_OPTION; if (selectedFile.exists()) { confirm = JOptionPane.showConfirmDialog(this, selectedFile.getName() + " exists, overwrite?", "Confirm Overwriting", JOptionPane.YES_NO_OPTION); } if (confirm == JOptionPane.YES_OPTION) { saveTo(filename, fileExtension); System.setProperty(StaticValues.SYSTEM_SAVE_PATH_LABEL, fileChooser.getSelectedFile().getParent()); } } }
From source file:org.fhaes.fhsamplesize.view.FHSampleSize.java
/** * Open a JFileChooser and return the file that the user specified for saving. Takes a parameter that specifies the type of file. Either * TAB or PNG./*from w w w .ja v a2 s . c om*/ * * @return */ private File getFileFromSaveDialog(String fileTypeToSave) { String lastVisitedFolder = App.prefs.getPref(PrefKey.PREF_LAST_EXPORT_FOLDER, null); JFileChooser fc = new JFileChooser(lastVisitedFolder); File outputFile; if (fileTypeToSave == "TAB") { TABFilter filterTAB = new TABFilter(); fc.addChoosableFileFilter(filterTAB); fc.setFileFilter(filterTAB); fc.setDialogTitle("Export table as text file..."); } else if (fileTypeToSave == "PDF") { PDFFilter filterPDF = new PDFFilter(); fc.addChoosableFileFilter(filterPDF); fc.setFileFilter(filterPDF); fc.setDialogTitle("Export chart as PDF..."); } fc.setFileSelectionMode(JFileChooser.FILES_ONLY); fc.setMultiSelectionEnabled(false); int returnVal = fc.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { outputFile = fc.getSelectedFile(); if (FileUtils.getExtension(outputFile.getAbsolutePath()) == "") { log.debug("Output file extension not set by user"); if (fc.getFileFilter().getDescription().equals(new CSVFileFilter().getDescription())) { log.debug("Adding csv extension to output file name"); outputFile = new File(outputFile.getAbsolutePath() + ".csv"); } else if (fc.getFileFilter().getDescription().equals(new PDFFilter().getDescription())) { log.debug("Adding pdf extension to output file name"); outputFile = new File(outputFile.getAbsolutePath() + ".pdf"); } } else { log.debug("Output file extension set my user to '" + FileUtils.getExtension(outputFile.getAbsolutePath()) + "'"); } App.prefs.setPref(PrefKey.PREF_LAST_EXPORT_FOLDER, outputFile.getAbsolutePath()); } else { return null; } if (outputFile.exists()) { Object[] options = { "Overwrite", "No", "Cancel" }; // notes about parameters: null (don't use custom icon), options (the titles of buttons), options[0] (default button title) int response = JOptionPane.showOptionDialog(App.mainFrame, "The file '" + outputFile.getName() + "' already exists. Are you sure you want to overwrite?", "Confirm", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if (response != JOptionPane.YES_OPTION) return null; } return outputFile; }
From source file:com.openbravo.pos.util.JRViewer411.java
void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed // Add your handling code here: JFileChooser fileChooser = new JFileChooser(); fileChooser.setLocale(this.getLocale()); fileChooser.updateUI();/*from w w w . j a va 2 s . co m*/ for (int i = 0; i < saveContributors.size(); i++) { fileChooser.addChoosableFileFilter(saveContributors.get(i)); } if (saveContributors.contains(lastSaveContributor)) { fileChooser.setFileFilter(lastSaveContributor); } else if (saveContributors.size() > 0) { fileChooser.setFileFilter(saveContributors.get(0)); } if (lastFolder != null) { fileChooser.setCurrentDirectory(lastFolder); } int retValue = fileChooser.showSaveDialog(this); if (retValue == JFileChooser.APPROVE_OPTION) { FileFilter fileFilter = fileChooser.getFileFilter(); File file = fileChooser.getSelectedFile(); lastFolder = file.getParentFile(); JRSaveContributor contributor = null; if (fileFilter instanceof JRSaveContributor) { contributor = (JRSaveContributor) fileFilter; } else { int i = 0; while (contributor == null && i < saveContributors.size()) { contributor = saveContributors.get(i++); if (!contributor.accept(file)) { contributor = null; } } if (contributor == null) { contributor = new JRPrintSaveContributor(getLocale(), this.resourceBundle); } } lastSaveContributor = contributor; try { contributor.save(jasperPrint, file); } catch (JRException e) { if (log.isErrorEnabled()) { log.error("Save error.", e); } JOptionPane.showMessageDialog(this, getBundleString("error.saving")); } } }
From source file:neg.JRViewer.java
void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed // Add your handling code here: JFileChooser fileChooser = new JFileChooser(); fileChooser.setLocale(this.getLocale()); fileChooser.updateUI();/*from w w w . ja v a 2 s.com*/ for (int i = 0; i < saveContributors.size(); i++) { fileChooser.addChoosableFileFilter((JRSaveContributor) saveContributors.get(i)); } if (saveContributors.contains(lastSaveContributor)) { fileChooser.setFileFilter(lastSaveContributor); } else if (saveContributors.size() > 0) { fileChooser.setFileFilter((JRSaveContributor) saveContributors.get(0)); } if (lastFolder != null) { fileChooser.setCurrentDirectory(lastFolder); } int retValue = fileChooser.showSaveDialog(this); if (retValue == JFileChooser.APPROVE_OPTION) { FileFilter fileFilter = fileChooser.getFileFilter(); File file = fileChooser.getSelectedFile(); lastFolder = file.getParentFile(); JRSaveContributor contributor = null; if (fileFilter instanceof JRSaveContributor) { contributor = (JRSaveContributor) fileFilter; } else { int i = 0; while (contributor == null && i < saveContributors.size()) { contributor = (JRSaveContributor) saveContributors.get(i++); if (!contributor.accept(file)) { contributor = null; } } if (contributor == null) { contributor = new JRPrintSaveContributor(getLocale(), this.resourceBundle); } } lastSaveContributor = contributor; try { contributor.save(jasperPrint, file); } catch (JRException e) { if (log.isErrorEnabled()) log.error("Save error.", e); JOptionPane.showMessageDialog(this, getBundleString("error.saving")); } } }
From source file:cn.pholance.datamanager.common.components.JRViewer.java
void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed // Add your handling code here: JFileChooser fileChooser = new JFileChooser(); fileChooser.setLocale(this.getLocale()); fileChooser.updateUI();/*from w ww .jav a2 s. c o m*/ for (int i = 0; i < saveContributors.size(); i++) { fileChooser.addChoosableFileFilter(saveContributors.get(i)); } if (saveContributors.contains(lastSaveContributor)) { fileChooser.setFileFilter(lastSaveContributor); } else if (saveContributors.size() > 0) { fileChooser.setFileFilter(saveContributors.get(0)); } if (lastFolder != null) { fileChooser.setCurrentDirectory(lastFolder); } int retValue = fileChooser.showSaveDialog(this); if (retValue == JFileChooser.APPROVE_OPTION) { FileFilter fileFilter = fileChooser.getFileFilter(); File file = fileChooser.getSelectedFile(); lastFolder = file.getParentFile(); JRSaveContributor contributor = null; if (fileFilter instanceof JRSaveContributor) { contributor = (JRSaveContributor) fileFilter; } else { int i = 0; while (contributor == null && i < saveContributors.size()) { contributor = saveContributors.get(i++); if (!contributor.accept(file)) { contributor = null; } } if (contributor == null) { contributor = new JRPrintSaveContributor(this.jasperReportsContext, getLocale(), this.resourceBundle); } } lastSaveContributor = contributor; try { contributor.save(jasperPrint, file); } catch (JRException e) { if (log.isErrorEnabled()) { log.error("Save error.", e); } JOptionPane.showMessageDialog(this, getBundleString("error.saving")); } } }
From source file:org.gumtree.vis.awt.JChartPanel.java
@Override public void doSaveAs() throws IOException { JFileChooser fileChooser = new JFileChooser(); String currentDirectory = System.getProperty(StaticValues.SYSTEM_SAVE_PATH_LABEL); if (currentDirectory != null) { File savePath = new File(currentDirectory); if (savePath.exists() && savePath.isDirectory()) { fileChooser.setCurrentDirectory(savePath); }/*from w w w.j a v a 2 s. c om*/ } ExtensionFileFilter ascFilter = new ExtensionFileFilter("Text_Files", ".txt"); ExtensionFileFilter jpgFilter = new ExtensionFileFilter("JPG_Image_Files", ".jpg"); ExtensionFileFilter pngFilter = new ExtensionFileFilter("PNG_Image_Files", ".png"); fileChooser.addChoosableFileFilter(pngFilter); fileChooser.addChoosableFileFilter(jpgFilter); fileChooser.addChoosableFileFilter(ascFilter); fileChooser.setFileFilter(jpgFilter); int option = fileChooser.showSaveDialog(this); if (option == JFileChooser.APPROVE_OPTION) { String filename = fileChooser.getSelectedFile().getPath(); String selectedDescription = fileChooser.getFileFilter().getDescription(); String fileExtension = StaticValues.DEFAULT_IMAGE_FILE_EXTENSION; if (selectedDescription.toLowerCase().contains("png")) { fileExtension = "png"; if (!filename.toLowerCase().endsWith(".png")) { filename = filename + ".png"; } } else if (selectedDescription.toLowerCase().contains("jpg")) { fileExtension = "jpg"; if (!filename.toLowerCase().endsWith(".jpg")) { filename = filename + ".jpg"; } } else if (selectedDescription.toLowerCase().contains("text")) { fileExtension = "txt"; if (!filename.toLowerCase().endsWith(".txt")) { filename = filename + ".txt"; } } File selectedFile = new File(filename); int confirm = JOptionPane.YES_OPTION; if (selectedFile.exists()) { confirm = JOptionPane.showConfirmDialog(this, selectedFile.getName() + " exists, overwrite?", "Confirm Overwriting", JOptionPane.YES_NO_OPTION); } if (confirm == JOptionPane.YES_OPTION) { saveTo(filename, fileExtension); System.setProperty(StaticValues.SYSTEM_SAVE_PATH_LABEL, fileChooser.getSelectedFile().getParent()); } } }