Browser using JEditorPane for blue background

This is the code I use to display google in 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 blue, it doesn’t matter if I call

 setBackgroundColor(Color.WHITE); 
+7
java url colors swing jeditorpane
source share
4 answers

As @AndrewThompson noted in JEditorPane's comments, it only supports a subset of HTML 3.2 and CSS1 and is not really a cable for rendering any modern web pages.

I highly recommend using an alternative, for example:

  • 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 example:

     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

+5
source share

A possible reason is that HTMLDocument parses three-digit color codes differently from normal. Therefore, everything is displayed as blue, because only the blue byte is set (and the least significant 4 bits of the green byte).

For example: #FFF will be interpreted as #000FFF , which is blue.

At least this solved my problem mentioned in the comments. A possible reason for linked threads in the background too.

0
source share

It seems you have expanded JFrame in your class. So please use the editorPane Object element to set the color below

 String url="http://google.com"; editorPane.setEditable(false); editorPane.setBackground(Color.WHITE); try { editorPane.setPage(url); } ca 
0
source share

I once tried to use JEditorPane near JDK1.3, and support was terribly limited. As far as I understand, there were not many enhancements to this API to provide support for viewing.

I recommend you check out the DJ here . Easy to install and use reliably.

0
source share

All Articles