open file with SwingWorker - Java Swing

Java examples for Swing:Swing Thread

Description

open file with SwingWorker

Demo Code

/**/*from w ww  .ja  va2  s . co m*/
 * This file is part of Technic Launcher Core.
 * Copyright ?2015 Syndicate, LLC
 *
 * Technic Launcher Core is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Technic Launcher Core is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License,
 * as well as a copy of the GNU Lesser General Public License,
 * along with Technic Launcher Core.  If not, see <http://www.gnu.org/licenses/>.
 */
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.logging.Level;

public class Main{
    public static void open(final File file) {
        new SwingWorker<Void, Void>() {
            @Override
            protected Void doInBackground() throws Exception {
                Utils.getLogger().info(
                        "Attempting to open " + file.getAbsolutePath());
                String asciiUri = file.toURI().toASCIIString();
                Utils.getLogger().info("Using " + asciiUri);
                if (asciiUri.startsWith("file:")
                        && !asciiUri.startsWith("file://"))
                    asciiUri = asciiUri.replaceFirst("file:", "file://");
                Utils.getLogger().info("Intermediary path " + asciiUri);
                try {
                    Desktop.getDesktop().open(
                            new File(new URI(asciiUri).getPath()));
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

                return null;
            }
        }.execute();
    }
}

Related Tutorials