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

Method JTree.SetShowsRootHanles() should not expand tree view.

XMLWordPrintable

    • beta
    • x86
    • linux
    • Verified



      Name: kaC94536 Date: 01/13/2000



      Java 2 Platform SE v1.3 specification reads:

      ...
      public void setShowsRootHandles(boolean newValue)
           Determines whether the node handles are to be displayed.
      ...

      The specification says nothing about tree view changing after this method
      invocation.

      But if a tree view was collapsed then after SetShowsRootHandles() invocation
      the tree becomes expanded. This effect appears in Linux 1.2.2 JDK and in
      Solaris 1.2.2 JDK.

      In the following test clicking on button "Set" call SetShowsRootHandle(true)
      (root handle becomes visible) and clicking on button "Unset" call
      SetShowsRootHandle(false) (root handle becomes invisible). If you collapse tree
      and then click on button "Set" or "Unset", the tree becomes expanded.

      --------------------test.java---------------------------------------------

      import javax.swing.JTree;
      import javax.swing.AbstractButton;
      import javax.swing.JButton;
      import javax.swing.tree.DefaultMutableTreeNode;
      import javax.swing.JScrollPane;
      import javax.swing.JSplitPane;
      import javax.swing.JFrame;
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.JPanel;

      public class test extends JFrame {
          static JTree tree;

          public test() {
              super("JTree.setShowsRootHandle() test");

              DefaultMutableTreeNode top = new DefaultMutableTreeNode("First");
              createNodes(top);
              tree = new JTree(top);
              JScrollPane treeView = new JScrollPane(tree);

              JPanel buttonPanel = new ButtonDemo(tree);
              getContentPane().add(buttonPanel,BorderLayout.CENTER);
              JScrollPane buttonView = new JScrollPane(buttonPanel);

              JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
              splitPane.setTopComponent(treeView);
              splitPane.setBottomComponent(buttonView);

              Dimension minimumSize = new Dimension(100, 50);
              buttonView.setMinimumSize(minimumSize);
              treeView.setMinimumSize(minimumSize);
              splitPane.setDividerLocation(100);
              splitPane.setPreferredSize(new Dimension(500, 300));

              getContentPane().add(splitPane, BorderLayout.CENTER);
          }

         private void createNodes(DefaultMutableTreeNode top) {
              DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("N1");
              top.add(node1);
              DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("N2");
              top.add(node2);
         }

         public static void main(String[] args) {
              JFrame frame = new test();
              frame.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {
                      System.exit(0);
                  }
              });
              frame.pack();
              frame.setVisible(true);
          }
      }

      class ButtonDemo extends JPanel implements ActionListener {
          JButton b1, b2;
          JTree tree;

          public ButtonDemo(JTree t) {
              tree = t;
              b1 = new JButton("set");
              b1.setVerticalTextPosition(AbstractButton.CENTER);
              b1.setHorizontalTextPosition(AbstractButton.LEFT);
              b1.setActionCommand("enable");
              b1.addActionListener(this);
              b1.setToolTipText("setShowsRootHandles(true)");
              add(b1);

              b2 = new JButton("unset");
              b2.setVerticalTextPosition(AbstractButton.BOTTOM);
              b2.setHorizontalTextPosition(AbstractButton.CENTER);
              b2.setActionCommand("disable");
              b2.addActionListener(this);
              b2.setToolTipText("setShowsRootHandles(false)");
              add(b2);
          }

          public void actionPerformed(ActionEvent e) {
              if (e.getActionCommand().equals("enable")) {
                   tree.setShowsRootHandles(true);
               }
              else {
                   tree.setShowsRootHandles(false);
               }
          }
      }

      ======================================================================

      ======================================================================

            svioletsunw Scott Violet (Inactive)
            kanisimosunw Konstantin Anisimov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: