Browser using JEditorPane forcing blue background
Asked Answered
F

4

10

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);
Foretell answered 22/3, 2014 at 16:0 Comment(6)
For which component have you called setBackgroundColor(Color.WHITE);? You are setting background color of its parent. Please share some code. editorPane.setBackground(Color.WHITE); is work perfectly for me and I have added it into JScrollPane.Katharina
I have tried it on both the editorpane and the container which contains it. I don't know what code I should add, there is no more relevant code besides creating the container, giving it a border layout, creating the editorpane and adding it to the container. The background is simply blueForetell
Try this one JTextPane text background color does not work.Katharina
"This is the code I'm using to display google in a JEditorPane" The JEditorPane was never intended to render 'real world' HTML. Note that it only supports a subset of HTML 3.2 & (very) basic CSS.Frilling
I am unable to reproduce the same problem, but instead, I got a blue foreground. I have a white background, which is correct. But I have blue text for any code formatted in a <span> or a <font>, while I have white text (regardless of background and foreground color from Java code) for text in bare <p>. Note that everything is in the same <p>, white text or blue text, span or font or bare.Stupefacient
However, background color works in my code. You can find the source code for my project from GitHub (I hadn't got time to simplify the problem).Stupefacient
L
8

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

Luminary answered 4/4, 2016 at 19:28 Comment(0)
S
1

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.

Stupefacient answered 31/3, 2016 at 12:29 Comment(0)
L
1

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
Lighten answered 5/4, 2016 at 11:24 Comment(0)
K
0

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.

Keverne answered 5/4, 2016 at 17:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.