Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By sehudson
  • 1 Post By Fubarable

Thread: open a web page from JButton?

  1. #1
    adiotrox is offline Member
    Join Date
    Feb 2012
    Posts
    43
    Rep Power
    0

    Question open a web page from JButton?

    can anyone tell me to make the following:
    when the user clicks on the JButton in a frame then gmail.com page gets open? i,e. some how actionPerformed() opens the web browser with gmail.com ??

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: open a web page from JButton?

    Create your button listener and give this a try.
    I believe that the browser you have designated as your default browser will be used to render the webpage:

    Java Code:
            try
              {
                Runtime.getRuntime().exec("cmd /c start http://www.gmail.com");
              } 
            catch (IOException e) 
              {
                System.out.println(e);
              }
    Java applications have an instance of the Runtime class that lets you connect to the environment that your application is running inside of (which can obviously vary). So to get this runtime, you call .getRuntime() on the Runtime class.

    Then, the exec method allows you to execute the string command that you pass to it. It would be the equivalent of running the command inside of a command prompt
    Last edited by sehudson; 03-23-2012 at 03:36 PM.
    adiotrox likes this.

  3. #3
    adiotrox is offline Member
    Join Date
    Feb 2012
    Posts
    43
    Rep Power
    0

    Default Re: open a web page from JButton?

    Awesome sir, you did it in such a simplified way...thank you sir

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: open a web page from JButton?

    But be sure to do this in a background thread so as not to freeze your Swing program.
    adiotrox likes this.

  5. #5
    adiotrox is offline Member
    Join Date
    Feb 2012
    Posts
    43
    Rep Power
    0

    Default Re: open a web page from JButton?

    thanks for the advice

  6. #6
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: open a web page from JButton?

    Quote Originally Posted by Fubarable View Post
    But be sure to do this in a background thread so as not to freeze your Swing program.
    good call.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: open a web page from JButton?

    Better than mucking around with Runtime, check out the API for java.awt.Desktop

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. JButton to open new Page
    By knightwriter in forum AWT / Swing
    Replies: 6
    Last Post: 02-27-2012, 07:03 PM
  2. Open JPanel with JButton
    By web_dev in forum New To Java
    Replies: 7
    Last Post: 06-13-2011, 10:25 PM
  3. open text document using JButton
    By coopc in forum New To Java
    Replies: 3
    Last Post: 05-04-2011, 12:02 AM
  4. How To Open a JFrame Form from a jButton
    By Lyricid in forum AWT / Swing
    Replies: 12
    Last Post: 03-28-2010, 06:53 AM
  5. how to click a jbutton and open an url
    By katie in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 10:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •