0

I have a PageRead class and methods which will download the html source code from a given url.

public class PageRead {
public static StringBuilder readPage(String pageAddr) {
try {
URL url = new URL(pageAddr);
BufferedReader reader = new BufferedReader(new 
InputStreamReader(url.openStream()));

String line;
StringBuilder sb=new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line+"\n");
}
reader.close();
return sb;            
} 

catch (MalformedURLException e) {
e.printStackTrace();
return new StringBuilder("");
}  

catch (IOException e) {
e.printStackTrace();
return new StringBuilder("");
}
}

public static void main(String arg[]){
System.out.println(readPage("http://www.google.com"));
}
}

This will return me the source code in a String soemthing like :

<!doctype html>.......</body></html>

Is there a way to display this html in something like a JFrame using this source code?

1
  • Swing components have very basic support for html, you may switch to JavaFx's WebView Commented Aug 5, 2018 at 7:33

3 Answers 3

2

You can write source code to an file, something like: File file = new File("index.html"); and than open that file with your default browser. Page open can be done with https://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html.

try {
    Files.write(file.toPath(), content.getBytes());
    Desktop.getDesktop().browse(file.toURI());
} catch (IOException e) {
  // TODO Auto-generated catch block
}

If that is that what you think for :D

Sign up to request clarification or add additional context in comments.

Comments

1

You can have a simple browser in Java, if that was the question. There was a JWebPane until Java6, and now there is a WebKit-based engine as part of JavaFX:

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

public class BrowserTest extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        StackPane root = new StackPane();

        WebView view = new WebView();
        WebEngine engine = view.getEngine();
        engine.load("http://www.google.com");
        root.getChildren().add(view);

        Scene scene = new Scene(root, 800, 600);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) throws Exception {
        Application.launch(args);
    }
}

Example was taken from a gist of GitHub user skrb.
One thing I checked is that WebEngine also has loadContent() method where you can directly feed it with HTML content as a String, so you can write

engine.loadContent(readPage("http://www.google.com").toString());

in place of the current load() call (which takes an URL), just be prepared that the downloaded HTML code is not everything what a webpage needs.

2 Comments

hi I have tried your codes and it will work! But however I have added a JFrame class inside my java program, and I want to call this show browser method inside the JFrame, is it possible to do that? Right now i cant use the above codes inside the JFrame as JFrame extends JFrame class and does not allow me to extends Application class anymore!
@JasonWEI I have not tried that, but I think you will need this tutorial: JavaFX for Swing Developers
0

One can open a html page or content within a Swing's JEditorPane. JEditorPane has various constructors including:

JEditorPane(URL initialPage)
JEditorPane(String type, String text) // where type can be a MIME type: text/html

This can be used with simple html content; the limitation is the html version supported is 3.2 only. Optionally, the Swing application can open a JavaFX's WebView within a JFrame using JFXPanel.

Comments

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.