Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8318590

JButton ignores margin when painting HTML text

XMLWordPrintable

    • b23
    • 19
    • b27
    • generic
    • generic

        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 and JDK-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


          1. HtmlButtonTest.java
            2 kB
          2. From_Submitter.png
            From_Submitter.png
            136 kB
          3. Capture.PNG
            Capture.PNG
            129 kB

              dnguyen Damon Nguyen
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              8 Start watching this issue

                Created:
                Updated:
                Resolved: