1

I am trying to create a GUI-based program in Java that has a 'Submit' button and when clicked, it takes you to a website.

I have read about the URL and URLConnection classes online and the connection to the website is established but the program does not open the link... This is what I have so far:

if(command.equals("Submit data"))
    {
        try {
            URL myURL = new URL("http://google.com/");
            URLConnection myURLConnection = myURL.openConnection();
            myURLConnection.connect();
        } 
        catch (IOException t) {   
            // openConnection() failed
            // ...
        }
}

The connection seems to be established but I want the program to open up the browser and take to the website.. I've tried everything and no luck.. Thank you

5
  • Just a question, if the URL is hardcoded why are you catching a malformed url exception? Commented Aug 18, 2013 at 2:43
  • my bad.. I was trying other ways to create the URL and forgot to erase these lines of code.. thanks for noticing Commented Aug 18, 2013 at 2:45
  • Oh no, I was just asking, I didn't know if you were getting user's input somewhere - I was curious. Totally okay to catch that exception though, doesn't hurt anything. Commented Aug 18, 2013 at 2:47
  • @jeva006 : Please have a look at Working with URLs, hope it helps :-) Commented Aug 18, 2013 at 9:39
  • Thanks @nIcEcOw, I was reading that last night and the explanation became much clearer.. It's something good to know Commented Aug 19, 2013 at 0:16

1 Answer 1

4

You could either used a swing component like you can see in this thread --> Best Java/Swing browser component?

Otherwise use this snippet found at http://andy.ekiwi.de/?p=1026

public void openUrl(String url) throws IOException, URISyntaxException {
  if(java.awt.Desktop.isDesktopSupported() ) {
        java.awt.Desktop desktop = java.awt.Desktop.getDesktop();

        if(desktop.isSupported(java.awt.Desktop.Action.BROWSE) ) {
          java.net.URI uri = new java.net.URI(url);
              desktop.browse(uri);
        }
      }
}
Sign up to request clarification or add additional context in comments.

5 Comments

That's the only way that seems to open the URL but I wanted to try it with the URL and URLConnection classes. I assume that since I'm working with these classes, there must be a way to open the link. Thanks, though.
Don't confuse opening a link with downloading the content from that data stream and displaying this content. The above code simply launches the client's default web browser (doesn't work on Mac though). In the code of your question you aren't downloading or displaying anything from the URL.
Do you want to show the website inside your swing program or do you want to open an external browser?
Yep, you're right! They are two different things. I guess I'll to use the Desktop object methods to perform what I want.. Thanks a lot man
@Templar I wanted it to open on an external browser for now.. I have just started working with URLs in Java so I wanted to know what was in there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.