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

LabelUI is not updated for TitledBorder

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3
    • 9
    • 9
    • client-libs
    • None

    Description

      Run the code below. The previous LabelUI is used for TitledBorder after L&F changing:
      --------------------------
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.image.BufferedImage;
      import javax.swing.JComponent;
      import javax.swing.JLabel;
      import javax.swing.SwingUtilities;
      import javax.swing.UIDefaults;
      import javax.swing.UIManager;
      import javax.swing.border.TitledBorder;
      import javax.swing.plaf.ComponentUI;
      import javax.swing.plaf.UIResource;
      import javax.swing.plaf.metal.MetalLabelUI;
      import javax.swing.plaf.metal.MetalLookAndFeel;
      import javax.swing.plaf.nimbus.NimbusLookAndFeel;

      public class TextUIDrawingTitleBorder {

          private static final int SIZE = 50;

          public static void main(String[] args) {
              SwingUtilities.invokeLater(TextUIDrawingTitleBorder::createAndShowGUI);
          }

          private static void createAndShowGUI() {

              try {
                  UIManager.setLookAndFeel(new TestLookAndFeel());

                  JComponent label = new JLabel("Test Label");
                  label.setSize(SIZE, SIZE);
                  TitledBorder border = new TitledBorder("ABCDEF");
                  label.setBorder(new TitledBorder(border));

                  UIManager.setLookAndFeel(new NimbusLookAndFeel());
                  SwingUtilities.updateComponentTreeUI(label);

                  paintToImage(label);

              } catch (Exception e) {
                  throw new RuntimeException(e);
              }
          }

          private static void paintToImage(JComponent comp) {
              BufferedImage image = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB);
              Graphics2D g = image.createGraphics();
              comp.paint(g);
              g.dispose();
          }

          public static class TestLookAndFeel extends MetalLookAndFeel {

              @Override
              protected void initClassDefaults(UIDefaults table) {
                  super.initClassDefaults(table);
                  table.put("LabelUI", TestLabelUI.class.getName());
              }
          }

          public static class TestLabelUI extends MetalLabelUI implements UIResource {

              public static ComponentUI createUI(JComponent c) {
                  return new TestLabelUI();
              }

              @Override
              public void paint(Graphics g, JComponent c) {
                  super.paint(g, c);
                  throw new RuntimeException("New LabelUI is not installed!");
              }
          }
      }
      --------------------------

      Attachments

        Activity

          People

            alexsch Alexandr Scherbatiy
            alexsch Alexandr Scherbatiy
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: