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);
DGK
  • 2,947
  • 5
  • 32
  • 47
  • 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`. – Braj Mar 22 '14 at 16:03
  • 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 blue – DGK Mar 22 '14 at 16:27
  • Try this one [JTextPane text background color does not work](http://stackoverflow.com/questions/13285526/jtextpane-text-background-color-does-not-work). – Braj Mar 22 '14 at 16:36
  • *"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. – Andrew Thompson Mar 23 '14 at 00:33
  • 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 `` or a ``, while I have white text (regardless of background and foreground color from Java code) for text in bare `

    `. Note that everything is in the same `

    `, white text or blue text, span or font or bare.

    – SOFe Mar 30 '16 at 14:46
  • However, background color works in my code. You can find the source code for my project from [GitHub](https://github.com/PEMapModder/PocketMine-GUI/blob/master/src/main/java/com/github/pemapmodder/pocketminegui/gui/server/ConsolePanel.java) (I hadn't got time to simplify the problem). – SOFe Mar 30 '16 at 14:48

4 Answers4

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

Yoav Aharoni
  • 2,672
  • 13
  • 18
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.

SOFe
  • 7,867
  • 4
  • 33
  • 61
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
Venkadesh
  • 89
  • 7
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.

Som Bhattacharyya
  • 3,972
  • 35
  • 54