Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Simple swing application

Posted on 2007-11-19
2
209 Views
Last Modified: 2008-02-01
I've been challenged to figure out what's wrong with this application. It seems to run as intended.

So far, I've came up with the following:

1) The EventDispatchThread is not respected; however, it is highly unlikely to cause an issue with this particular application.
2) The email address in the label and mailto do not match.
3) System.out.println(ex.getMessage()); >> ex.printStackTrace();


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 wtf {
  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 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);
    
  }
}

Open in new window

0
Comment
Question by:modsiw
  • 2
2 Comments
 
LVL 16

Accepted Solution

by:
ellandrd earned 500 total points
ID: 20315074
has this example anything to do with this:

http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/desktop_api/

anyways i cant see much wrong but i would change some of the exception types like this:

try {
   URI browseURI = new URI("www.java2s.com");
   desktop.browse(browseURI);
} catch (Exception ex) {
   System.out.println(ex.getMessage());
}

should be:

try {
   URI browseURI = new URI("www.java2s.com");
   desktop.browse(browseURI);
} catch (IOException ex) {
   System.err.println(ex.getMessage());
} catch(URISyntaxException use) {
   System.err.println(use.printStackTrace());
}

ellandrd
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 20459650
what a grade B?  what did i do wrong?

ellandrd
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 26
Is Applet the way to go for my drag and drop system? 8 25
Java syntax, or is it Selenium 6 30
Running JavaFX on the Raspberry Pi 27 41
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question