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

public static void main(String[] args) throws Exception {
    JFileChooser fileChooser = new JFileChooser();
    int a = fileChooser.showOpenDialog(null);

    if (a == JFileChooser.APPROVE_OPTION) {
        File fileToOpen = fileChooser.getSelectedFile();
        Desktop.getDesktop().open(fileToOpen);
    }// w w w  .  j a  v  a2s  . c o m
}

From source file:DesktopDemo.java

public static void main(String[] args) {

    if (Desktop.isDesktopSupported()) {
        desktop = Desktop.getDesktop();
    } else {//from  w ww.  j a va  2s  .  co m
        System.out.println("Desktop class is not supported");
        System.exit(1);
    }
    JMenuItem openItem = new JMenuItem("Open");
    JMenuItem editItem = new JMenuItem("Edit");
    JMenuItem printItem = new JMenuItem("Print");
    JMenuItem browseToItem = new JMenuItem("Go to www.java2s.com");
    JMenuItem mailToItem = new JMenuItem("Email to a@java.com");
    JMenu fileMenu = new JMenu("File");
    JMenu mailMenu = new JMenu("Email");
    JMenu browseMenu = new JMenu("Browser");

    openItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                try {
                    desktop.open(chooser.getSelectedFile().getAbsoluteFile());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    });
    fileMenu.add(openItem);

    editItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                try {
                    desktop.edit(chooser.getSelectedFile().getAbsoluteFile());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    });
    fileMenu.add(editItem);

    printItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                try {
                    desktop.print(chooser.getSelectedFile().getAbsoluteFile());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    });
    fileMenu.add(printItem);

    browseToItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                URI browseURI = new URI("www.java2s.com");
                desktop.browse(browseURI);
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
    });
    browseMenu.add(browseToItem);

    mailToItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                URI mailURI = new URI("mailto:support@java.com");
                desktop.mail(mailURI);
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
    });
    mailMenu.add(mailToItem);

    JMenuBar jMenuBar = new JMenuBar();
    jMenuBar.add(fileMenu);
    jMenuBar.add(browseMenu);
    jMenuBar.add(mailMenu);

    JFrame frame = new JFrame();
    frame.setTitle("Desktop Helper Applications");
    frame.setSize(300, 100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setJMenuBar(jMenuBar);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] argv) {
    JEditorPane jep = new JEditorPane();
    jep.setContentType("text/html");
    StringBuilder sb = new StringBuilder();
    sb.append("<b>Welcome</b>:<br><hr>");
    for (int i = 1; i <= 3; i++) {
        sb.append(create(i));/*from w w  w .j a  va 2s.  c  o m*/
    }
    sb.append("<hr>");
    jep.setText(sb.toString());
    jep.setEditable(false);
    jep.addHyperlinkListener(e -> {
        if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
            System.out.println(e.getURL());
            Desktop desktop = Desktop.getDesktop();
            try {
                desktop.browse(e.getURL().toURI());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(jep);
    f.pack();
    f.setVisible(true);
}

From source file:de.brazzy.nikki.util.ThumbnailBenchmark.java

public static void main(String[] args) throws Exception {
    ImageReader r = new ImageReader(new File("C:/tmp/test.JPG"), DateTimeZone.UTC);
    r.readMainImage();//from w  w w .ja  va 2s.co  m
    long start = System.nanoTime();
    byte[] t = r.scale(150, false, true);
    System.out.println();
    System.out.println("ThumpnailRescaleOp: " + (System.nanoTime() - start) / (1000 * 1000 * 1000.0));
    File out = File.createTempFile("thumbnail", ".jpg");
    FileOutputStream stream = new FileOutputStream(out);
    IOUtils.write(t, stream);
    stream.close();
    Desktop.getDesktop().open(out);

    start = System.nanoTime();
    t = r.scale(150, false, false);
    System.out.println();
    System.out.println("ResampleOp: " + (System.nanoTime() - start) / (1000 * 1000 * 1000.0));
    out = File.createTempFile("thumbnail", ".jpg");
    stream = new FileOutputStream(out);
    IOUtils.write(t, stream);
    stream.close();
    Desktop.getDesktop().open(out);
}

From source file:com.rest.samples.getReportFromJasperServer.java

public static void main(String[] args) {
    // TODO code application logic here
    String url = "http://username:password@10.49.28.3:8081/jasperserver/rest_v2/reports/Reportes/vencimientos.pdf?feini=2016-09-30&fefin=2016-09-30";
    try {//from w  w  w.j  a  v  a  2  s .  co  m
        HttpClient hc = HttpClientBuilder.create().build();
        HttpGet getMethod = new HttpGet(url);
        getMethod.addHeader("accept", "application/pdf");
        HttpResponse res = hc.execute(getMethod);
        if (res.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException("Failed : HTTP eror code: " + res.getStatusLine().getStatusCode());
        }

        InputStream is = res.getEntity().getContent();
        OutputStream os = new FileOutputStream(new File("vencimientos.pdf"));
        int read = 0;
        byte[] bytes = new byte[2048];

        while ((read = is.read(bytes)) != -1) {
            os.write(bytes, 0, read);
        }
        is.close();
        os.close();

        if (Desktop.isDesktopSupported()) {
            File pdfFile = new File("vencimientos.pdf");
            Desktop.getDesktop().open(pdfFile);
        }

    } catch (IOException ex) {
        Logger.getLogger(SamplesUseHttpclient.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.n52.geolabel.client.example.MyClient.java

public static void main(String[] args) throws IOException {
    // create a new (empty) request builder
    GeoLabelRequestBuilder builder = GeoLabelClientV1.createGeoLabelRequest();

    // find out a metadata URL and generate a label for it...
    String metadataUrl = "http://www.geoportal.org/geoportal/vrd?sourceType=USGSGN&uuid=NASA_LIS_DATA";
    builder = builder.setDesiredSize(200).setMetadataDocument(metadataUrl);

    InputStream svg = builder.getSVG();
    String svgString = IOUtils.toString(svg);
    System.out.println(svgString);

    // set another rendering server
    svgString = IOUtils.toString(/*from   w  w w.j a v a  2  s . c o  m*/
            GeoLabelClientV1.createGeoLabelRequest("http://geoviqua.dev.52north.org/glbservice/api/v1/svg/")
                    .setMetadataDocument(metadataUrl).getSVG());
    System.out.println(svgString);

    // display the file in a browser
    File tempFile = File.createTempFile("geolabel-", ".svg");
    FileUtils.writeStringToFile(tempFile, svgString);
    // open the default web browser for the HTML page
    Desktop.getDesktop().browse(tempFile.toURI());
    // if a web browser is the default HTML handler, this might work too
    // Desktop.getDesktop().open(htmlFile);
}

From source file:com.msopentech.ThaliClient.ProxyDesktop.java

public static void main(String[] rgs) throws InterruptedException, URISyntaxException, IOException {

    final ProxyDesktop instance = new ProxyDesktop();
    try {/*from w  w w.ja va  2 s.com*/
        instance.initialize();
    } catch (RuntimeException e) {
        System.out.println(e);
    }

    // Attempt to launch the default browser to our page
    if (Desktop.isDesktopSupported()) {
        Desktop.getDesktop().browse(new URI("http://localhost:" + localWebserverPort));
    }

    // Register to shutdown the server properly from a sigterm
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            instance.shutdown();
        }
    });

    // Let user press enter to kill the console session
    Console console = System.console();
    if (console != null) {
        console.format("\nPress ENTER to exit.\n");
        console.readLine();
        instance.shutdown();
    } else {
        // Don't exit on your own when running without a console (debugging in an IDE).
        while (true) {
            Thread.sleep(500);
        }
    }
}

From source file:it.anyplace.sync.webclient.Main.java

public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption("C", "set-config", true, "set config file for s-client");
    options.addOption("h", "help", false, "print help");
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = parser.parse(options, args);

    if (cmd.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("s-client", options);
        return;//from w  w w  .j  a  va2  s .c o  m
    }

    File configFile = cmd.hasOption("C") ? new File(cmd.getOptionValue("C"))
            : new File(System.getProperty("user.home"), ".s-client.properties");
    logger.info("using config file = {}", configFile);
    try (ConfigurationService configuration = ConfigurationService.newLoader().loadFrom(configFile)) {
        FileUtils.cleanDirectory(configuration.getTemp());
        KeystoreHandler.newLoader().loadAndStore(configuration);
        logger.debug("{}", configuration.getStorageInfo().dumpAvailableSpace());
        try (HttpService httpService = new HttpService(configuration)) {
            httpService.start();
            if (Desktop.isDesktopSupported()) {
                Desktop.getDesktop().browse(
                        URI.create("http://localhost:" + httpService.getPort() + "/web/webclient.html"));
            }
            httpService.join();
        }
    }
}

From source file:de.tbuchloh.kiskis.gui.feedback.SubmitBugTest.java

public static void main(String[] args) throws Exception {
    Desktop.getDesktop().mail();
    new SubmitBug().openMail(TextMessageBox.exceptionToString(new Exception()));
}

From source file:de.tbuchloh.kiskis.gui.feedback.SubmitFeatureRequestTest.java

public static void main(String[] args) throws Exception {
    Desktop.getDesktop().mail();
    new SubmitFeatureRequest().openMail(TextMessageBox.exceptionToString(new Exception()));
}