code3h
code3h

Reputation: 11

open a web link in java netbeans

I want to open a web page after clicking a button. I have a button and a text field. in that text field i type a web address. then i click the button.after that i want to show the result in my frame. is this possible? is there a component in palette to do so? .can i open a web page in java frame instead of opening in a browser. If it's impossible please guide me to open a web page in chrome or IE using java in net beans. Thanks in advance.

i tried this in event of button .

 try {
        Runtime r = Runtime.getRuntime();
        r.exec("C:\Users\K-9\AppData\Local\Google\Chrome\Application\chrome.exe");
    } catch (Exception e) {
        e.printStackTrace();
    }

Am i in a correct path.?

Upvotes: 1

Views: 6428

Answers (2)

2sku
2sku

Reputation: 31

1 .I have made a new project in that I have selected a new frame and drag a button from the swing controls to your frame.

  1. In the buttons action event type the following code

    String url="www.google.com";

    java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));

  2. Then run the project

And you can also follow this tutorial

Open a web link in java netbeans

Upvotes: 1

KIC
KIC

Reputation: 6121

Since you use java you need to escape back slashes. This code perfectly works for me:

    String fileName="test.html";
    try {
        Runtime r = Runtime.getRuntime();
        r.exec("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe file:///C:/temp/" + fileName);
    } catch (Exception e) {
        e.printStackTrace();
    }

Note: put the path in a properties file to get more felxibility

Upvotes: 1

Related Questions