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

GTK L&F list borders cannot be applied to anything but a JLabel without a NPE

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.7.0-ea"
      Java(TM) SE Runtime Environment (build 1.7.0-ea-b33)
      Java HotSpot(TM) 64-Bit Server VM (build 14.0.b01, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Seen on Gentoo 64-bit and Fedora 8 32-bit.

      A DESCRIPTION OF THE PROBLEM :
      Under the GTK L&F, attempting to set a List.focusSelectedCellHightlightBorder on a component other than a JLabel results in a NullPointerException.

      I am using a JCheckBox as a ListCellRenderer and using the following code throws a NullPointerException:

      Border border = UIManager.getBorder("List.focusSelectedCellHighlightBorder");
      if(null != border)
         setBorder(border);

      The stack trace indicates the exception occurring at javax.swing.JComponent.setBorder(JComponent.java:1783) and when I dig into this further I can see that it is caused by the call to getBorderInsets(this) which returns null from GTKPainter$ListTableFocusBorder unless the component is an instance of JLabel.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a JCheckBox that implements ListCellRenderer and set its border to UIManager.getBorder("List.focusSelectedCellHighlightBorder") under the GTK L&F.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      setBorder() should return normally.
      ACTUAL -
      NullPointerException

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.lang.NullPointerException
          at javax.swing.JComponent.setBorder(JComponent.java:1783)
          at MyCellRenderer.getListCellRendererComponent()

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.BorderLayout;
      import java.awt.Container;

      import javax.swing.JCheckBox;
      import javax.swing.JFrame;
      import javax.swing.SwingUtilities;
      import javax.swing.UIManager;
      import javax.swing.UnsupportedLookAndFeelException;
      import javax.swing.border.Border;


      public class Test {

      private static class CellRenderer extends JCheckBox {
      public CellRenderer() {
      super();
      setText("Test");
      Border border = UIManager.getBorder("List.focusSelectedCellHighlightBorder");
      if(null != border)
      setBorder(border);
      }
      }

      public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
      UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
      SwingUtilities.invokeLater(new Runnable() {

      @Override
      public void run() {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container cp = frame.getContentPane();
      cp.setLayout(new BorderLayout());
      cp.add(new CellRenderer());

      frame.pack();
      frame.setVisible(true);
      }

      });
      }

      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Wrap call to setBorder in try... catch and set an empty border in the catch block.

            malenkov Sergey Malenkov (Inactive)
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: