Reputation: 73
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?
Upvotes: 0
Views: 4421
Reputation: 14287
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
.
Upvotes: 0
Reputation: 13195
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.
Upvotes: 1
Reputation: 111
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
Upvotes: 1