1

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.?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
code3h
  • 11
  • 1
  • 2
  • 1
    *Am I in a correct path?* No since you're executing the Chrome browser directly, and what's worse you're assuming that Chrome is installed in C:\Users\K-9\AppData\Local\Google\Chrome\Application\ path for every pc. Related: http://stackoverflow.com/q/10601676/1065197 – Luiggi Mendoza Aug 29 '13 at 20:38
  • @user919860. i just want to show a web page in my project instead of opening a browser. – code3h Aug 29 '13 at 20:41
  • The link in my comment with the *Related* word before means *hey! this Q/A is related to what you're looking for*. Have you at least go into it and read the contents? – Luiggi Mendoza Aug 29 '13 at 20:42
  • This link might help: http://stackoverflow.com/questions/9548459/load-javascript-within-jframe-or-jpanel – user919860 Aug 29 '13 at 20:43
  • @Luiggi Mendoza.this is just for me in my computer,so the path is same.thanks for the link.it helped me very much.thanks again. – code3h Aug 29 '13 at 20:43
  • I personally don't like how code3h is trying to run a web browser externally like that. Unless it's necessary, I don't think that you should make it a habit of trying to get your work done in an external application. – user919860 Aug 29 '13 at 20:46
  • No problem, code3h. :). But, I think that Luiggi Mendoza's post might provide a simpler solution. The one that I provided allows you to execute JavaScript, but if your goal is only to show a web page, then I think that the solution in his comment should be sufficient. – user919860 Aug 29 '13 at 20:47

2 Answers2

1

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

KIC
  • 5,887
  • 7
  • 58
  • 98
1

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

2sku
  • 31
  • 2