Here you can find the source of saveFile(String title, File currentDir, javax.swing.filechooser.FileFilter filter)
Parameter | Description |
---|---|
title | The title of the file-chooser window. |
currentDir | The root directory. If null, new File(".") will be used. |
filter | The file filter to use. |
public static File saveFile(String title, File currentDir, javax.swing.filechooser.FileFilter filter)
//package com.java2s; /*/*from w ww.ja v a 2 s . com*/ * Copyright 2014 Aritz Lopez * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import javax.swing.*; import java.io.File; public class Main { /** * Open a {@link javax.swing.JFileChooser} which can be used to save a new file, and return the path to the new file, or null if closed or cancelled. * * @param title The title of the file-chooser window. * @param currentDir The root directory. If null, {@code new File(".")} will be used. * @return The new file, or {@code null} if none was chosen. */ public static File saveFile(String title, File currentDir) { JFileChooser fileChooser = new JFileChooser(); fileChooser.setCurrentDirectory(currentDir); fileChooser.setDialogTitle(title); fileChooser.setMultiSelectionEnabled(false); int result = fileChooser.showSaveDialog(null); if (result == JFileChooser.APPROVE_OPTION) { return fileChooser.getSelectedFile(); } return null; } /** * Open a {@link javax.swing.JFileChooser} which can be used to save a new file, and return the path to the new file, or null if closed or cancelled. * * @param title The title of the file-chooser window. * @param currentDir The root directory. If null, {@code new File(".")} will be used. * @param filter The file filter to use. * @return The new file, or {@code null} if none was chosen. */ public static File saveFile(String title, File currentDir, javax.swing.filechooser.FileFilter filter) { JFileChooser fileChooser = new JFileChooser(); fileChooser.setCurrentDirectory(currentDir); fileChooser.setDialogTitle(title); fileChooser.setMultiSelectionEnabled(false); fileChooser.setFileFilter(filter); int result = fileChooser.showSaveDialog(null); if (result == JFileChooser.APPROVE_OPTION) { return fileChooser.getSelectedFile(); } return null; } }