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(final String args[]) {
    JFrame frame = new JFrame("Editable Tree");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTree tree = new JTree();
    tree.setEditable(true);//from w w  w .  j a  v a2s . c o m

    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();

    DefaultCellEditor comboEditor = new DefaultCellEditor(new JTextField());

    System.out.println(comboEditor.getComponent());

    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(final String args[]) {
    JFrame frame = new JFrame("Editable Tree");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTree tree = new JTree();
    tree.setEditable(true);// w  ww .j a va2  s .  c  om

    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();

    DefaultCellEditor comboEditor = new DefaultCellEditor(new JTextField());

    comboEditor.stopCellEditing();

    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(final String args[]) {
    JFrame frame = new JFrame("Editable Tree");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTree tree = new JTree();
    tree.setEditable(true);/*w ww . j  a v  a 2  s . com*/

    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();

    DefaultCellEditor comboEditor = new DefaultCellEditor(new JTextField());

    comboEditor.getCellEditorValue();

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

public static void main(String args[]) {
    String title = ("JTree Expand Sample");
    JFrame frame = new JFrame(title);
    JTree tree = new JTree();
    TreeWillExpandListener treeWillExpandListener = new TreeWillExpandListener() {
        public void treeWillCollapse(TreeExpansionEvent treeExpansionEvent) throws ExpandVetoException {
            TreePath path = treeExpansionEvent.getPath();
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
            String data = node.getUserObject().toString();
            if (data.equals("colors")) {
                throw new ExpandVetoException(treeExpansionEvent);
            }//from ww w. jav a2s . com
        }

        public void treeWillExpand(TreeExpansionEvent treeExpansionEvent) throws ExpandVetoException {
            TreePath path = treeExpansionEvent.getPath();
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
            String data = node.getUserObject().toString();
            if (data.equals("sports")) {
                throw new ExpandVetoException(treeExpansionEvent);
            }
        }
    };
    tree.addTreeWillExpandListener(treeWillExpandListener);
    JScrollPane scrollPane = new JScrollPane(tree);
    frame.getContentPane().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();
    final ConfirmDialog dialog = new ConfirmDialog(f);
    final JTree tree = new JTree();
    tree.setVisibleRowCount(5);/*  w w  w. j a v  a2 s .co  m*/
    final JScrollPane treeScroll = new JScrollPane(tree);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton b = new JButton("Choose Tree Item");
    b.addActionListener(e -> {
        int result = dialog.showConfirmDialog(treeScroll, "Choose an item");
        if (result == ConfirmDialog.OK_OPTION) {
            System.out.println(tree.getSelectionPath());
        } else {
            System.out.println("User cancelled");
        }
    });
    JPanel p = new JPanel(new BorderLayout());
    p.add(b);
    p.setBorder(new EmptyBorder(50, 50, 50, 50));
    f.setContentPane(p);
    f.pack();
    f.setLocationByPlatform(true);
    f.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    Main dn = new Main(new JTree());
    dn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    dn.setSize(400, 300);/*from   w w w  . j a  v a  2s. com*/
    dn.setVisible(true);
}

From source file:Main.java

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

    Icon empty = new TreeIcon();
    UIManager.put("Tree.closedIcon", empty);
    UIManager.put("Tree.openIcon", empty);
    UIManager.put("Tree.collapsedIcon", empty);
    UIManager.put("Tree.expandedIcon", empty);
    UIManager.put("Tree.leafIcon", empty);

    JTree jt = new JTree();
    frame.add(jt);//from   ww  w  .  j a  v a 2  s.  c  o m
    frame.pack();
    frame.setSize(300, 400);
    frame.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);/*  w  w w  .  jav  a2s  . c  o  m*/
    TreeModel model = tree.getModel();
    Object rootObject = model.getRoot();
    if ((rootObject != null) && (rootObject instanceof DefaultMutableTreeNode)) {
        DefaultMutableTreeNode r = (DefaultMutableTreeNode) rootObject;
        printDescendents(r);
        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:LazySample.java

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

    Integer fifteen = new Integer(15);
    Object lazyArgs[] = new Object[] { Color.GREEN, Boolean.TRUE, fifteen, fifteen };
    Object lazyDiamond = new UIDefaults.ProxyLazyValue("MyIcon", lazyArgs);
    UIManager.put("Tree.openIcon", lazyDiamond);

    JTree tree = new JTree();
    JScrollPane scrollPane = new JScrollPane(tree);

    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(200, 200);/*from w w  w .j a v a2  s .c om*/
    frame.setVisible(true);
}

From source file:EmbedSwingAWTSWT.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("SWT and Swing/AWT Example");

    Composite treeComp = new Composite(shell, SWT.EMBEDDED);

    treeComp.setBounds(5, 5, 300, 300);/*from  w  w  w  .  j  a  v a2  s  . c  o  m*/

    java.awt.Frame fileTableFrame = SWT_AWT.new_Frame(treeComp);
    java.awt.Panel panel = new java.awt.Panel(new java.awt.BorderLayout());
    fileTableFrame.add(panel);
    JTree fileTable = new JTree();
    fileTable.setDoubleBuffered(true);
    JScrollPane scrollPane = new JScrollPane(fileTable);
    panel.add(scrollPane);

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}