Example usage for java.awt Desktop getDesktop

List of usage examples for java.awt Desktop getDesktop

Introduction

In this page you can find the example usage for java.awt Desktop getDesktop.

Prototype

public static synchronized Desktop getDesktop() 

Source Link

Document

Returns the Desktop instance of the current desktop context.

Usage

From source file:Main.java

private static void openWebpage(URI uri) throws IOException {
    Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
    if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
        desktop.browse(uri);//from   w  ww .  j  av a2 s  .  c  om
    }
}

From source file:Main.java

/**
 * Create a HTML hyperlink in JLabel component
 *
 * @param label//  ww w .  j a  v a 2s.  c  om
 * @param url
 * @param text
 */
public static void createHyperLink(JLabel label, final String url, String text) {
    label.setToolTipText(url);
    label.setText("<html><a href=\"\">" + text + "</a></html>");
    label.setCursor(new Cursor(Cursor.HAND_CURSOR));
    label.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            try {
                Desktop.getDesktop().browse(new URI(url));
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            } catch (URISyntaxException ex) {
                throw new RuntimeException(ex);
            }
        }
    });
}

From source file:Main.java

public static boolean launchURL(String urlString) {
    try {/*  w  ww.j  ava  2  s.  c om*/
        Desktop.getDesktop().browse(URI.create(urlString));
        return true;
    } catch (IOException e) {
        return false;
    }
}

From source file:Main.java

/**
 * Browse to a folder./* w ww . j  ava2 s  .  co  m*/
 * 
 * @param file the path
 * @param component the component
 */
public static void browseDir(File file, Component component) {
    try {
        Desktop.getDesktop().browse(new URL("file://" + file.getAbsolutePath()).toURI());
    } catch (IOException e) {
        JOptionPane.showMessageDialog(component,
                "Unable to open '" + file.getAbsolutePath() + "'. Maybe it doesn't exist?", "Open failed",
                JOptionPane.ERROR_MESSAGE);
    } catch (URISyntaxException e) {
    }
}

From source file:Main.java

public static void open(File f) {
    char ch = File.separatorChar;
    if (ch == '\\') {
        openWindows(f);//  ww  w .  jav  a2s  .c  o m
    } else {
        try {
            Desktop.getDesktop().open(f);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:org.jdal.system.SystemUtils.java

public static void open(byte[] data, String extension) {
    if (data != null && Desktop.isDesktopSupported()) {
        Desktop desktop = Desktop.getDesktop();
        File file;// w  w  w.  j  av  a2  s.c  o m
        try {
            file = File.createTempFile("tmp", "." + extension);
            file.deleteOnExit();
            FileUtils.writeByteArrayToFile(file, data);
            desktop.open(file);
        } catch (IOException e) {
            String message = "No ha sido posible abrir el fichero";
            JOptionPane.showMessageDialog(null, message, "Error de datos", JOptionPane.ERROR_MESSAGE);
        }
    }
}

From source file:org.openstreetmap.josm.plugins.mapillary.utils.MapillaryUtils.java

/**
 * Open the default browser in the given URL.
 *
 * @param url The (not-null) URL that is going to be opened.
 * @throws IOException when the URL could not be opened
 *//*from  w w  w .j  a v  a  2  s  .  c  om*/
public static void browse(URL url) throws IOException {
    if (url == null) {
        throw new IllegalArgumentException();
    }
    Desktop desktop = Desktop.getDesktop();
    if (desktop.isSupported(Desktop.Action.BROWSE)) {
        try {
            desktop.browse(url.toURI());
        } catch (URISyntaxException e1) {
            throw new IOException(e1);
        }
    } else {
        Runtime runtime = Runtime.getRuntime();
        runtime.exec("xdg-open " + url);
    }
}

From source file:org.jdal.system.SystemUtils.java

public static void open(File file) {
    if (Desktop.isDesktopSupported())
        try {/*from w  ww.  jav  a 2s.  c  om*/
            Desktop.getDesktop().open(file);
        } catch (IOException e) {
            log.error(e);
        }
}

From source file:com.net2plan.utils.HTMLUtils.java

/**
 * Opens a browser for the given {@code URI}. It is supposed to avoid issues with KDE systems.
 *
 * @param uri URI to browse//from   w  w  w .  ja v a  2s. c  o m
 */
public static void browse(URI uri) {
    try {
        if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
            Desktop.getDesktop().browse(uri);
            return;
        } else if (SystemUtils.IS_OS_UNIX
                && (new File("/usr/bin/xdg-open").exists() || new File("/usr/local/bin/xdg-open").exists())) {
            new ProcessBuilder("xdg-open", uri.toString()).start();
            return;
        }

        throw new UnsupportedOperationException(
                "Your operating system does not support launching browser from Net2Plan");
    } catch (IOException | UnsupportedOperationException e) {
        throw new RuntimeException(e);
    }
}

From source file:test.be.fedict.eid.applet.DesktopTest.java

@Test
public void mailto() throws Exception {
    Desktop desktop = Desktop.getDesktop();
    URI mailUri = new URI("mailto:frank.cornelis@fedict.be?subject="
            + URLEncoder.encode("Hello World", "UTF-8").replaceAll("\\+", "%20") + "&cc="
            + URLEncoder.encode("frank.cornelis@fedict.be", "UTF-8") + "&body="
            + URLEncoder.encode("test body message", "UTF-8").replaceAll("\\+", "%20"));
    LOG.debug("mail uri: " + mailUri);
    desktop.mail(mailUri);/*from ww  w . j  a  v a2s  .  co m*/
}