RFR: JDK-8282772: JButton text set as HTML content has unwanted padding [v4]
Karl T
duke at openjdk.org
Fri Oct 13 14:30:09 UTC 2023
On Tue, 10 May 2022 19:03:23 GMT, Damon Nguyen <dnguyen at openjdk.org> wrote:
>> The insets for buttons were incorrect for L&Fs except for Aqua when the text is set to HTML. This was fixed in Aqua by adding a conditional to check for the BasicHTML property key in the button component. This same logic can be used to fix Metal & Motif L&Fs in BasicButtonUI, but Nimbus is not fixed by this. Nimbus gets its default values from a skin.laf file, and when the defaults here are set to have left & right insets to 0 for ButtonUI, the issue is fixed. I also tested for non-HTML text after the changes, and the changes do not affect normal text.
>>
>> The HtmlButtonImageTest has been changed to cycle through all L&Fs available on a device.
>
> Damon Nguyen has updated the pull request incrementally with one additional commit since the last revision:
>
> Added string buffer. Separated fail images. Changed exception to throw at the end of test.
This PR breaks correct text location when using HTML text in buttons.
It works only if horizontal/vertical alignment is center and if using symmetric margin (top == bottom and left == right).
If using left/right alignment or using asymmetric margin (e.g. top=30, bottom=4) then the HTML text location is wrong.
And shouldn't text positioned the same for HTML and non-HTML buttons?
Screenshot that demonstrates the problem.
Left is made with Java 20, right with Java 17. Red arrows mark wrong text locations.
In Java 17, HTML text is painted at same location as non-HTML text.
Since Java 19, there is a huge difference because margin is simply ignored when painting.
(interestingly margin is still used to compute preferred size 😕 )

Code used to create above screenshots:
~~~java
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class HtmlButtonTest
{
public static void main( String[] args ) {
SwingUtilities.invokeLater( () -> {
JFrame frame = new JFrame( "HTML Button Test" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
JPanel panel = new JPanel( new GridBagLayout() );
panel.setBorder( new EmptyBorder( 20, 20, 20, 20 ) );
createButtons( panel, "center", SwingConstants.CENTER, SwingConstants.CENTER, null );
createButtons( panel, "left", SwingConstants.LEFT, SwingConstants.CENTER, null );
createButtons( panel, "right", SwingConstants.RIGHT, SwingConstants.CENTER, null );
createButtons( panel, "center with margin 30,4,4,4", SwingConstants.CENTER, SwingConstants.CENTER, new Insets( 30, 4, 4, 4 ) );
createButtons( panel, "left with margin 30,4,4,4", SwingConstants.LEFT, SwingConstants.CENTER, new Insets( 30, 4, 4, 4 ) );
createButtons( panel, "left/top with margin 30,4,4,4", SwingConstants.LEFT, SwingConstants.TOP, new Insets( 30, 4, 4, 4 ) );
frame.add( new JLabel( "Java version " + System.getProperty( "java.version" ) ), BorderLayout.NORTH );
frame.add( panel );
frame.pack();
frame.setVisible( true );
} );
}
private static void createButtons( JPanel panel, String text, int horizontalAlignment, int verticalAlignment, Insets margin ) {
JButton button = new JButton( text );
button.setHorizontalAlignment( horizontalAlignment );
button.setVerticalAlignment( verticalAlignment );
if( margin != null )
button.setMargin( margin );
panel.add( button, new GridBagConstraints( 0, GridBagConstraints.RELATIVE, 1, 1, 1.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets( 4, 4, 4, 4 ), 0, 0 ) );
JButton htmlButton = new JButton( "<html>HTML " + text + "</html>" );
htmlButton.setHorizontalAlignment( horizontalAlignment );
htmlButton.setVerticalAlignment( verticalAlignment );
if( margin != null )
htmlButton.setMargin( margin );
panel.add( htmlButton, new GridBagConstraints( 0, GridBagConstraints.RELATIVE, 1, 1, 1.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets( 4, 4, 24, 4 ), 0, 0 ) );
}
}
~~~
BTW this PR also breaks 3rd party L&Fs like [FlatLaf](https://github.com/JFormDesigner/FlatLaf).
See https://github.com/JFormDesigner/FlatLaf/issues/746
-------------
PR Comment: https://git.openjdk.org/jdk/pull/8407#issuecomment-1761583430
PR Comment: https://git.openjdk.org/jdk/pull/8407#issuecomment-1761595394
More information about the client-libs-dev
mailing list