Example usage for javax.swing JTree JTree

List of usage examples for javax.swing JTree JTree

Introduction

In this page you can find the example usage for javax.swing JTree JTree.

Prototype

@ConstructorProperties({ "model" })
public JTree(TreeModel newModel) 

Source Link

Document

Returns an instance of JTree which displays the root node -- the tree is created using the specified data model.

Usage

From source file:TreeCellRendererImplementation.java

public TreeCellRendererImplementation() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("+");
    root.add(new DefaultMutableTreeNode(new Integer(3)));
    DefaultMutableTreeNode node = new DefaultMutableTreeNode("*");
    node.add(new DefaultMutableTreeNode("string"));
    node.add(new DefaultMutableTreeNode(new Short((short) 5)));
    root.add(node);/*  w w  w .  jav  a 2  s .  com*/

    TreeModel tm = new DefaultTreeModel(root);
    JTree tree = new JTree(tm);
    tree.setShowsRootHandles(true);
    tree.setCellRenderer(new MyRenderer());
    getContentPane().add(tree, BorderLayout.CENTER);
    setSize(400, 300);
    setVisible(true);
}

From source file:FileTreeFrame.java

public FileTreeFrame(String directory) {
    super("JTree FileSystem Viewer");
    fileDetailsTextArea.setEditable(false);
    fileSystemModel = new FileSystemModel(new File(directory));
    fileTree = new JTree(fileSystemModel);
    fileTree.setEditable(true);//from  ww  w.j  a  v a2  s . c om
    fileTree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent event) {
            File file = (File) fileTree.getLastSelectedPathComponent();
            fileDetailsTextArea.setText(getFileDetails(file));
        }
    });
    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, new JScrollPane(fileTree),
            new JScrollPane(fileDetailsTextArea));
    getContentPane().add(splitPane);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(640, 480);
    setVisible(true);
}

From source file:StAXStreamTreeViewer.java

public StAXStreamTreeViewer() throws Exception {
    StAXStreamTreeViewer viewer = new StAXStreamTreeViewer();
    File file = new File("yourXML.xml");
    DefaultMutableTreeNode base = new DefaultMutableTreeNode("XML Document: " + file.getAbsolutePath());

    defaultTreeModel = new DefaultTreeModel(base);
    jTree = new JTree(defaultTreeModel);

    buildTree(defaultTreeModel, base, file);

    getContentPane().add(new JScrollPane(jTree), BorderLayout.CENTER);
    viewer.setVisible(true);/*from  ww  w  . ja v  a2s  . co m*/
    setSize(800, 450);
}

From source file:Main.java

public Main() {
    setLayout(new GridLayout(1, 3));
    tree = new JTree(getTreeModel());
    tree.setDragEnabled(true);/* ww w . ja v  a  2 s.  co  m*/
    tree.setPreferredSize(new Dimension(200, 400));
    JScrollPane scroll = new JScrollPane();
    scroll.setViewportView(tree);

    treeModel = getTreeModel();
    JTree secondTree = new JTree(treeModel);
    secondTree.setPreferredSize(new Dimension(200, 400));
    secondTree.setTransferHandler(new TransferHandler() {
        @Override
        public boolean importData(TransferSupport support) {
            if (!canImport(support)) {
                return false;
            }
            JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
            TreePath path = dl.getPath();
            int childIndex = dl.getChildIndex();

            String data;
            try {
                data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
            } catch (Exception e) {
                return false;
            }
            if (childIndex == -1) {
                childIndex = tree.getModel().getChildCount(path.getLastPathComponent());
            }

            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(data);
            DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();
            treeModel.insertNodeInto(newNode, parentNode, childIndex);

            tree.makeVisible(path.pathByAddingChild(newNode));
            tree.scrollRectToVisible(tree.getPathBounds(path.pathByAddingChild(newNode)));
            return true;
        }

        public boolean canImport(TransferSupport support) {
            if (!support.isDrop()) {
                return false;
            }
            support.setShowDropLocation(true);
            if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                System.out.println("only string is supported");
                return false;
            }
            JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
            TreePath path = dl.getPath();
            if (path == null) {
                return false;
            }
            return true;
        }
    });
    JScrollPane secondScroll = new JScrollPane();
    secondScroll.setViewportView(secondTree);

    JPanel topPanel = new JPanel(new BorderLayout());
    topPanel.add(scroll, BorderLayout.CENTER);
    JPanel btmPanel = new JPanel(new BorderLayout());
    btmPanel.add(secondScroll, BorderLayout.CENTER);

    add(topPanel);
    add(btmPanel);
}

From source file:Main.java

