Java-tree structure

Source: Internet
Author: User
Tags first string

The

Use a jtree can simply be represented as follows:
Add (New JTree (
New object[] {"This", "that", "other"});
The program shows an original tree. The API for the tree is huge, but--of course, it's huge in swing. It shows that we can do anything about the tree, but more complex tasks may require a lot of research and experimentation. Fortunately, a compromise is provided in the library: the "Default" tree component, which is usually what we need. So most of the time we can take advantage of these components, and only in special cases we need to study and understand more deeply.
The following example uses the "default" tree component to display a tree in a single piece of the program. When we press the button, a new subtree is added to the currently selected node (if no node is selected, the root node is used):
 

: Trees.java//Simple Swing tree example.
Trees can be made//vastly more complex than this.
Package c13.swing;
Import java.awt.*;
Import java.awt.event.*;
Import javax.swing.*;

Import javax.swing.tree.*; Takes an array of Strings and makes the I//element a node and the rest leaves:class Branch {defaultmutabletre
  Enode R;
    Public Branch (string[] data) {r = new Defaultmutabletreenode (data[0]);
  for (int i = 1; i < data.length i++) R.add (new Defaultmutabletreenode (data[i)); 
  Public Defaultmutabletreenode node () {return r; } public class Trees extends JPanel {string[][] data = {{"Colors", "Red", "Blue", "Green"}, {"Flavors"
    , "tart", "Sweet", "bland"}, {"Length", "short", "Medium", "Long"}, {"Volume", "High", "Medium", "Low"},
  {' Temperature ', ' high ', ' Medium ', ' low '}, {' Intensity ', ' high ', ' Medium ', ' Low '},};
  static int i = 0;
  Defaultmutabletreenode root, child, chosen;
  JTree Tree; DeFaulttreemodel model;
    Public trees () {setlayout (New BorderLayout ());
    root = new Defaultmutabletreenode ("root");
    Tree = new JTree (root);
    Add it and make it take care of Scrolling:add (new JScrollPane, borderlayout.center);
    Capture the tree ' s Model:model = (Defaulttreemodel) Tree.getmodel ();
    JButton test = new JButton ("Press Me"); Test.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {if (I < Data.len
          GTH) {child = new Branch (data[i++]). Node ();
          What ' s The last one of you clicked?
          chosen = (Defaultmutabletreenode) tree.getlastselectedpathcomponent ();
          if (chosen = = null) chosen = root; The model would create the//appropriate event.
          In response, the//tree would update itself:model.insertNodeInto (child, chosen, 0); This puts the new node on the//currently chosen NOde.
    }
      }
    });
    Change the button ' s colors:test.setBackground (Color.Blue);
    Test.setforeground (Color.White);
    JPanel p = new JPanel ();
    P.add (test);
  Add (P, Borderlayout.south);
  public static void Main (String args[]) {Show.inframe (New trees (), 200,500); }
} ///:~

The most important class is the branch, which is a tool that gets an array of strings and creates a Defaultmutabletreenode for the first string as the root, and the rest in the array as the leaf of the string. The node () method is then called to produce the root of the branch. The tree class consists of a two-dimensional string array from the manufactured branch and a static interrupt I used to count the array. The Defaultmutabletreenode object controls the node, but the on-screen representation is controlled by the JTree and its associated (Defaulttreemodel) patterns. Note that when JTree is added to the program, it is encapsulated into the JScrollPane-this is the automatic scrolling that it provides.
JTree is controlled by its own model. When we modify this model, the model produces an event that causes JTree to complete any necessary upgrades to the visible tree. In Init (), the model is captured by invoking the Getmodel () method. When the button is pressed, a new branch is created. The currently selected component is then found (if no choice is the root) and the Insertnodeinto () method of the model does all the work of changing the tree and causing it to escalate.
Most of the time, like the example above, the program will give us everything we need in the tree. However, the tree has the power to do anything we can imagine--in the example above we can see the word "default" everywhere, we can replace our own class to get different actions. Note, however, that almost all of these classes have a large interface, so we can spend some time trying to understand these intricate tree-like objects.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.