I create a separate class extending JTree. For example, I decided to try out the tutorial

Here is the class:

public class Tree extends JTree { private DefaultMutableTreeNode rootNode; private DefaultTreeModel treeModel; public Tree() { rootNode = new DefaultMutableTreeNode("Root Node"); createNodes(rootNode); treeModel = new DefaultTreeModel(rootNode, true);  setRootVisible(true); setPreferredSize(new Dimension(250, 400)); setMaximumSize(new Dimension(250, 400)); } private void createNodes(DefaultMutableTreeNode top) { DefaultMutableTreeNode category= null; DefaultMutableTreeNode book= null;  category = new DefaultMutableTreeNode("Books for Java Programmers"); top.add(category); //original Tutorial book = new DefaultMutableTreeNode("The Java Tutorial: A Short Course on the Basics"); category.add(book); //Tutorial Continued book = new DefaultMutableTreeNode("The Java Tutorial Continued: The Rest of the JDK"); category.add(book); //JFC Swing Tutorial book = new DefaultMutableTreeNode("The JFC Swing Tutorial: A Guide to Constructing GUIs"); category.add(book); //...add more books for programmers... category = new DefaultMutableTreeNode("Books for Java Implementers"); top.add(category); //VM book = new DefaultMutableTreeNode("The Java Virtual Machine Specification"); category.add(book); //Language Spec book = new DefaultMutableTreeNode("The Java Language Specification"); category.add(book); } } 

 public class Tree extends JTree { private DefaultMutableTreeNode rootNode; private DefaultTreeModel treeModel; public Tree() { rootNode = new DefaultMutableTreeNode("Root Node"); createNodes(rootNode); treeModel = new DefaultTreeModel(rootNode, true);  setRootVisible(true); setPreferredSize(new Dimension(250, 400)); setMaximumSize(new Dimension(250, 400)); } private void createNodes(DefaultMutableTreeNode top) { DefaultMutableTreeNode category= null; DefaultMutableTreeNode book= null;  category = new DefaultMutableTreeNode("Books for Java Programmers"); top.add(category); //original Tutorial book = new DefaultMutableTreeNode("The Java Tutorial: A Short Course on the Basics"); category.add(book); //Tutorial Continued book = new DefaultMutableTreeNode("The Java Tutorial Continued: The Rest of the JDK"); category.add(book); //JFC Swing Tutorial book = new DefaultMutableTreeNode("The JFC Swing Tutorial: A Guide to Constructing GUIs"); category.add(book); //...add more books for programmers... category = new DefaultMutableTreeNode("Books for Java Implementers"); top.add(category); //VM book = new DefaultMutableTreeNode("The Java Virtual Machine Specification"); category.add(book); //Language Spec book = new DefaultMutableTreeNode("The Java Language Specification"); category.add(book); } } 

public class Tree extends JTree { private DefaultMutableTreeNode rootNode; private DefaultTreeModel treeModel; public Tree() { rootNode = new DefaultMutableTreeNode("Root Node"); createNodes(rootNode); treeModel = new DefaultTreeModel(rootNode, true); setRootVisible(true); setPreferredSize(new Dimension(250, 400)); setMaximumSize(new Dimension(250, 400)); } private void createNodes(DefaultMutableTreeNode top) { DefaultMutableTreeNode category= null; DefaultMutableTreeNode book= null; category = new DefaultMutableTreeNode("Books for Java Programmers"); top.add(category); //original Tutorial book = new DefaultMutableTreeNode("The Java Tutorial: A Short Course on the Basics"); category.add(book); //Tutorial Continued book = new DefaultMutableTreeNode("The Java Tutorial Continued: The Rest of the JDK"); category.add(book); //JFC Swing Tutorial book = new DefaultMutableTreeNode("The JFC Swing Tutorial: A Guide to Constructing GUIs"); category.add(book); //...add more books for programmers... category = new DefaultMutableTreeNode("Books for Java Implementers"); top.add(category); //VM book = new DefaultMutableTreeNode("The Java Virtual Machine Specification"); category.add(book); //Language Spec book = new DefaultMutableTreeNode("The Java Language Specification"); category.add(book); } }

