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

MetalToolBarUI.createContainerListener() could not be used

    XMLWordPrintable

Details

    • beta2
    • sparc
    • solaris_2.6

    Description



      Name: nkR10003 Date: 07/04/2001



      JavaDoc comments for contListener field and createContainerListener method state:
      "protected ContainerListener contListener

            This protected field is implemenation specific. Do not access directly or
            override. Use the create method instead.

            See Also:
                  createContainerListener()

      protected ContainerListener createContainerListener()

            Creates a container listener that will be added to the JToolBar. If this
            method returns null then it will not be added to the toolbar.
            
            Returns:
                  an instance of a ContainerListener or null
      "

      So one can use createContainerListener() to return an instance of
      MetalToolBarUI.MetalContainerListener to support super class
      functionality and supply additional behavior. In this case component
      removal fails with NPE.

      Example below demonstrates this problem:
      ------------------example--------------------
      import javax.swing.plaf.metal.*;
      import javax.swing.*;
      import java.awt.event.*;
      class StubToolBarUI extends MetalToolBarUI {
          StubToolBarUI() {
              super();
          }
          
          protected ContainerListener createContainerListener() {
              return new StubContainerListener();
          }
          
          class StubContainerListener extends MetalToolBarUI.MetalContainerListener {
              public void componentAdded(ContainerEvent e) {
                  super.componentAdded(e);
                  System.out.println("Component Added");
              }
              
              public void componentRemoved(ContainerEvent e) {
                  super.componentRemoved(e);
                  System.out.println("Component Removed");
              }
          }
      }
      public class jdk_bug31_test {
          public static void main(String[] args) {
               JToolBar toolBar = new JToolBar();
               StubToolBarUI ui = new StubToolBarUI();
               toolBar.setUI(ui);
               JButton button = new JButton("test");
               try {
                  toolBar.add(button);
                  toolBar.remove(button);
               } catch(Exception e) {
                   System.out.println("Test Failed");
                   e.printStackTrace();
                   System.exit(-1);
               }
               
               System.out.println("Test Passed");
               System.exit(0);
          }
      }
      ----------------output-----------------------
      Component Added
      Test Failed
      java.lang.NullPointerException
      at javax.swing.plaf.basic.BasicToolBarUI.setBorderToNormal(BasicToolBarUI.java:647)
      at javax.swing.plaf.basic.BasicToolBarUI$ToolBarContListener.componentRemoved(BasicToolBarUI.java:1016)
      at StubToolBarUI$StubContainerListener.componentRemoved(jdk_bug31_test.java:20)
      at java.awt.AWTEventMulticaster.componentRemoved(AWTEventMulticaster.java:151)
      at java.awt.Container.processContainerEvent(Container.java:1358)
      at java.awt.Container.processEvent(Container.java:1334)
      at java.awt.Component.dispatchEventImpl(Component.java:3476)
      at java.awt.Container.dispatchEventImpl(Container.java:1399)
      at java.awt.Component.dispatchEvent(Component.java:3343)
      at java.awt.Container.remove(Container.java:564)
      at java.awt.Container.remove(Container.java:589)
      at jdk_bug31_test.main(jdk_bug31_test.java:33)
      ---------------------------------------------
      ======================================================================

      Attachments

        Activity

          People

            mdavidsosunw Mark Davidson (Inactive)
            knasunw Kna Kna (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: