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:ToolTipTreeCellRenderer.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Tree Tips");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Properties props = System.getProperties();
    JTree tree = new JTree(props);

    ToolTipManager.sharedInstance().registerComponent(tree);
    TreeCellRenderer renderer = new ToolTipTreeCellRenderer(props);

    tree.setCellRenderer(renderer);/*from ww  w. j  av a 2 s  .c om*/
    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[]) {
    JFrame f = new JFrame("JTree Demo");
    f.setSize(260, 240);//  www  .j a v  a  2s. c  om
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    DefaultMutableTreeNode aNode = new DefaultMutableTreeNode("A");
    root.add(aNode);

    DefaultMutableTreeNode bNode = new DefaultMutableTreeNode("B");
    aNode.add(bNode);

    DefaultMutableTreeNode cNode = new DefaultMutableTreeNode("C");
    aNode.add(cNode);

    cNode.add(new DefaultMutableTreeNode("D"));
    cNode.add(new DefaultMutableTreeNode("E"));

    DefaultMutableTreeNode fNode = new DefaultMutableTreeNode("F");
    root.add(fNode);

    DefaultMutableTreeNode gNode = new DefaultMutableTreeNode("G");
    fNode.add(gNode);
    fNode.add(new DefaultMutableTreeNode("H"));
    gNode.add(new DefaultMutableTreeNode("I"));

    JTree jtree = new JTree(root);
    jtree.setEditable(true);

    TreeSelectionModel tsm = jtree.getSelectionModel();
    tsm.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    jtree.addTreeExpansionListener(new TreeExpansionListener() {
        public void treeExpanded(TreeExpansionEvent tee) {
            TreePath tp = tee.getPath();
            System.out.println("Expansion: " + tp.getLastPathComponent());
        }

        public void treeCollapsed(TreeExpansionEvent tee) {
            TreePath tp = tee.getPath();
            System.out.println("Collapse: " + tp.getLastPathComponent());
        }
    });

    jtree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent tse) {
            TreePath tp = tse.getPath();
            System.out.println("Selection event: " + tp.getLastPathComponent());
        }
    });

    jtree.getModel().addTreeModelListener(new TreeModelListener() {
        public void treeNodesChanged(TreeModelEvent tme) {
            TreePath tp = tme.getTreePath();
            Object[] children = tme.getChildren();
            DefaultMutableTreeNode changedNode;
            if (children != null)
                changedNode = (DefaultMutableTreeNode) children[0];
            else
                changedNode = (DefaultMutableTreeNode) tp.getLastPathComponent();

            System.out.println("Model change path: " + tp + "New data: " + changedNode.getUserObject());

        }

        public void treeNodesInserted(TreeModelEvent tme) {
        }

        public void treeNodesRemoved(TreeModelEvent tme) {
        }

        public void treeStructureChanged(TreeModelEvent tme) {
        }
    });

    f.add(new JScrollPane(jtree));
    f.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    CheckBoxNode accessibilityOptions[] = { new CheckBoxNode("A", false), new CheckBoxNode("B", true) };
    CheckBoxNode browsingOptions[] = { new CheckBoxNode("C", true), new CheckBoxNode("D", true),
            new CheckBoxNode("E", true), new CheckBoxNode("F", false) };
    Vector accessVector = new NamedVector("G", accessibilityOptions);
    Vector browseVector = new NamedVector("H", browsingOptions);
    Object rootNodes[] = { accessVector, browseVector };
    Vector rootVector = new NamedVector("Root", rootNodes);
    tree = new JTree(rootVector);

    CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
    tree.setCellRenderer(renderer);//from  w  ww  .  j ava  2s.  c o m

    tree.setCellEditor(new CheckBoxNodeEditor(tree));
    tree.setEditable(true);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.getContentPane().add(scrollPane, BorderLayout.NORTH);

    JPanel buttonPanel = new JPanel();
    JButton button = new JButton("new node");
    buttonPanel.add(button);
    frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    button.addActionListener(e -> {
        DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
        DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New node");
        root.add(newNode);
        model.reload();
    });

    frame.setSize(300, 450);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    final JTree tree = new JTree(getTreeModel());
    tree.setCellRenderer(new MyTreeCellRenderer());
    HyperlinkMouseListener listener = new HyperlinkMouseListener(tree);
    tree.addMouseListener(listener);/*from   ww  w.j  a  v  a  2  s  . co m*/
    tree.addMouseMotionListener(listener);
    JFrame f = new JFrame(Main.class.getSimpleName());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new JScrollPane(tree), BorderLayout.CENTER);
    f.pack();
    f.setSize(f.getWidth() + 100, f.getHeight() + 100);
    f.setVisible(true);
}

From source file:CheckBoxNodeTreeSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("CheckBox Tree");

    CheckBoxNode accessibilityOptions[] = {
            new CheckBoxNode("Move system caret with focus/selection changes", false),
            new CheckBoxNode("Always expand alt text for images", true) };
    CheckBoxNode browsingOptions[] = { new CheckBoxNode("Notify when downloads complete", true),
            new CheckBoxNode("Disable script debugging", true), new CheckBoxNode("Use AutoComplete", true),
            new CheckBoxNode("Browse in a new process", false) };
    Vector accessVector = new NamedVector("Accessibility", accessibilityOptions);
    Vector browseVector = new NamedVector("Browsing", browsingOptions);
    Object rootNodes[] = { accessVector, browseVector };
    Vector rootVector = new NamedVector("Root", rootNodes);
    JTree tree = new JTree(rootVector);

    CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
    tree.setCellRenderer(renderer);/*from  www .j  av  a  2s.c  om*/

    tree.setCellEditor(new CheckBoxNodeEditor(tree));
    tree.setEditable(true);

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

From source file:ComponentHier.java

public static void main(String args[]) {
    JFrame frame = new JFrame("JComponent Hierarchy");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Font font = (Font) UIManager.get("Tree.font");
    font = new Font(font.getFontName(), Font.BOLD, font.getSize() - 3);
    UIManager.put("Tree.font", font);

    Vector jEditorPaneVector = new NamedVector("JEditorPane", new Object[] { "JTextPane" });

    Vector jTextFieldVector = new NamedVector("JTextField", new Object[] { "JPasswordField" });

    Vector jTextComponentVector = new NamedVector("JTextComponent",
            new Object[] { jEditorPaneVector, "JTextArea", jTextFieldVector });

    Vector jLayeredPaneVector = new NamedVector("JLayeredPane", new Object[] { "JDesktopPane" });

    Vector jToggleButtonVector = new NamedVector("JToggleButton", new Object[] { "JCheckBox", "JRadioButton" });

    Vector jMenuItemVector = new NamedVector("JMenuItem",
            new Object[] { "JCheckBoxMenuItem", "JMenu", "JRadioButtonMenuItem" });

    Vector abstractButtonVector = new NamedVector("Abstract Button",
            new Object[] { "JButton", jMenuItemVector, jToggleButtonVector });

    Object jComponentNodes[] = { abstractButtonVector, "JColorChooser", "JComboBox", "JFileChooser",
            "JInternalFrame", "JLabel", jLayeredPaneVector, "JList", "JMenuBar", "JOptionPane", "JPanel",
            "JPopupMenu", "JProgressBar", "JRootPane", "JScrollBar", "JScrollPane", "JSeparator", "JSlider",
            "JSplitPane", "JTabbedPane", "JTable", jTextComponentVector, "JToolBar", "JTree", "JViewPort" };
    Vector jComponentVector = new NamedVector("JComponent", jComponentNodes);

    Object rootNodes[] = { jComponentVector };
    Vector rootVector = new NamedVector("Root", rootNodes);

    JTree tree = new JTree(rootVector);
    tree.putClientProperty("JTree.lineStyle", "Angled");

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

From source file:ws.moor.bt.grapher.Grapher.java

public static void main(String[] args) throws IOException {
    if (args.length != 1) {
        System.err.println("Please specify a tab-separated values file");
        System.exit(1);//  ww w  .j ava  2  s .  c  o m
    }
    File file = new File(args[0]);
    final CSVMapCollector collector = new CSVMapCollector(
            new CSVSkipFilter(new CSVInputStream(new FileInputStream(file)), 0 * 1000));

    JFrame window = new JFrame("Grapher");
    window.setSize(1100, 800);
    window.setLayout(new BorderLayout());
    final ChartPanel chartPanel = new ChartPanel(null);

    List<String> possibleNames = collector.getAvailableStreams();
    Collections.sort(possibleNames);
    TreeNode root = convertToTree(possibleNames);

    final JTree tree = new JTree(root);
    tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            List<String> names = new ArrayList<String>();
            final TreePath[] paths = tree.getSelectionModel().getSelectionPaths();
            if (paths == null) {
                chartPanel.setChart(null);
                return;
            }
            for (TreePath path : paths) {
                Object lastPath = path.getLastPathComponent();
                if (lastPath instanceof DefaultMutableTreeNode) {
                    Object value = ((DefaultMutableTreeNode) lastPath).getUserObject();
                    if (value instanceof NodeValue) {
                        names.add(value.toString());
                    }
                }
            }
            chartPanel.setChart(createChart(collector, names.toArray(new String[names.size()])));
        }
    });
    Font font = tree.getFont();
    tree.setFont(font.deriveFont(10.0f));
    JScrollPane scrollPane = new JScrollPane(tree);

    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    splitPane.setLeftComponent(scrollPane);
    splitPane.setRightComponent(chartPanel);
    splitPane.setDividerLocation(200);

    window.setContentPane(splitPane);
    window.setVisible(true);
}

From source file:TreeNodeVectorA.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Employee javaBooks[] = { new Employee("A", "F", 9.99f), new Employee("B", "E", 4.99f),
            new Employee("C", "D", 9.95f) };
    Employee netBooks[] = { new Employee("AA", "CC", 9.99f), new Employee("BB", "DD", 9.99f) };
    Vector<Employee> javaVector = new TreeNodeVectorA<Employee>("A", javaBooks);
    Vector<Employee> netVector = new TreeNodeVectorA<Employee>("As", netBooks);
    Object rootNodes[] = { javaVector, netVector };
    Vector<Object> rootVector = new TreeNodeVectorA<Object>("Root", rootNodes);
    JTree tree = new JTree(rootVector);
    TreeCellRenderer renderer = new EmployeeCellRenderer();
    tree.setCellRenderer(renderer);//from w w w.j  a  v a2  s .com
    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 300);
    frame.setVisible(true);
}

From source file:VSX.java

public static void main(String args[]) {
    JFrame frame = new JFrame("VSX Test");
    VSX parser = new VSX();
    JTree tree = new JTree(parser.parse("build.xml"));
    frame.getContentPane().add(new JScrollPane(tree));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 400);//from   w ww . j av a 2s. c o m
    frame.setVisible(true);
}

From source file:UIDefaultsTreeModel.java

public static void main(String[] args) {
    JFrame treeFrame1 = new JFrame();

    JScrollPane jScrollPane1 = new JScrollPane();
    JTree jTree1 = new JTree(new UIDefaultsTreeModel());

    treeFrame1.setSize(new Dimension(400, 300));
    treeFrame1.addWindowListener(new WindowAdapter() {
        public void windowClosed(WindowEvent e) {
            System.exit(0);/*from w w  w  . ja  v a2  s.  co m*/
        }
    });
    treeFrame1.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    treeFrame1.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
    jScrollPane1.getViewport().add(jTree1, null);

    treeFrame1.setVisible(true);
}