Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser using JEditorPane forcing blue background

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);
like image 958
DGK Avatar asked Mar 22 '14 16:03

DGK


3 Answers

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

like image 102
Yoav Aharoni Avatar answered Nov 07 '22 00:11

Yoav Aharoni


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
like image 39
Venkadesh Avatar answered Nov 07 '22 02:11

Venkadesh


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.

like image 25
SOFe Avatar answered Nov 07 '22 01:11

SOFe