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

Button border overlaps with button icon on macOS system LaF

XMLWordPrintable

    • b13
    • 19
    • b12
    • x86_64
    • os_x

      A DESCRIPTION OF THE PROBLEM :
      Looks GOOD on JDK 17
      <link>

      Looks BAD on JDK 19:
      <llink>

      The issue seems to be specific to (1) macOS system LaF and (2) using HTML as button text and (3) using setHorizontalAlignment(LEFT) which in combination leads to icon / text / button border not aligning correctly.

      REGRESSION : Last worked in version 17.0.6

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Please run the self-contained test case below on macOS with JDK 17 and JDK 19 and you will find that that JDK 19 does not render the icon in the correct location.


      ---------- BEGIN SOURCE ----------
      package test;

      import java.awt.BorderLayout;
      import java.awt.Color;
      import java.awt.Component;
      import java.awt.Graphics;

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

      public class Main {

      public static void main(String[] args) throws Exception {
      SwingUtilities.invokeLater(() -> {
      try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      } catch (Exception e) {
      e.printStackTrace();
      }

      JButton button = new JButton("<html><nobr><u>Test</u></nobr></html>", new TestIcon());
      button.setHorizontalAlignment(JButton.LEFT);

      JFrame window = new JFrame();
      window.getContentPane().add(button, BorderLayout.CENTER);

      window.pack();
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      window.setVisible(true);
      });
      }

      public static class TestIcon implements Icon {

      @Override
      public void paintIcon(Component c, Graphics g, int x, int y) {
      g.setColor(Color.MAGENTA);
      g.fillRect(x, y, 16, 16);
      }

      @Override
      public int getIconWidth() {
      return 16;
      }

      @Override
      public int getIconHeight() {
      return 16;
      }
      }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use JDK 17 instead of JDK 19.

      FREQUENCY : always


        1. jdk17_capture.png
          jdk17_capture.png
          66 kB
        2. jdk19_capture.png
          jdk19_capture.png
          65 kB
        3. Main.java
          1 kB

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

              Created:
              Updated:
              Resolved: