Missing TextPane.background property in GTKLookAndFeel causes white background on dark theme

Bug #1894419 reported by Hiroshi Miura
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
openjdk-lts (Ubuntu)
Expired
Undecided
Unassigned

Bug Description

OpenJDK provide GTKLookAndFeel for Ubuntu and other Linux desktop when using Gnome and GTK based environment.
When setting dark theme (such as adwaita-dark) from operating system preference, GTK LAF changes its colors (great!).

Unfortunately when getting "TextPane.background" it returns always 'white' so edit pane become a "white-out", that means both background and font are white.

It is because GTK LaF inherit from Synth Laf, which is extend BasicLookAndFeel, and TextPane.background is defined in BasicLookAndFeel as follows:

```
            "TextPane.background", white,
```

Since GTK LaF respect desktop theme such as dark, so it should be also change based on the desktop configuration.

I've attached a patch to solve it.

Tags: patch
Revision history for this message
Hiroshi Miura (miurahr) wrote :
Revision history for this message
Ubuntu Foundations Team Bug Bot (crichton) wrote :

The attachment "Fix openjdk GTK LaF color bug" seems to be a patch. If it isn't, please remove the "patch" flag from the attachment, remove the "patch" tag, and if you are a member of the ~ubuntu-reviewers, unsubscribe the team.

[This is an automated message performed by a Launchpad user owned by ~brian-murray, for any issues please contact him.]

tags: added: patch
Revision history for this message
Tiago Stürmer Daitx (tdaitx) wrote :

Thank you for taking the time to report this bug and helping to make Ubuntu better.

I tried the code in https://stackoverflow.com/a/31928409 with Ayu-Mirage-Dark and I couldn't reproduce the issue.

Please provide the following items:
- a reproducer with actual code
- the exact OpenJDK version
- which Ubuntu release was this tested on

I'm setting it as incomplete for now.

Revision history for this message
Tiago Stürmer Daitx (tdaitx) wrote :

Forgot to add what happened in my case.

- background was dark
- text was white

I modified the code a bit to simplify it:

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JTextPane;

public class Example {

    public Example() {

        JFrame frame = new JFrame();
        JTextPane pane = new JTextPane();;

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pane.setPreferredSize(new Dimension(200, 200));
        frame.getContentPane().add(pane, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Example();
    }

}

Changed in openjdk-lts (Ubuntu):
status: New → Incomplete
Revision history for this message
Hiroshi Miura (miurahr) wrote :

Hi, here is the reproducer;

OpenJDK version/OS version:
   openjdk 11.0.8 2020-07-14 / Mint Linux 20
   openjdk 1.8.0_265 / Ubuntu 20.04

import java.awt.*;
import javax.swing.*;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.StyleSheet;

public class Example {

    private String toHex(Color color) {
        return String.format("#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue());
    }

    public Example() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
        JFrame frame = new JFrame();
        JTextPane pane = new JTextPane();
        pane.setContentType("text/html");
        ((HTMLDocument) pane.getDocument()).setPreservesUnknownTags(false);
        String text = "Displays the text using htmlDocument class <br/>"
                + "font color : TextPane.foreground=" + toHex(UIManager.getColor("TextPane.foreground"))
                + "<br/> background color : TextPane.background=" + toHex(UIManager.getColor("TextPane.background"))
                + "<br/><br/> background color should lighter when light mode and darker when dark mode.";
        StyleSheet styleSheet = ((HTMLDocument) pane.getDocument()).getStyleSheet();
        styleSheet.addRule("body { font-family: Arial; font-size: 24pt; font-style: bold;"
                + " color: " + toHex(UIManager.getColor("TextPane.foreground")) + "; "
                + " background: " + toHex(UIManager.getColor("TextPane.background")) + "; "
                + " }");
        pane.setText(text);
        pane.setPreferredSize(new Dimension(800, 200));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(pane, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Example();
    }

}

Revision history for this message
Hiroshi Miura (miurahr) wrote :
summary: - Wrong TextPane.background color with GTKLookAndFeel on dark theme
+ Missing TextPane.background property in GTKLookAndFeel causes white
+ background on dark theme
description: updated
Revision history for this message
Tiago Stürmer Daitx (tdaitx) wrote :

Thanks for the reproducer.

JTextPane by itself does work correctly and has the right background and text colors from the theme, but when getting the TextPane.background color property the theme color is not being respect.

I haven't found any open issues in OpenJDK upstream about this problem and so far it seems to affect all codebases I have looked at (OpenJDK 8-16). To address that I have send a message to the swing mailing about this. I will add more comments when I get a reply.

Revision history for this message
Hiroshi Miura (miurahr) wrote :

There may be another approach for fix this.

Since OpenJDK BasicLookAndFeel.java set text background color to fixed 'white' color,
it causes the issue.
https://github.com/openjdk/jdk/blob/fb8ceae0a77fecb0b14de5bd00253e074b0aba90/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicLookAndFeel.java#L1657-L1678

            "TextPane.font", serifPlain12,
            "TextPane.background", white,
            "TextPane.foreground", textText,
            "TextPane.selectionBackground", textHighlight,
            "TextPane.selectionForeground", textHighlightText,
            "TextPane.caretForeground", textText,
            "TextPane.caretBlinkRate", caretBlinkRate,
            "TextPane.inactiveForeground", textInactiveText,
            "TextPane.border", marginBorder,
            "TextPane.margin", editorMargin,

            "EditorPane.font", serifPlain12,
            "EditorPane.background", white,
            "EditorPane.foreground", textText,
            "EditorPane.selectionBackground", textHighlight,
            "EditorPane.selectionForeground", textHighlightText,
            "EditorPane.caretForeground", textText,
            "EditorPane.caretBlinkRate", caretBlinkRate,
            "EditorPane.inactiveForeground", textInactiveText,
            "EditorPane.border", marginBorder,
            "EditorPane.margin", editorMargin,

One idea is that we can change 'TextPane.background' to 'text' instead of 'white'.
It would make side effects around all default LaFs other than Aqua.

My proposed patch is a quick hack only to effect against GTKLaF.

Revision history for this message
Launchpad Janitor (janitor) wrote :

[Expired for openjdk-lts (Ubuntu) because there has been no activity for 60 days.]

Changed in openjdk-lts (Ubuntu):
status: Incomplete → Expired
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.