The Source for Java Technology Collaboration
User: Password:



Evan Summers

Evan Summers's Blog

Cute and Pasty: Browser Opener

Posted by evanx on October 29, 2006 at 11:58 AM | Comments (9)

I'll maintain this page at this permalink.

Here's one way to popup a browser (in Windows), or otherwise prompt the user with the URL, ready for cutting and pasting into your browser.

public class BrowserOpener {
    
    public void showDocument(String url) {
        if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
            try {
                Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
                return;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        showDocumentRequest(url);
    }
    
    protected void showDocumentRequest(String url) {
        JTextField textField = new JTextField(url);
        final JDialog dialog = new JDialog((JFrame) null, "Browser", true);
        dialog.setLayout(new GridBagLayout());
        JPanel panel = new JPanel(new GridBagLayout());
        String message = "Please paste the following URL into your browser. " + 
                "Press Control X to cut.";
        panel.add(new JLabel(message), Gbc.xyi(0, 0, 2));
        panel.add(textField, Gbc.xyi(0, 1, 2).horizontal());
        dialog.add(panel, Gbc.xyi(0, 0, 4).horizontal());
        dialog.setBounds(100, 200, 700, 250);
        dialog.pack();
        dialog.setMinimumSize(new Dimension(600, 40));
        textField.setSelectionEnd(url.length());
        textField.getDocument().addDocumentListener(new DocumentListener() {
            public void changedUpdate(DocumentEvent e) {
            }
            public void insertUpdate(DocumentEvent e) {
            }
            public void removeUpdate(DocumentEvent e) {
                dialog.dispose();
            }
        });
        dialog.setVisible(true);
    }
    
    public static void main(String[] args) {
        new BrowserOpener().showDocumentRequest("http://aptframework.dev.java.net");
        System.exit(1);
    }
}

This uses gridbaglady.dev.java.net ie. Gbc.java.

The following dialog will popup. We already select the URL text, so the user need just press Control X, and it will dispose, thanks to the DocumentListener.

browserOpener.png

If you got some ideas on improving this, eg. for other OSes, and other approaches eg. JNLP's BasicService, please comment, and i'll extend this page. How do you do it?


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • It just occurred to me that one could put the URL directly into the system clipboard (using java.awt.datatransfer package, ie. Clipboard et al) - does anyone do it that way, and does it work on linux and mac?

    Posted by: evanx on October 29, 2006 at 03:05 PM

  • Have a look at http://browserlaunch2.sourceforge.net/.

    Laird

    Posted by: ljnelson on October 29, 2006 at 03:31 PM

  • I would be interested to learn, if I can run a second or more URL's after a while in the same browser instance automatically. Runtime.exec(...) opens a new instance every time it is called.

    Posted by: rbirenheide on October 30, 2006 at 02:33 AM

  • Some time ago I tried to implement a simple class to launch browsers in Windows/Linux/MacOS. You can find an example in programarenjava.blogspot.com. The explanation is in spanish, but I think that the code (at the bottom of the article) is self-explanatory. The code needs more testing, but I think that It works quite well.
    You can get some ideas to make your example work on other platforms.

    Posted by: dcondevigo on October 30, 2006 at 12:00 PM


  • rbirenheide, on my desktop (XP), the above opens new Firefox tab everytime. That might be because Firefox options has "open links from other applications in a new tab in the latest window"?

    Thanks, dcondevigo, that looks great! :)

    Thanks Laird! Wow, this browser stuff can get quite involved, with multiple platforms. Hopefully JDK can offer a standard API for this at some point? maybe JDIC does already?

    Posted by: evanx on October 30, 2006 at 12:18 PM

  • Java 1.6 - Desktop.browse(URI uri)

    Other libraries are out there that do this and they are PAINFUL esp. when you start supporting Solaris, Linux, OS X, etc. So, I won't complain that it took forever to get it into the JDK- I'm just glad it made it eventually.

    Posted by: bwy on October 30, 2006 at 04:15 PM

  • You might be happy to see the new Desktop API available in Java SE 6. This article might help you get started:
    Using the Desktop API in Java SE 6

    Posted by: joconner on October 30, 2006 at 05:48 PM


  • That Desktop API looks great, thanks for that! Wow, this Java6 thing... One really gets the feeling that Java is really getting into its stride now!

    I reckon the floodgates are gonna open, and a new dawn is upon us, and our industry. It's never been this good before. Except maybe for that time when our astronauts walked on the moon for the first time - goddamn that was good ;)

    Posted by: evanx on October 30, 2006 at 07:29 PM

  • Here's a second vote for BrowserLauncher! I use it in AuctionSieve

    Posted by: nevster on November 12, 2006 at 06:13 PM





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds