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

[macosx] JButton's HTML ImageView adding unwanted padding

    Details

      Description

      FULL PRODUCT VERSION :
      java version " 1.7.0_21 "
      Java(TM) SE Runtime Environment (build 1.7.0_21-b12)
      Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Darwin andr3i-mac.local 12.3.0 Darwin Kernel Version 12.3.0: Sun Jan 6 22:37:10 PST 2013; root:xnu-2050.22.13~1/RELEASE_X86_64 x86_64

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Mac OS X 10.8.3

      A DESCRIPTION OF THE PROBLEM :
      Creating a JButton, with text set to an HTML image and a preferred size of the button, set to about the same size as that of the image up to twice the image size (for instance, for an image of size 19x19, set the preferred size of the button to about 37X37), makes the image (resulting ImageView) appear not centered (with or without forcing horizontal centering), i.e. the image appears outside the bounds of the button.

      This is reproducible with the Mac default (Aqua) look and feel; using another LAF (Metal for instance) seems to position the ImageView correctly.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1) Create the JButton.
      2) Set the text to point to an html image
      3) Set the button's size to around 1.5x the size of the image (even larger)
      (for instance an image of size 19x19 and a preferred button size of 30x30)

      Setting text horizontal positioning to center does not help much.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The ImageView centered within the JButton.
      ACTUAL -
      The ImageView has some additional padding in front of it.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.Dimension;
      import java.awt.FlowLayout;

      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.SwingUtilities;

      /**
       * @author acostache
       *
       */
      public final class HTMLTextButtonDemo
      {
      public static void main(String[] args)
      {
      SwingUtilities.invokeLater(new Runnable()
      {
      @Override
      public void run()
      {
      createAndShowGUI();
      }
      });
      }

      private static void createAndShowGUI()
      {
      final JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLayout(new FlowLayout());

      JButton testButton = new JButton();
      testButton.setFocusPainted(false);
      testButton.setPreferredSize(new Dimension(30, 30));
      //testButton.setHorizontalTextPosition(SwingConstants.CENTER); //does not help much
      testButton.setText( " <html><img src='http://files.softicons.com/download/internet-icons/mazes-mini-icons-by-mazenl77/png/19/Flower.png&#39;/&gt;&lt;/html>
       " ); //$NON-NLS-1$

      frame.add(testButton);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      No workaround.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated: