Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7093691: Nimbus LAF: disabled JComboBox using renderer has bad font color #12390

Conversation

DamonGuy
Copy link
Contributor

@DamonGuy DamonGuy commented Feb 2, 2023

Before the fix, a JComboBox in Nimbus L&F would have normal black text even when the JComboBox was disabled if SynthComboBoxRenderer was replaced with a DefaultListCellRenderer. This text should be greyed out like in other L&F's. When looking into the defaults for Nimbus L&F files for attributes and states of a JComboBox, it confirm that the intention for disabled JComboBoxes is nimbusDisabledText (which is grey text).

SynthComboBoxes have an additional check in its default SynthComboBoxRenderer that enables/disables the renderer itself. The SynthComboBoxRenderer inherits its enabled state from the parent ComboBox. Since the renderer with DefaultListCellRenderer is in a separate class without a reference to the comboBox, a listener was added to SynthComboBoxUI.

An additional issue occurred in DefaultListCellRenderer because the renderer overrode the listener's re-assigned enabled state. In testing, setting the enabled state in DefaultListCellRenderer is redundant for all L&F's and is not needed here. However, instead of removing it altogether, a conditional was added specifically to allow ComboBoxes to skip setting enabled state here.

After the fix, the Nimbus JComboBox with DLCR set matches the appearance of a normal Nimbus JComboBox. I can enable/disable the JComboBoxes in the test, and the UI elements behave and appear as expected.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-7093691: Nimbus LAF: disabled JComboBox using renderer has bad font color

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/12390/head:pull/12390
$ git checkout pull/12390

Update a local copy of the PR:
$ git checkout pull/12390
$ git pull https://git.openjdk.org/jdk.git pull/12390/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 12390

View PR using the GUI difftool:
$ git pr show -t 12390

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/12390.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Feb 2, 2023

👋 Welcome back dnguyen! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Feb 2, 2023
@DamonGuy
Copy link
Contributor Author

DamonGuy commented Feb 2, 2023

Here are screenshots of the bug and the UI elements after the fix is applied.

Disabled JComboBox with DLCR before fix:
Screen Shot 2023-02-01 at 3 52 46 PM

After fix:
Screen Shot 2023-02-01 at 3 46 19 PM

@openjdk
Copy link

openjdk bot commented Feb 2, 2023

@DamonGuy The following label will be automatically applied to this pull request:

  • client

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the client client-libs-dev@openjdk.org label Feb 2, 2023
@mlbridge
Copy link

mlbridge bot commented Feb 2, 2023


hasNimbus = false;

for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the intention to run the test is in Nimbus LAF only. I think you can directly set the Nimbus LAF using UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); and can avoid the for-loop, condition check and hasNimbus variable.

To print the error message, LAF can be set within try-catch block.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. I made the change. I initially had this loop to test locally for all LAF's, but ended up using this test for Nimbus only as originally designed.

@@ -0,0 +1,138 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copyright year can be changed to 2023.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

"status with the button on the right of the frame.";

private static JFrame frame;
private static boolean hasNimbus;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasNimbus variable is unused now, can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, updated

try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch (Exception e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion, try-catch block can be like

try {
...
} catch {
...
}

@DamonGuy
Copy link
Contributor Author

DamonGuy commented Feb 6, 2023

@prsadhuk could you review this as well since I got your input about this issue previously?

Initially, I thought it was the default value of the disabled combobox, but none of the skin.laf UI states/values affected the displayed text color.

So, I assumed it is a state issue as you suggested. I checked the enabled state of each L&F, and the combobox's state is correct, but I noticed that the renderer in Synth has an additional condition that sets the renderer to disabled/enabled to match the combobox state. In testing, setting the renderer to disabled produced the correct behavior, so I made a listener as a workaround to access the combobox's renderer for the case with a DefaultListCellRenderer.

With the test I created, this approach creates the correct behavior, so I implemented the fix the cleanest way possible.

Copy link
Contributor

@honkar-jdk honkar-jdk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested the fix on SwingSet2 too and the fix works as expected.
Copyright year needs to be updated for SynthComboBoxUI , DefaultListCellRenderer too.

@prsadhuk
Copy link
Contributor

prsadhuk commented Feb 7, 2023

// The renderer component should inherit the enabled and
// orientation state of its parent combobox. This is
// especially needed for GTK comboboxes, where the
// ListCellRenderer's state determines the visual state
// of the combobox.
if (comboBox != null){
setEnabled(comboBox.isEnabled());

The present code SynthComboBoxRenderer does enable/disable the listcell renderer based on comboBox status, why it is not working? why do we need to use a listener for DefaultListCellRenderer?

Also, I think it will be better to make the test automated..something like draw comboBox content into BufferedImage and pixel compare if it is same for disabled/enabled and fail if they are same and
iterate for all L&Fs as it will also affect GTK as per the comment

@DamonGuy
Copy link
Contributor Author

DamonGuy commented Feb 7, 2023

// The renderer component should inherit the enabled and
// orientation state of its parent combobox. This is
// especially needed for GTK comboboxes, where the
// ListCellRenderer's state determines the visual state
// of the combobox.
if (comboBox != null){
setEnabled(comboBox.isEnabled());

The present code SynthComboBoxRenderer does enable/disable the listcell renderer based on comboBox status, why it is not working? why do we need to use a listener for DefaultListCellRenderer?

Also, I think it will be better to make the test automated..something like draw comboBox content into BufferedImage and pixel compare if it is same for disabled/enabled and fail if they are same and iterate for all L&Fs as it will also affect GTK as per the comment

The setEnabled line you mentioned in SynthComboBoxUI applies to SynthComboBoxRenderer only. This issue only occurs when DefaultListCellRenderer is used for a disabled Nimbus ComboBox instead of the default renderer for Nimbus (SynthComboBoxRenderer). So this code is never reached, but is necessary to update the enabled state. IInitially, I wanted to check for this state in DefaultListCellRenderer, but there's no way to get a reference to the ComboBox from there. So, instead I used a listener and did this approach, which looks better since the ComboBox behavior stays in SynthComboBoxUI.

I will work on making the test automated instead if preferred.

@DamonGuy
Copy link
Contributor Author

DamonGuy commented Feb 8, 2023

Also, I think it will be better to make the test automated..something like draw comboBox content into BufferedImage and pixel compare if it is same for disabled/enabled and fail if they are same and iterate for all L&Fs as it will also affect GTK as per the comment

I created an automated test, but for some L&F's (such as Nimbus), an enabled vs disabled ComboBox differs in more than just the text color. The background, border, and button color may change as well. So, even if I shave off the dimensions of the border and button, it seems difficult to detect just the text for comparison.

Before the fix, the issue was that the ComboBox as a whole worked correctly for disabled ComboBoxes with a DLCR, but the text was black instead of grey. This was the only difference.

A manual test might still be the best approach since more aspects than just the test color can be checked. So, I think it's best to not automate this test.

@prsadhuk
Copy link
Contributor

prsadhuk commented Feb 8, 2023

SynthComboBoxUI.createRenderer points to SynthComboBoxRenderer so I think DefaultListCellRenderer should not come into picture for rendering cells in the list which should go via getListCellRendererComponent.
If it is playing a part, then how it works in other L&F which also has it own renderer (for example, BasicComboBoxUI.createRenderer points to BasicComboBoxRenderer, AquaComboBox points to AquaComboBoxRenderer) and they dont have this listener to make it work, so the question why do we need a listener for Synth still not clear to me..

@prsadhuk
Copy link
Contributor

prsadhuk commented Feb 8, 2023

I created an automated test, but for some L&F's (such as Nimbus), an enabled vs disabled ComboBox differs in more than just the text color. The background, border, and button color may change as well

Then can we not compare the comboBox without DLCR with comboBox with DLCR when both are disabled...both should be same as per your screenshot

@alisenchung
Copy link
Contributor

I ran the test on linux on GTK L&F. Screenshots of enabled/disabled JComboBoxes:
Screenshot 2023-02-08 125006
Screenshot 2023-02-08 125030

They look fine to me.

@DamonGuy
Copy link
Contributor Author

DamonGuy commented Feb 8, 2023

I created an automated test, but for some L&F's (such as Nimbus), an enabled vs disabled ComboBox differs in more than just the text color. The background, border, and button color may change as well

Then can we not compare the comboBox without DLCR with comboBox with DLCR when both are disabled...both should be same as per your screenshot

A comboBox with DLCR is slightly smaller than one without DLCR (138x25 vs 140x25). If I force the same dimensions with setSize, the RGB values are slightly off. If these comboBoxes are supposed to exactly match in dimensions, they currently don't, and this is due to what would be a separate bug if so.

@DamonGuy
Copy link
Contributor Author

DamonGuy commented Feb 9, 2023

SynthComboBoxUI.createRenderer points to SynthComboBoxRenderer so I think DefaultListCellRenderer should not come into picture for rendering cells in the list which should go via getListCellRendererComponent. If it is playing a part, then how it works in other L&F which also has it own renderer (for example, BasicComboBoxUI.createRenderer points to BasicComboBoxRenderer, AquaComboBox points to AquaComboBoxRenderer) and they dont have this listener to make it work, so the question why do we need a listener for Synth still not clear to me..

I thought the same, but Nimbus seems to act different for this instance only. I traced the conditional in SynthComboBoxRenderer, and this was added in Apr 25, 2009 here. This seems like the introduction of Nimbus and how it's enabled/disabled ComboBoxes for Nimbus, with the oversight of setting a different renderer. This looks like the only OS that does this like you mentioned. The reason why this need to setEnabled in the renderer for Nimbus wasn't stated in the comment or bug description page. But it does seem to still need it as this was the only fix I found.

@prsadhuk
Copy link
Contributor

prsadhuk commented Feb 9, 2023

I created an automated test, but for some L&F's (such as Nimbus), an enabled vs disabled ComboBox differs in more than just the text color. The background, border, and button color may change as well

Then can we not compare the comboBox without DLCR with comboBox with DLCR when both are disabled...both should be same as per your screenshot

A comboBox with DLCR is slightly smaller than one without DLCR (138x25 vs 140x25). If I force the same dimensions with setSize, the RGB values are slightly off. If these comboBoxes are supposed to exactly match in dimensions, they currently don't, and this is due to what would be a separate bug if so.

maybe you can try to compare the mid scanline rather than the whole rectangle

@openjdk openjdk bot removed the rfr Pull request is ready for review label Feb 14, 2023
@DamonGuy
Copy link
Contributor Author

I created an automated test, but for some L&F's (such as Nimbus), an enabled vs disabled ComboBox differs in more than just the text color. The background, border, and button color may change as well

Then can we not compare the comboBox without DLCR with comboBox with DLCR when both are disabled...both should be same as per your screenshot

A comboBox with DLCR is slightly smaller than one without DLCR (138x25 vs 140x25). If I force the same dimensions with setSize, the RGB values are slightly off. If these comboBoxes are supposed to exactly match in dimensions, they currently don't, and this is due to what would be a separate bug if so.

maybe you can try to compare the mid scanline rather than the whole rectangle

Replaced the original test with an automated version using a mid scanline. I can't scan across the whole midline because of the width difference, so I adjusted the testing method slightly to accommodate for this.

@openjdk openjdk bot added the rfr Pull request is ready for review label Feb 14, 2023
@prsadhuk
Copy link
Contributor

Did you try extending DLCR for SynthComboBoxRenderer instead of implementing LCR interface?

private class SynthComboBoxRenderer extends JLabel implements ListCellRenderer<Object>, UIResource {

just as it is done here

private class SynthListCellRenderer extends DefaultListCellRenderer.UIResource {

@DamonGuy
Copy link
Contributor Author

extends DefaultListCellRenderer.UIResource

I tried implementing this after your suggestion, but it doesn't achieve the wanted result. This makes sense from my understanding because with a new comboBox set to using a new DefaultListCellRenderer, the SynthComboBoxRenderer wouldn't be used. Changing what is extended for SynthComboBoxRenderer would only affect the normal comboBox using the default Synth renderer. The listener is the way around this because the comboBox with a DLCR still is a SynthComboBox but does not rely on the SynthComboBoxRenderer.

+ "/" + lafName + "EnabledDLCR.png"));
ImageIO.write(disabledImage2, "png", new File(testDir
+ "/" + lafName + "DisabledDLCR.png"));
System.out.println("DIR: " + testDir);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed? I know it is just a test but please remove the debug output when not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the message. I added it to double check the saved images' file path, but didn't remove it before uploading.

SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
SwingUtilities.invokeAndWait(DisabledComboBoxFontTestAuto::createCombo);
SwingUtilities.invokeAndWait(DisabledComboBoxFontTestAuto::paintCombo);
testMethod();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So test will fail whenever it encounters the first wrong color? Ideally we would run test for all the installed LAFs anyways and report which ones are failed. But in this case you would need to pass the LAF name to the test method so it saves screenshots for all the LAFs for future analysis.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a StringBuffer which now outputs the failed LAFs to the thrown runtime exception.

@openjdk
Copy link

openjdk bot commented Mar 3, 2023

@DamonGuy This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

7093691: Nimbus LAF: disabled JComboBox using renderer has bad font color

Reviewed-by: honkar, kizune, psadhukhan

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 754 new commits pushed to the master branch:

  • 2f34687: 8304689: Add hidden option to disable external spec page
  • 6b2f34f: 8304718: GetIntArrayElements should not be passed JNI_FALSE
  • 138cdc9: 8304694: Runtime exception thrown when break stmt is missing
  • 46b0602: 8304547: Remove checking of -Djava.compiler in src/jdk.jdi/share/classes/com/sun/tools/jdi/SunCommandLineLauncher.java
  • 8d423f7: 8304723: Statically allocate global mutexes
  • 2083088: 8304828: Lots of constant static data not declared static const in cpu/x86
  • 0712adc: 7169951: SwingSet2 throws NullPointerException with Nimbus L&F
  • de1c12e: 8301012: [vectorapi]: Intrinsify CompressBitsV/ExpandBitsV and add the AArch64 SVE backend implementation
  • 10fa7d1: 8304804: Remove develop flag G1VerifyCTCleanup
  • 4acf20d: 8304809: Remove develop flag G1ExitOnExpansionFailure
  • ... and 744 more: https://git.openjdk.org/jdk/compare/5744c91bf5742379913a9926a5d70a2d49dbea04...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@honkar-jdk, @azuev-java, @prsadhuk) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Mar 3, 2023
@DamonGuy
Copy link
Contributor Author

DamonGuy commented Mar 6, 2023

@prsadhuk @honkar-jdk Could you take another look because of the updates?

@prsadhuk
Copy link
Contributor

prsadhuk commented Mar 8, 2023

It seems the test fails on windows for WIndows L&F..This is what I got

./jdk/bin/java DisabledComboBoxFontTestAuto
PASSED
PASSED
PASSED
Windows Enabled RGB failure: 240, 240, 240 vs 225, 225, 225
PASSED
Exception in thread "main" java.lang.RuntimeException: FAIL - Enabled and disabled ComboBox does not match in these LAFs: Windows
        at DisabledComboBoxFontTestAuto.main(DisabledComboBoxFontTestAuto.java:178)

It seems it does not fail in mach5 maybe because it is not run in hidpi system.

Also, I was expecting some explanation about this snippet if it is still needed for GTK (because of which, I think, you had to do the change in javax/swing/DefaultListCellRenderer.java)

// The renderer component should inherit the enabled and
// orientation state of its parent combobox. This is
// especially needed for GTK comboboxes, where the
// ListCellRenderer's state determines the visual state
// of the combobox.
if (comboBox != null){
setEnabled(comboBox.isEnabled());
setComponentOrientation(comboBox.getComponentOrientation());
}

You took care of enabled property..If the above snippet still hold significance, do we need to do the same for orientation property too

@DamonGuy
Copy link
Contributor Author

// The renderer component should inherit the enabled and
// orientation state of its parent combobox. This is
// especially needed for GTK comboboxes, where the
// ListCellRenderer's state determines the visual state
// of the combobox.
if (comboBox != null){
setEnabled(comboBox.isEnabled());
setComponentOrientation(comboBox.getComponentOrientation());
}

You took care of enabled property..If the above snippet still hold significance, do we need to do the same for orientation property too

I tested without any additional listener for orientation and set the combo's orientation to right_to_left. It's working correctly. So, I tested the code block you referenced above and removed the code for updating orientation when comboBox != null. This still works correctly. So, this portion of the code that updates the orientation in SynthComboBoxRenderer may be redundant.

However, the enabled portion seems to be needed, and the additional listener added is needed to generate the correct text color.

Nimbus test with a normal comboBox and a custom comboBox (with DefaultListCellRenderer) set with orientation RTL:
Screen Shot 2023-03-22 at 9 44 25 AM

Test with a disabled, normal comboBox and custom comboBox (with DefaultListCellRenderer) set with orientation RTL:
Screen Shot 2023-03-22 at 10 00 23 AM

Copy link
Contributor

@prsadhuk prsadhuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok..

@DamonGuy
Copy link
Contributor Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Mar 27, 2023
@openjdk
Copy link

openjdk bot commented Mar 27, 2023

@DamonGuy
Your change (at version ca73398) is now ready to be sponsored by a Committer.

@azuev-java
Copy link
Member

/sponsor

@openjdk
Copy link

openjdk bot commented Mar 27, 2023

Going to push as commit 87b314a.
Since your change was applied there have been 754 commits pushed to the master branch:

  • 2f34687: 8304689: Add hidden option to disable external spec page
  • 6b2f34f: 8304718: GetIntArrayElements should not be passed JNI_FALSE
  • 138cdc9: 8304694: Runtime exception thrown when break stmt is missing
  • 46b0602: 8304547: Remove checking of -Djava.compiler in src/jdk.jdi/share/classes/com/sun/tools/jdi/SunCommandLineLauncher.java
  • 8d423f7: 8304723: Statically allocate global mutexes
  • 2083088: 8304828: Lots of constant static data not declared static const in cpu/x86
  • 0712adc: 7169951: SwingSet2 throws NullPointerException with Nimbus L&F
  • de1c12e: 8301012: [vectorapi]: Intrinsify CompressBitsV/ExpandBitsV and add the AArch64 SVE backend implementation
  • 10fa7d1: 8304804: Remove develop flag G1VerifyCTCleanup
  • 4acf20d: 8304809: Remove develop flag G1ExitOnExpansionFailure
  • ... and 744 more: https://git.openjdk.org/jdk/compare/5744c91bf5742379913a9926a5d70a2d49dbea04...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Mar 27, 2023
@openjdk openjdk bot closed this Mar 27, 2023
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored labels Mar 27, 2023
@openjdk
Copy link

openjdk bot commented Mar 27, 2023

@azuev-java @DamonGuy Pushed as commit 87b314a.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@mrserb
Copy link
Member

mrserb commented Apr 27, 2023

It seems the test fails on windows for WIndows L&F..This is what I got
It seems it does not fail in mach5 maybe because it is not run in hidpi system.

@prsadhuk Did you able to fix it, or report the new JBS bug for that? I have the same issue, the test fails on the HiDPI system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client client-libs-dev@openjdk.org integrated Pull request has been integrated
7 participants