CompEng
CompEng

Reputation: 7376

How can I open a url in my app, not browser?

I want to open a url in my app, not in browser.How can I do this? I think I need a webview.I use netbeans desktop app with jdk 6

If javafx needed , How can I use it? Please give some tutorial?

Upvotes: 1

Views: 1405

Answers (2)

jewelsea
jewelsea

Reputation: 159291

I'm not quite sure from your question if you are trying to embed your application as a module in the NetBeans platform or if you are just using the NetBeans IDE as your development platform. So this answer provides resources on how to do both.

Embedding a JavaFX WebView as a NetBeans module

Here is a sample project which embeds a the a simple JavaFX WebView based html browser in a NetBeans module. A blog entry discussing the project is here.

Standalone JavaFX program using WebView

A sample JavaFX program which renders the page at imageshack link is:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class GoogleSouthAfrica extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(Stage stage) {
    WebView webview = new WebView();
    webview.getEngine().load("http://www.google.co.za/");
    stage.setScene(new Scene(webview, 750, 450));
    stage.show();
  }
}

And the output of the sample program is: Sample program output

Here is a link to a tutorial on Adding HTML Content to JavaFX Applications using the NetBeans IDE.

Upvotes: 1

David Kroukamp
David Kroukamp

Reputation: 36423

Why not use JEditorPane, setContentType() and setText().

You can set the content type and then get the HTML from the URL repsonse and set the JEditorPane text:

editor.setContentType( "text/html" );    
editor.setText( "<html><body>Hello, world</body></html>" );

UPDATE:

Here is a small exmaple though there are a few glitches :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JEditorPaneTest extends JPanel {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                final JFrame frame = new JFrame();
                JEditorPane editor = new JEditorPane();

                frame.getContentPane().add(editor);

                editor.setContentType("text/html");
                URL url = null;
                try {
                    url = new URL("http://www.google.co.za");
                } catch (MalformedURLException ex) {
                    Logger.getLogger(JEditorPaneTest.class.getName()).log(Level.SEVERE, null, ex);
                }

                BufferedReader in = null;
                try {
                    in = new BufferedReader(new InputStreamReader(url.openStream()));
                } catch (IOException ex) {
                    Logger.getLogger(JEditorPaneTest.class.getName()).log(Level.SEVERE, null, ex);
                }
                String inputLine;
                StringBuffer response = new StringBuffer();
                try {
                    while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine).append("\n");
                    }
                    in.close();
                } catch (IOException ex) {
                    Logger.getLogger(JEditorPaneTest.class.getName()).log(Level.SEVERE, null, ex);
                }


                // editor.setText("<html><body>Hello, world</body></html>");
                editor.setText(response.toString());
                editor.setEditable(false);

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

You should consider JavaFX though this has a WebView like you need: http://docs.oracle.com/javafx/2/webview/WebViewSample.java.htm

Download here: http://www.oracle.com/technetwork/java/javafx/downloads/index.html

To setup Java FX and netbeans see here: http://netbeans.org/kb/docs/java/javafx-setup.html

Upvotes: 5

Related Questions