JFormDesigner / FlatLaf

FlatLaf - Swing Look and Feel (with Darcula/IntelliJ themes support)
https://www.formdev.com/flatlaf/
Apache License 2.0
3.42k stars 272 forks source link

Usage of SwingX PromptSupport grays out text fields #223

Closed Bios-Marcel closed 2 years ago

Bios-Marcel commented 3 years ago

bug1 bug2

As soon as you type any text, the color is correct.

import com.formdev.flatlaf.FlatLightLaf;
import org.jdesktop.swingx.prompt.PromptSupport;

import javax.swing.*;
import java.awt.*;

public class Main {
    public static void main(String[] args) {
        FlatLightLaf.install();

        JTextField a = new JTextField();
        PromptSupport.setPrompt("Prompt", a);
        JTextField b = new JTextField();

        JPanel layout = new JPanel(new FlowLayout());
        layout.add(a);
        layout.add(b);

        JFrame frame = new JFrame();
        frame.add(layout);
        frame.setSize(400, 200);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Dependencies:

    implementation 'com.formdev:flatlaf:0.45'
    implementation 'com.formdev:flatlaf-swingx:0.45'
    implementation 'org.swinglabs.swingx:swingx-all:1.6.5-1'
DevCharly commented 3 years ago

SwingX PromptSupport does some tricks that FlatLaf does not like. It replaces text field UI delegate FlatTextFieldUI with own class BuddyTextFieldUI. Also border FlatTextBorder is replaced with BuddyLayoutAndBorder. Layout manager is replaced. Seems to be very complicated and tricky...

This avoids that FlatLaf paint text field background. Also if you enter text and use Flat IntelliJ or Darcula themes (with outside focus borders), the background is not correct:

image

Not sure whether this is fixable...

If you use FlatLaf only, I would recommend using placeholder texts instead:

a.putClientProperty( "JTextField.placeholderText", "Prompt" );

image

With next version 0.46, you can use FlatTextField from "extras" subproject:

FlatTextField a = new FlatTextField();
a.setPlaceholderText( "Prompt" );
Bios-Marcel commented 3 years ago

Ah thanks for the info. Yeah, i don't know, but maybe we can fix it in swingx. We are actually using our own fork of swingx, so maybe there's something we can do.

mwithake commented 3 years ago

We have the same problem. We are also using the foreground color option to hint error messages in red (e.g. missing entries). We are using PromptSupport directly in our derived JTextField class, so we could use the ClientProperty if FlatLaf is set instead of using PromptSupport but we are missing the color option.

mwithake commented 3 years ago

JXSearchField has the same problem when setting the prompt.

DevCharly commented 2 years ago

Had another look at the issue. The problem is that FlatTextFieldUI background painting is not invoked.

PromptSupport replaces text field UI delegate FlatTextFieldUI with own class BuddyTextFieldUI, which delegates painting to FlatTextFieldUI, but only if text field is not empty. If empty, then it fills background itself if text field is opaque. But in FlatLaf it is not opaque.

Here is the related code (in PromptTextUI):

    public void update(Graphics g, JComponent c) {
        if (shouldPaintPrompt((JTextComponent) c)) {
            super.update(g, c);
        } else {
            delegate.update(g, c);
        }
    }

One solution would be to make text fields opaque if they use PromptSupport. This works fine for themes "Flat Light" and "Flat Dark", but has issues with "IntelliJ/Darcual" themes (see my previous post).

Another solution would be to modify SwingX PromptTextUI. I think following should work:

    public void update(Graphics g, JComponent c) {
        if (shouldPaintPrompt((JTextComponent) c)) {
            if (!c.isOpaque()) {
                delegate.update(g, c);
            }
            super.update(g, c);
        } else {
            delegate.update(g, c);
        }
    }

Unfortunately, there is nothing I can do to fix this issue in FlatLaf...

Bios-Marcel commented 2 years ago

Thanks for looking into it again, I'll be closing the issue then.