 public class Tree extends JTree { private DefaultMutableTreeNode rootNode; private DefaultTreeModel treeModel; public Tree() { rootNode = new DefaultMutableTreeNode("Root Node"); createNodes(rootNode); treeModel = new DefaultTreeModel(rootNode, true);  setRootVisible(true); setPreferredSize(new Dimension(250, 400)); setMaximumSize(new Dimension(250, 400)); } private void createNodes(DefaultMutableTreeNode top) { DefaultMutableTreeNode category= null; DefaultMutableTreeNode book= null;  category = new DefaultMutableTreeNode("Books for Java Programmers"); top.add(category); //original Tutorial book = new DefaultMutableTreeNode("The Java Tutorial: A Short Course on the Basics"); category.add(book); //Tutorial Continued book = new DefaultMutableTreeNode("The Java Tutorial Continued: The Rest of the JDK"); category.add(book); //JFC Swing Tutorial book = new DefaultMutableTreeNode("The JFC Swing Tutorial: A Guide to Constructing GUIs"); category.add(book); //...add more books for programmers... category = new DefaultMutableTreeNode("Books for Java Implementers"); top.add(category); //VM book = new DefaultMutableTreeNode("The Java Virtual Machine Specification"); category.add(book); //Language Spec book = new DefaultMutableTreeNode("The Java Language Specification"); category.add(book); } } 

public class Tree extends JTree { private DefaultMutableTreeNode rootNode; private DefaultTreeModel treeModel; public Tree() { rootNode = new DefaultMutableTreeNode("Root Node"); createNodes(rootNode); treeModel = new DefaultTreeModel(rootNode, true); setRootVisible(true); setPreferredSize(new Dimension(250, 400)); setMaximumSize(new Dimension(250, 400)); } private void createNodes(DefaultMutableTreeNode top) { DefaultMutableTreeNode category= null; DefaultMutableTreeNode book= null; category = new DefaultMutableTreeNode("Books for Java Programmers"); top.add(category); //original Tutorial book = new DefaultMutableTreeNode("The Java Tutorial: A Short Course on the Basics"); category.add(book); //Tutorial Continued book = new DefaultMutableTreeNode("The Java Tutorial Continued: The Rest of the JDK"); category.add(book); //JFC Swing Tutorial book = new DefaultMutableTreeNode("The JFC Swing Tutorial: A Guide to Constructing GUIs"); category.add(book); //...add more books for programmers... category = new DefaultMutableTreeNode("Books for Java Implementers"); top.add(category); //VM book = new DefaultMutableTreeNode("The Java Virtual Machine Specification"); category.add(book); //Language Spec book = new DefaultMutableTreeNode("The Java Language Specification"); category.add(book); } }

When creating an instance of this class, for some reason in the frame the default tree (Jtree> Colors> Sports> Food), I do not understand what is the matter?)

    1 answer 1

    Better show the code where you add and create a tree on the form. In the designer it is necessary to transfer model.

     DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series"); createNodes(top); tree = new JTree(top);' 

    In general, it is written there.

    • That is, it turns out, I need to create a model separately and pass it to the constructor? - Kobayashi_Maru
    • aaa, I understood everything)) don `t know - what is needed in the designer, be sure to transfer the model ... thank you! - Kobayashi_Maru
    • one
      Not necessarily in the constructor. You can shove the model through <a href=" download.oracle.com/javase/6/docs/api/javax/swing/…> . In general, learn to flip through the dock. Use JTree, so at least read the introduction to the class. There's a text with gulkin nose, and good use. - cy6erGn0m
    • one
      Thanks, I will test setModel. Just hurried and missed the obvious ... - Kobayashi_Maru