This is the code I'm using to display google in a JEditorPane
String url="http://google.com";
editorPane.setEditable(false);
try {
editorPane.setPage(url);
} catch (IOException e) {}
But for some reason the background will always be a blue colour, doesn't matter if I call
setBackgroundColor(Color.WHITE);
As @AndrewThompson noted in the comments JEditorPane
is really behind, it supports only a subset of HTML 3.2 and CSS1, and isn't really cable of rendering any modern web pages.
I strongly suggest using an alternative, like:
JavaFX WebView
Code Snippet: (no dependencies, you can run it as-is)
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javax.swing.*;
import java.awt.*;
public class JavaFxBrowser implements Runnable {
private WebEngine webEngine;
public static void main(String[] args) {
SwingUtilities.invokeLater(new JavaFxBrowser());
}
public void loadURL(final String url) {
Platform.runLater(() -> {
webEngine.load(url);
});
}
@Override
public void run() {
// setup UI
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setPreferredSize(new Dimension(1024, 600));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFXPanel jfxPanel = new JFXPanel();
frame.getContentPane().add(jfxPanel);
frame.pack();
Platform.runLater(() -> {
WebView view = new WebView();
webEngine = view.getEngine();
jfxPanel.setScene(new Scene(view));
});
loadURL("http://www.google.com");
}
}
Flying Saucer
Code Sample:
XHTMLPanel panel = new XHTMLPanel();
panel.setDocument("http://www.google.com");
@see BrowsePanel.java
or NativeSwing
Code Snippet:
final JWebBrowser webBrowser = new JWebBrowser();
webBrowser.navigate("http://www.google.com");
@see SimpleWebBrowserExample.java
It seems you have extended JFrame in your class. So please use editorPane Object for setting the color as below
String url="http://google.com";
editorPane.setEditable(false);
editorPane.setBackground(Color.WHITE);
try {
editorPane.setPage(url);
} ca
A possible reason is that HTMLDocument parses three-digit color codes differently from normal. Hence, everything is shown as blue because only the blue byte (and the lowest 4 bits of the green byte) is set.
For example: #FFF
would be interpreted as #000FFF
, which is sharp blue.
At least this solved my problem mentioned in the comments. A possible reason for related threads on the background, too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With