Example usage for javax.swing JTree setRootVisible

List of usage examples for javax.swing JTree setRootVisible

Introduction

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

Prototype

@BeanProperty(description = "Whether or not the root node from the TreeModel is visible.")
public void setRootVisible(boolean rootVisible) 

Source Link

Document

Determines whether or not the root node from the TreeModel is visible.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Swing Package Hierarchy");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Vector rootVector = new TreeVector("A", new String[] { "a" });
    JTree tree = new JTree(rootVector);
    tree.setRootVisible(true);
    TreeModel model = tree.getModel();
    model.valueForPathChanged(new TreePath(model.getRoot()), "javax.swing");
    ((DefaultTreeModel) model).reload();

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 300);//from  ww  w  .j  a v a 2 s.  co m
    frame.setVisible(true);
}

From source file:Main.java

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

    Object array[] = { Boolean.TRUE, Boolean.FALSE, "Hello" };
    JTree tree = new JTree(array);
    tree.setEditable(true);/*from  w w  w  .  j  av  a 2 s .  co m*/
    tree.setRootVisible(true);
    JCheckBox checkBox = new JCheckBox();
    TreeCellEditor editor = new DefaultCellEditor(checkBox);
    tree.setCellEditor(editor);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:TreeEdit.java

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

    Object array[] = { Boolean.TRUE, Boolean.FALSE, "Hello" }; // Hello will map to false
    JTree tree = new JTree(array);
    tree.setEditable(true);/*w  w  w  .  j  a  va 2  s . c om*/
    tree.setRootVisible(true);

    JCheckBox checkBox = new JCheckBox();

    TreeCellEditor editor = new DefaultCellEditor(checkBox);
    tree.setCellEditor(editor);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:TreeEdit.java

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

    Object array[] = { Boolean.TRUE, Boolean.FALSE, "Hello" };
    JTree tree = new JTree(array);
    tree.setEditable(true);//w  ww. j  a va  2s  .  co m
    tree.setRootVisible(true);

    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
    String elements[] = { "A", "B", "C", "D" };
    JComboBox comboBox = new JComboBox(elements);
    comboBox.setEditable(true);
    TreeCellEditor comboEditor = new DefaultCellEditor(comboBox);
    TreeCellEditor editor = new DefaultTreeCellEditor(tree, renderer, comboEditor);
    tree.setCellEditor(editor);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Days");
    for (DaysOfTheWeek dotw : DaysOfTheWeek.values()) {
        root.add(new DefaultMutableTreeNode(dotw));
    }//from   w ww.  j a  v  a2  s.  c o m
    final DefaultTreeModel model = new DefaultTreeModel(root);
    JTree tree = new JTree(model);
    tree.setRootVisible(true);
    tree.setShowsRootHandles(true);
    ToolTipManager.sharedInstance().registerComponent(tree);
    tree.setCellRenderer(new MyTreeCellRenderer());
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new JScrollPane(tree));
    f.pack();
    f.setVisible(true);
}

From source file:MainClass.java

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

    JTree tree = new JTree();
    tree.setRootVisible(true);
    TreeModel model = tree.getModel();
    Object rootObject = model.getRoot();
    if ((rootObject != null) && (rootObject instanceof DefaultMutableTreeNode)) {
        DefaultMutableTreeNode r = (DefaultMutableTreeNode) rootObject;
        printDescendents(r);//from  ww w  .j ava 2s  .  c  om
        Enumeration breadth = r.breadthFirstEnumeration();
        Enumeration depth = r.depthFirstEnumeration();
        Enumeration preOrder = r.preorderEnumeration();
        printEnumeration(breadth, "Breadth");
        printEnumeration(depth, "Depth");
        printEnumeration(preOrder, "Pre");
    }

    TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
            JTree treeSource = (JTree) treeSelectionEvent.getSource();
            TreePath path = treeSource.getSelectionPath();
            System.out.println(path);
            System.out.println(path.getPath());
            System.out.println(path.getParentPath());
            System.out.println(((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject());
            System.out.println(path.getPathCount());
        }
    };
    tree.addTreeSelectionListener(treeSelectionListener);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 400);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(10, 11, 212, 500);
    frame.getContentPane().add(scrollPane);

    JTree tree = new JTree(addNodes(new File(".")));
    tree.setRootVisible(false);
    tree.setShowsRootHandles(true);//  w  ww  . j  a va2 s.  co m

    tree.setBorder(new LineBorder(new Color(0, 0, 0)));
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    scrollPane.setViewportView(tree);

    tree.setCellRenderer(new FileTreeCellRenderer());

    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:Main.java

public Main() {
    Object[] genealogy = { "A", "B", "C", "D", "E", "F" };
    java.util.Vector v = new java.util.Vector() {
        public String toString() {
            return "DD";
        }//from   www.j  ava  2 s. c om
    };
    v.addElement("1");
    v.addElement("2");
    v.addElement("3");
    v.addElement("4");
    genealogy[0] = v;
    JTree tree = new JTree(genealogy);
    tree.setRootVisible(true);
    JScrollPane jsp = new JScrollPane(tree);
    getContentPane().add(jsp);
}

From source file:SimpleTreeTest.java

public SimpleTreeTest() {
    Object[] genealogy = { "A", "B", "C", "D", "E", "F" };
    java.util.Vector v = new java.util.Vector() {
        public String toString() {
            return "DD";
        }//from w ww . j  a v a  2  s .  c o  m
    };
    v.addElement("1");
    v.addElement("2");
    v.addElement("3");
    v.addElement("4");
    genealogy[0] = v;
    JTree tree = new JTree(genealogy);
    tree.setRootVisible(true);
    JScrollPane jsp = new JScrollPane(tree);
    getContentPane().add(jsp);
}

From source file:Main.java

public Main() {
    super(BoxLayout.Y_AXIS);
    glassPane.setOpaque(false);//from w  w w.j av a2s .  c  o  m
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    for (int i = 0; i < 14000; i++) {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode("Root" + i);
        node.add(new DefaultMutableTreeNode("Child" + i));
        root.add(node);
    }

    final JTree tree = new JTree(root);
    tree.setRootVisible(false);
    final JScrollPane pane = new JScrollPane(tree);
    add(pane);

    JButton button = new JButton("Expand");
    button.addActionListener(e -> {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                TreePath[] paths = tree.getSelectionPaths();
                if (paths == null || paths.length == 0) {
                    glassPane.setVisible(false);
                    return;
                }
                tree.setSelectionPath(paths[0]);

                for (int i = 0; i < paths.length; i++) {
                    tree.expandPath(paths[i]);
                }
                glassPane.setVisible(false);
            }
        });
        getRootPane().setGlassPane(glassPane);
        glassPane.setVisible(true);
        t.start();
    });
    add(button);
    glassPane.addMouseWheelListener(e -> {
        for (MouseWheelListener mwl : pane.getMouseWheelListeners()) {
            mwl.mouseWheelMoved(e);
        }
    });
}