Desktop Help Applications : Desktop « JDK 6 « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » JDK 6 » Desktop 




Desktop Help Applications
  

import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class DesktopDemo {
  static Desktop desktop;
  public static void main(String[] args) {
    
    if (Desktop.isDesktopSupported()) {
      desktop = Desktop.getDesktop();
    else {
      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 [email protected]");
    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:[email protected]");
          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(300100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setJMenuBar(jMenuBar);
    frame.setVisible(true);

  }
}

          
  














Related examples in the same category
1.Using the Desktop class to launch a URL with default browser
2.Invoke the default editor to edit a file
3.Using the system default setting to open a file
4.Open a office word file with Java
5.Using system default printer to print a file out
6.Send out email
7.Open Mail client
8.This program demonstrates the desktop app API.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.