Here you can find the source of askUserForFileURL(Component aParent, boolean bOpen)
Parameter | Description |
---|---|
aParent | parent window of this dialog |
bOpen | If it is set to true => dialog is opened in "file open" mode - otherwise in "file save" mode. |
public static String askUserForFileURL(Component aParent, boolean bOpen)
//package com.java2s; /************************************************************************* * * The Contents of this file are made available subject to the terms of * the BSD license.// w w w. ja v a2 s.com * * Copyright 2000, 2010 Oracle and/or its affiliates. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of Sun Microsystems, Inc. nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * *************************************************************************/ import java.awt.*; import javax.swing.*; import java.io.*; import java.net.*; public class Main { /** * @member maLastDir save the last visited directory of used file open/save dialog * @member mnViewCount we try to set unique names on every frame we create (that's why we must count it) */ private static File maLastDir = null; /** * helper to get a file URL selected by user * This method doesn't show any API concepts... * but is necessary for this demo application. * * @param aParent parent window of this dialog * @param bOpen If it is set to true => * dialog is opened in "file open" mode - * otherwise in "file save" mode. */ public static String askUserForFileURL(Component aParent, boolean bOpen) { String sFileURL = null; int nDecision = JFileChooser.CANCEL_OPTION; JFileChooser aChooser = null; // set last visited directory on new file chosser // (if this information is available) if (maLastDir == null) aChooser = new JFileChooser(); else aChooser = new JFileChooser(maLastDir); // decide between file open/save dialog if (bOpen) nDecision = aChooser.showOpenDialog(aParent); else nDecision = aChooser.showSaveDialog(aParent); // react for "OK" result only if (nDecision == JFileChooser.APPROVE_OPTION) { // save current directory as last visited one maLastDir = aChooser.getCurrentDirectory(); // get file URL from the dialog try { sFileURL = aChooser.getSelectedFile().toURI().toURL().toExternalForm(); } catch (MalformedURLException ex) { ex.printStackTrace(); sFileURL = null; } // problem of java: file URL's are coded with 1 slash instead of 3 ones! // => correct this problem first, otherwise office can't use these URL's if ((sFileURL != null) && (sFileURL.startsWith("file:/")) && (!sFileURL.startsWith("file://"))) { StringBuffer sWorkBuffer = new StringBuffer(sFileURL); sWorkBuffer.insert(6, "//"); sFileURL = sWorkBuffer.toString(); } } return sFileURL; } }