Wednesday, April 6, 2016

Browser using JEditorPane forcing blue background

Leave a Comment

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); 

4 Answers

Answers 1

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

Answers 2

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.

Answers 3

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 

Answers 4

I once tried using JEditorPane circa JDK1.3 and the support was awfully limited. From what i understand there has not been much advancements in that API to provide support for browsing.

I recommend you checkout DJ here. Simple to setup and use reliably.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment