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

public JTree() 

Source Link

Document

Returns a JTree with a sample model.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    MyTreeNode root = new MyTreeNode(1, 0);
    DefaultTreeModel model = new DefaultTreeModel(root);
    JProgressBar bar = new JProgressBar();
    PropertyChangeListener progressListener = new PropertyChangeListener() {
        @Override/*from w ww.  j  a v a 2  s  .com*/
        public void propertyChange(PropertyChangeEvent evt) {
            bar.setValue((Integer) evt.getNewValue());
        }
    };
    JTree tree = new JTree();
    tree.setShowsRootHandles(true);
    tree.addTreeWillExpandListener(new TreeWillExpandListener() {
        @Override
        public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
            TreePath path = event.getPath();
            if (path.getLastPathComponent() instanceof MyTreeNode) {
                MyTreeNode node = (MyTreeNode) path.getLastPathComponent();
                node.loadChildren(model, progressListener);
            }
        }

        @Override
        public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {

        }
    });
    tree.setModel(model);
    root.loadChildren(model, progressListener);
    frame.add(new JScrollPane(tree));
    frame.add(bar, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}

From source file:de.codesourcery.eve.skills.ui.model.FilteringTreeModel.java

public static void main(String[] args) {
    final JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final FilteringTreeModel model = new FilteringTreeModel(createTreeModel());

    // setup text field
    final JTextField filterTextField = new JTextField(30);
    filterTextField.addActionListener(new ActionListener() {

        @Override/*from  w w w . j  a  v a2s .c  om*/
        public void actionPerformed(ActionEvent e) {
            model.viewFilterChanged();
        }
    });

    // setup tree
    final JTree tree = new JTree();
    final IViewFilter<ITreeNode> filter = new AbstractViewFilter<ITreeNode>() {

        @Override
        public boolean isHiddenUnfiltered(ITreeNode node) {
            final String nodeValue = node.toString();
            final String expected = filterTextField.getText();

            final boolean isHidden = !StringUtils.isBlank(nodeValue) && !StringUtils.isBlank(expected)
                    && nodeValue.startsWith("child")
                    && !nodeValue.toLowerCase().contains(expected.toLowerCase());

            System.out.println(nodeValue + " does not match " + expected + " => " + isHidden);
            return isHidden;
        }
    };
    model.setViewFilter(filter);
    tree.setModel(model);

    // set 
    final JPanel panel = new JPanel();

    new GridLayoutBuilder()
            .add(new GridLayoutBuilder.HorizontalGroup(new GridLayoutBuilder.Cell(new JScrollPane(tree)),
                    new GridLayoutBuilder.FixedCell(filterTextField)))
            .addTo(panel);

    frame.getContentPane().add(panel);

    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:Main.java

public static void main() {
    JTree tree = new JTree();
    tree.addTreeWillExpandListener(new MyTreeWillExpandListener());
    tree.addTreeExpansionListener(new MyTreeExpansionListener());

    JFrame f = new JFrame();
    f.add(new JScrollPane(tree));
    f.setSize(300, 300);/*ww  w  .j a  v  a 2s . co  m*/
    f.setVisible(true);

}

From source file:Main.java

public static JComponent makeUI() {
    JTree tree = new JTree();
    TreeModel model = tree.getModel();
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
    Enumeration e = root.breadthFirstEnumeration();
    while (e.hasMoreElements()) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
        Object o = node.getUserObject();
        if (o instanceof String) {
            node.setUserObject(new CheckBoxNode((String) o, false));
        }// w  w  w  . j a  v a 2 s  . c o  m
    }
    tree.setEditable(true);
    tree.setCellRenderer(new CheckBoxNodeRenderer());
    tree.setCellEditor(new CheckBoxNodeEditor());
    tree.expandRow(0);
    return new JScrollPane(tree);
}

From source file:Main.java

public Main() {
    super();//w  w w. j a  va2  s . co m
    JTree tree = new JTree();
    for (int i = 0; i < tree.getRowCount(); i++) {
        tree.expandRow(i);
    }

    final JScrollPane pane = new JScrollPane(tree) {
        Dimension prefSize = new Dimension(200, 150);

        public Dimension getPreferredSize() {
            return prefSize;
        }
    };

    pane.getVerticalScrollBar().addAdjustmentListener(e -> {
        JViewport vp = pane.getViewport();
        if (vp.getView().getHeight() <= vp.getHeight() + vp.getViewPosition().y) {
            System.out.println("End");
        }
    });

    add(pane);
}

From source file:Main.java

public Main() {
    this.tree = new JTree();
    this.add(this.tree);

    File fileRoot = new File("C:/");

    DefaultMutableTreeNode root = new DefaultMutableTreeNode(fileRoot);
    DefaultTreeModel model = new DefaultTreeModel(root);

    File[] subItems = fileRoot.listFiles();
    for (File file : subItems) {
        root.add(new DefaultMutableTreeNode(file));
    }/*from w  w w.  ja v  a2 s  .c  o  m*/

    this.tree.setModel(model);

    this.pack();
}

From source file:Main.java

public Main() {
    JTree myTree = new JTree();
    myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);

    myTree.addMouseListener(new MyMouseAdapter());

    add(new JScrollPane(myTree));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();// w  w w  .  j  a  va 2  s  .c o m
    setVisible(true);
}

From source file:Main.java

public TestPane() {
    setLayout(new BorderLayout());
    tree = new JTree();
    File rootFile = new File(".");
    DefaultMutableTreeNode root = new DefaultMutableTreeNode(rootFile);
    model = new DefaultTreeModel(root);

    tree.setModel(model);// w ww . ja  v a2  s.c  o m
    tree.setRootVisible(true);
    tree.setShowsRootHandles(true);

    add(new JScrollPane(tree));

    JButton load = new JButton("Load");
    add(load, BorderLayout.SOUTH);

    load.addActionListener(e -> {
        DefaultMutableTreeNode r = (DefaultMutableTreeNode) model.getRoot();
        root.removeAllChildren();
        model.reload();
        File f = (File) r.getUserObject();
        addFiles(f, model, r);
        tree.expandPath(new TreePath(r));
    });
}

From source file:net.sf.housekeeper.swing.CategoriesView.java

protected JComponent createControl() {
    tree = new JTree();

    tree.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    final BeanTreeCellRenderer renderer = new BeanTreeCellRenderer(Category.class, "name");
    renderer.setOpenIcon(null);/*w  w w . j a  va  2  s. c  o m*/
    renderer.setClosedIcon(null);
    renderer.setLeafIcon(null);
    tree.setCellRenderer(renderer);
    tree.setRootVisible(true);

    tree.addTreeSelectionListener(new TreeSelectionListener() {

        public void valueChanged(TreeSelectionEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

            if (node == null)
                return;

            Object nodeInfo = node.getUserObject();
            Category cat = (Category) nodeInfo;
            publishSelectionEvent(cat);
        }
    });

    refresh();
    return tree;
}

From source file:ec.nbdemetra.ui.demo.ComponentsDemo.java

public ComponentsDemo() {
    initStaticResources();//from   w  ww.  ja v  a2s . com

    final Map<Id, Component> demoData = lookupComponents();

    final JPanel main = new JPanel(new BorderLayout());
    final JTree tree = new JTree();
    tree.setRootVisible(false);
    tree.setCellRenderer(new IdRenderer(demoData));

    IdsTree.fill(tree, Lists.newArrayList(demoData.keySet()));
    expandAll(tree);

    tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
        @Override
        public void valueChanged(TreeSelectionEvent e) {
            TreePath p = tree.getSelectionPath();
            if (p != null) {
                main.removeAll();
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) p.getLastPathComponent();
                Id id = IdsTree.translate(node);
                Component c = demoData.get(id);
                main.add(c != null ? c : new JPanel());
                main.validate();
                main.repaint();
            }
        }
    });

    JTsList dragDrop = new JTsList();
    dragDrop.setShowHeader(false);
    dragDrop.setInformation(new ITsList.InfoType[] { ITsList.InfoType.TsIdentifier, ITsList.InfoType.Data });
    dragDrop.setPreferredSize(new Dimension(200, 200));
    dragDrop.setTsAction(DemoTsActions.DO_NOTHING);

    JSplitPane left = NbComponents.newJSplitPane(JSplitPane.VERTICAL_SPLIT, NbComponents.newJScrollPane(tree),
            dragDrop);
    JSplitPane splitPane = NbComponents.newJSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, main);
    splitPane.getLeftComponent().setPreferredSize(new Dimension(200, 400));

    setLayout(new BorderLayout());
    add(splitPane, BorderLayout.CENTER);
}