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

Exception occur when updateUI for JTree is triggered by KeyEvent

    XMLWordPrintable

Details

    • kestrel
    • generic
    • generic

    Description



      Name: wl91122 Date: 08/19/99


      I have a problem with the updateUI() for a JTree when this is triggered from a KeyEvent.

      To demonstrate the problem I reproduced it in a small example.
      I expand the DymanicTree demo from the tutorial with a KeyListener, where
      the updateUI() for the Tree is called. (DynamicTree.java)

      Expand the parent node and then press F5-Key.
      First time it works correct but from then on the following Exception occurs:

      Exception occurred during event dispatching:
      java.lang.NullPointerException
              at javax.swing.plaf.basic.BasicTreeUI$KeyHandler.keyPressed(Compiled Code)
              at javax.swing.plaf.basic.BasicTreeUI$KeyHandler.keyPressed(Compiled Code)
              at java.awt.AWTEventMulticaster.keyPressed(Compiled Code)
              at java.awt.Component.processKeyEvent(Compiled Code)
              at javax.swing.JComponent.processKeyEvent(Compiled Code)
              at java.awt.Component.processEvent(Compiled Code)
              at java.awt.Container.processEvent(Compiled Code)
              at java.awt.Component.dispatchEventImpl(Compiled Code)
              at java.awt.Container.dispatchEventImpl(Compiled Code)
              at java.awt.Component.dispatchEvent(Compiled Code)
              at java.awt.LightweightDispatcher.processKeyEvent(Compiled Code)
              at java.awt.LightweightDispatcher.dispatchEvent(Compiled Code)
              at java.awt.Container.dispatchEventImpl(Compiled Code)
              at java.awt.Window.dispatchEventImpl(Compiled Code)
              at java.awt.Component.dispatchEvent(Compiled Code)
              at java.awt.EventQueue.dispatchEvent(EventQueue.java:258)
              at java.awt.EventDispatchThread.run(EventDispatchThread.java:68)

      The Example Code:
      /** This code is based on an example provided by Richard Stanford,
       * a tutorial reader.
       */
      import java.awt.*;
      import javax.swing.*;
      import javax.swing.tree.*;
      import javax.swing.event.*;
      import java.awt.event.*;

      public class DynamicTree extends JPanel {
          protected DefaultMutableTreeNode rootNode;
          protected DefaultTreeModel treeModel;
          protected JTree tree;
          private Toolkit toolkit = Toolkit.getDefaultToolkit();

          public DynamicTree() {
              rootNode = new DefaultMutableTreeNode("Root Node");
              treeModel = new DefaultTreeModel(rootNode);

              tree = new JTree(treeModel);
              tree.setEditable(true);
              tree.getSelectionModel().setSelectionMode
                      (TreeSelectionModel.SINGLE_TREE_SELECTION);
              tree.setShowsRootHandles(true);

              tree.addKeyListener(new KeyAdapter() {
                  public void keyPressed(KeyEvent e) {
                   if(e.getKeyCode() == KeyEvent.VK_F5) tree.updateUI();
                  }
              });
              JScrollPane scrollPane = new JScrollPane(tree);
              setLayout(new GridLayout(1,0));
              add(scrollPane);
          }


          /** Remove all nodes except the root node. */
          public void clear() {
              rootNode.removeAllChildren();
              treeModel.reload();
          }

          /** Remove the currently selected node. */
          public void removeCurrentNode() {
              TreePath currentSelection = tree.getSelectionPath();
              if (currentSelection != null) {
                  DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)
                               (currentSelection.getLastPathComponent());
                  MutableTreeNode parent = (MutableTreeNode)(currentNode.getParent());
                  if (parent != null) {
                      treeModel.removeNodeFromParent(currentNode);
                      return;
                  }
              }

              // Either there was no selection, or the root was selected.
              toolkit.beep();
          }

          /** Add child to the currently selected node. */
          public DefaultMutableTreeNode addObject(Object child) {
              DefaultMutableTreeNode parentNode = null;
              TreePath parentPath = tree.getSelectionPath();

              if (parentPath == null) {
                  parentNode = rootNode;
              } else {
                  parentNode = (DefaultMutableTreeNode)
                               (parentPath.getLastPathComponent());
              }

              return addObject(parentNode, child, true);
          }

          public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
                                                  Object child) {
              return addObject(parent, child, false);
          }

          public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
                                                  Object child,
                                                  boolean shouldBeVisible) {
              DefaultMutableTreeNode childNode =
                      new DefaultMutableTreeNode(child);

              if (parent == null) {
                  parent = rootNode;
              }

              treeModel.insertNodeInto(childNode, parent,
                                       parent.getChildCount());

              // Make sure the user can see the lovely new node.
              if (shouldBeVisible) {
                  tree.expandPath(new TreePath(parent.getPath()));
                  tree.scrollPathToVisible(new TreePath(childNode.getPath()));
              }
              return childNode;
          }
      }

      /*
       * This code is based on an example provided by Richard Stanford,
       * a tutorial reader.
       */
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.swing.tree.*;

      public class DynamicTreeDemo extends JPanel {
          private int newNodeSuffix = 1;

          public DynamicTreeDemo(JFrame frame) {
              //create the components
              final DynamicTree treePanel = new DynamicTree();
              populateTree(treePanel);

              JButton addButton = new JButton("Add");
              addButton.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      treePanel.addObject("New Node " + newNodeSuffix++);
                  }
              });

              JButton removeButton = new JButton("Remove");
              removeButton.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      treePanel.removeCurrentNode();
                  }
              });

              JButton clearButton = new JButton("Clear");
              clearButton.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      treePanel.clear();
                  }
              });

              //Lay everything out.
              setLayout(new BorderLayout());
              treePanel.setPreferredSize(new Dimension(300, 150));
              add(treePanel, BorderLayout.CENTER);

              JPanel panel = new JPanel();
              panel.setLayout(new GridLayout(0,1));
              panel.add(addButton);
              panel.add(removeButton);
              panel.add(clearButton);
              add(panel, BorderLayout.EAST);
          }

          public void populateTree(DynamicTree treePanel) {
              String p1Name = new String("Parent 1");
              String p2Name = new String("Parent 2");
              String c1Name = new String("Child 1");
              String c2Name = new String("Child 2");

              DefaultMutableTreeNode p1, p2;

              p1 = treePanel.addObject(null, p1Name);
              p2 = treePanel.addObject(null, p2Name);

              treePanel.addObject(p1, c1Name);
              treePanel.addObject(p1, c2Name);

              treePanel.addObject(p2, c1Name);
              treePanel.addObject(p2, c2Name);
          }

          public static void main(String[] args) {
              JFrame frame = new JFrame("DynamicTreeDemo");

              Container contentPane = frame.getContentPane();
              contentPane.setLayout(new GridLayout(1,1));
              contentPane.add(new DynamicTreeDemo(frame));

              frame.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {
                      System.exit(0);
                  }
              });

              frame.pack();
              frame.setVisible(true);
          }
      }
      (Review ID: 93715)
      ======================================================================

      Attachments

        Issue Links

          Activity

            People

              svioletsunw Scott Violet (Inactive)
              wleesunw William Lee (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: