×

Welcome to TagMyCode

Please login or create account to add a snippet.
1
0
 
1
Language: Java
Posted by: Marcelo Aleks
Added: May 24, 2015 12:36 PM
Views: 9
Tags: no tags
  1.  public static void main(String[] args) throws URISyntaxException {
  2.     final URI uri = new URI("http://java.sun.com");
  3.     class OpenUrlAction implements ActionListener {
  4.       @Override public void actionPerformed(ActionEvent e) {
  5.         open(uri);
  6.       }
  7.     }
  8.     JFrame frame = new JFrame("Links");
  9.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  10.     frame.setSize(100, 400);
  11.     Container container = frame.getContentPane();
  12.     container.setLayout(new GridBagLayout());
  13.     JButton button = new JButton();
  14.     button.setText("<HTML>Click the <FONT color=\"#000099\"><U>link</U></FONT>"
  15.         + " to go to the Java website.</HTML>");
  16.     button.setHorizontalAlignment(SwingConstants.LEFT);
  17.     button.setBorderPainted(false);
  18.     button.setOpaque(false);
  19.     button.setBackground(Color.WHITE);
  20.     button.setToolTipText(uri.toString());
  21.     button.addActionListener(new OpenUrlAction());
  22.     container.add(button);
  23.     frame.setVisible(true);
  24.   }
  25.  
  26.   private static void open(URI uri) {
  27.     if (Desktop.isDesktopSupported()) {
  28.       try {
  29.         Desktop.getDesktop().browse(uri);
  30.       } catch (IOException e) { /* TODO: error handling */ }
  31.     } else { /* TODO: error handling */ }
  32.   }