public Main() {
    Container content = getContentPane();
    Object[] hierarchy = { "javax.swing", "javax.swing.border", "javax.swing.colorchooser", "javax.swing.event",
            "javax.swing.filechooser",
            new Object[] { "javax.swing.plaf", "javax.swing.plaf.basic", "javax.swing.plaf.metal",
                    "javax.swing.plaf.multi" },
            "javax.swing.table",
            new Object[] { "javax.swing.text",
                    new Object[] { "javax.swing.text.html", "javax.swing.text.html.parser" },
                    "javax.swing.text.rtf" },
            "javax.swing.tree", "javax.swing.undo" };
    DefaultMutableTreeNode root = processHierarchy(hierarchy);
    JTree tree = new JTree(root);
    content.add(new JScrollPane(tree), BorderLayout.CENTER);
    setSize(275, 300);/* ww  w .j a  v  a 2s  .  c  o  m*/
    setVisible(true);
}

From source file:SortTreeDemo.java

public SortTreeDemo(String startDir) {
    super("SortTreeModel Demonstration");
    setSize(300, 400);//from  ww  w.jav a  2  s .  c  om
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    PrettyFile f = new PrettyFile(startDir);
    DefaultMutableTreeNode root = new DefaultMutableTreeNode(f);
    SortTreeModel model = new SortTreeModel(root, new TreeStringComparator());
    fillModel(model, root);

    JTree tree = new JTree(model);
    getContentPane().add(new JScrollPane(tree));
}

From source file:AncestorTree.java

public AncestorTree() {
    super("Ancestor Tree");
    setSize(400, 300);//  w  w w  .j  a  v  a  2  s .  com

    DefaultMutableTreeNode top = new DefaultMutableTreeNode(new IconData(ICON_SELF, "Myself"));
    addAncestors(top);
    m_model = new DefaultTreeModel(top);
    m_tree = new JTree(m_model);
    m_tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    m_tree.setShowsRootHandles(true);
    m_tree.setEditable(true);

    m_renderer = new IconCellRenderer();
    m_tree.setCellRenderer(m_renderer);
    m_editor = new IconCellEditor(m_tree);
    m_tree.setCellEditor(m_editor);
    m_tree.setInvokesStopCellEditing(true);

    m_tree.addMouseListener(new TreeExpander());

    JScrollPane s = new JScrollPane();
    s.getViewport().add(m_tree);
    getContentPane().add(s, BorderLayout.CENTER);

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

    setVisible(true);
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);//from ww  w  .j a  va 2  s. co  m

    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    JButton btnNewButton = new JButton("Click Me");
    btnNewButton.addActionListener(e -> doClick());
    contentPane.add(btnNewButton, BorderLayout.NORTH);
    jarFilesDB = new MyTreeModel();
    jarFilesDB.load(isAlpha);
    isAlpha = !isAlpha;
    jtree = new JTree(jarFilesDB);
    JScrollPane scrollPane = new JScrollPane(jtree);
    contentPane.add(scrollPane, BorderLayout.CENTER);
}

From source file:Main.java

void initContainer(Container container) {
    container.setLayout(new GridLayout(1, 0));
    buttonPanel = new JPanel(new GridLayout(0, 1));
    Object[] menuNames = { "ROOT",
            new Object[] { "A", new Object[] { "CSS", "HTML", "SQL", "Java" }, "Code",
                    new Object[] { "Test", "S", "C" } },
            new Object[] { "Code 1", new Object[] { "A", "I", "H", "O" }, "Code",
                    new Object[] { "P", "S", "C" }, "C" } };

    DefaultMutableTreeNode currentNode = processHierarchy(menuNames);
    menuTree = new JTree(currentNode);
    menuTree.setVisibleRowCount(10);/*from   w ww .j a v  a 2  s . co  m*/
    menuTree.expandRow(2);
    initializeButtons(currentNode);
    container.add(buttonPanel, BorderLayout.WEST);
    container.add(new JScrollPane(menuTree), BorderLayout.EAST);
    menuTree.addTreeSelectionListener(e -> {
        initializeButtons((DefaultMutableTreeNode) menuTree.getLastSelectedPathComponent());
    });
}

From source file:MainClass.java

public MainClass() {
    this.setSize(225, 325);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel1 = new JPanel();

    DefaultMutableTreeNode root, a, b, c, d, e;

    root = new DefaultMutableTreeNode("A");

    a = makeShow("B", root);
    makeShow("C", a);
    makeShow("D", a);

    b = makeShow("E", root);
    d = makeShow("F", b);
    makeShow("G", d);
    e = makeShow("H", b);
    makeShow("I", e);
    makeShow("J", b);
    makeShow("K", b);

    c = makeShow("L", root);
    makeShow("M", c);
    makeShow("N", c);
    makeShow("O", c);

    tree1 = new JTree(root);
    tree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree1.setVisibleRowCount(12);//from w  w  w . j  av  a  2 s  . c  om
    tree1.addTreeSelectionListener(new TreeListener());

    JScrollPane scroll = new JScrollPane(tree1);
    panel1.add(scroll);

    panel1.add(showName);
    this.add(panel1);
    this.setVisible(true);
}