Metal JButton doesn't show focus if no text or icon

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      If a JButton in Metal Look&Feel has no text or icon, it will not show an indicator when selected.

      From the MetalButtonUI source, you can see that in paintFocus(), the focusRect variable is given meaningful bounds only if there is text or an icon, otherwise it is left at (0,0,0,0) and rectangle (-1,-1,1,1) is painted.

      We do use empty buttons in our UI. It would make sense for the focus rectangle of an empty button to be at the border, I suppose. This is what happens with the Windows Look&Feel.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run code, hit "TAB" so the empty button in the center is selected.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Should see a selection rectangle.
      ACTUAL -
      No selection rectangle is visible.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import javax.swing.*;

      public class JButtonBug {
         public static void main(String[] args) {
            SwingUtilities.invokeLater(
               () -> {
                  try {
                     UIManager.setLookAndFeel("com.sun.java.swing.plaf.metal.MetalLookAndFeel");
                     // Uncomment to see that it does work with Windows L&F.
                     //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                  }
                  catch(Exception e) {}
                  // Make focus rectangle very obvious.
                  UIManager.put("Button.focus", new javax.swing.plaf.ColorUIResource(Color.RED));
                  final JFrame frame = new JFrame();
                  frame.getContentPane().add(new JButton("X"), "North");
                  frame.getContentPane().add(new JButton(""), "Center");
                  frame.setSize(600, 600);
                  frame.setVisible(true);
               });
         }
      }

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

        1. JButtonBug.java
          0.9 kB
          Praveen Narayanaswamy

            Assignee:
            Prasanta Sadhukhan
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: