-
Bug
-
Resolution: Fixed
-
P3
-
19, 21, 22
-
b23
-
b27
-
generic
-
generic
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8323119 | 21.0.3-oracle | Renjith Kannath Pariyangad | P3 | Resolved | Fixed | b01 |
JDK-8324140 | 21.0.3 | Goetz Lindenmaier | P3 | Resolved | Fixed | b01 |
A DESCRIPTION OF THE PROBLEM :
When using HTML text in a JButton and setting horizontal alignment to e.g. left, then the left margin is ignored and the text is painted at the left edge of the button, but there is no gap (the left margin) between the left border and the text. This is only one example. The JButton.margin property and the border insets are simply ignored for painting (and for computing baseline) when JButton.text property contains HTML text.
This is a regression since Java 19 caused by fixes for 8015854 andJDK-8282772.
Those fixes simply use zero margin for painting HTML text.
See https://github.com/openjdk/jdk/pull/8407#issuecomment-1761583430 for screenshots of the below test case and for more details.
REGRESSION : Last worked in version 18
---------- BEGIN SOURCE ----------
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 ) );
}
}
---------- END SOURCE ----------
FREQUENCY : always
When using HTML text in a JButton and setting horizontal alignment to e.g. left, then the left margin is ignored and the text is painted at the left edge of the button, but there is no gap (the left margin) between the left border and the text. This is only one example. The JButton.margin property and the border insets are simply ignored for painting (and for computing baseline) when JButton.text property contains HTML text.
This is a regression since Java 19 caused by fixes for 8015854 and
Those fixes simply use zero margin for painting HTML text.
See https://github.com/openjdk/jdk/pull/8407#issuecomment-1761583430 for screenshots of the below test case and for more details.
REGRESSION : Last worked in version 18
---------- BEGIN SOURCE ----------
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 ) );
}
}
---------- END SOURCE ----------
FREQUENCY : always
- backported by
-
JDK-8323119 JButton ignores margin when painting HTML text
- Resolved
-
JDK-8324140 JButton ignores margin when painting HTML text
- Resolved
- relates to
-
JDK-8015854 [macosx] JButton's HTML ImageView adding unwanted padding
- Resolved
-
JDK-8282772 JButton text set as HTML content has unwanted padding
- Resolved
- links to
-
Commit openjdk/jdk21u-dev/0d30c8d4
-
Commit openjdk/jdk/acaf2c8d
-
Review openjdk/jdk21u-dev/182
-
Review openjdk/jdk/16869
(3 links to)