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[] argv) throws Exception {
    JTree tree = new JTree();
    TreePath path = findByName(tree, new String[] { "JTree", "A", "a" });

}

From source file:Main.java

public static void main(String args[]) {
    JTree tree = new JTree();
    TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {

        @Override//from   w ww .  j a  v a2  s . co  m
        public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
            JTree treeSource = (JTree) treeSelectionEvent.getSource();
            System.out.println("Min: " + treeSource.getMinSelectionRow());
            System.out.println("Max: " + treeSource.getMaxSelectionRow());
            System.out.println("Lead: " + treeSource.getLeadSelectionRow());
            System.out.println("Row: " + treeSource.getSelectionRows()[0]);
        }
    };
    tree.addTreeSelectionListener(treeSelectionListener);
    JFrame frame = new JFrame();
    frame.add(new JScrollPane(tree));
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame f = new JFrame("JTree Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTree tree = new JTree();
    JScrollPane scrollPane = new JScrollPane(tree);
    f.add(scrollPane, BorderLayout.CENTER);
    f.setSize(300, 200);// ww w .  jav  a  2 s. c  o m
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) {
    JTree tree = new JTree();
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    int treeSelectedRows[] = { 3, 1 };
    tree.setSelectionRows(treeSelectedRows);
    TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {

        @Override/*from   w ww.j  a  va 2 s .c  om*/
        public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
            JTree treeSource = (JTree) treeSelectionEvent.getSource();
            System.out.println("Min: " + treeSource.getMinSelectionRow());
            System.out.println("Max: " + treeSource.getMaxSelectionRow());
            System.out.println("Lead: " + treeSource.getLeadSelectionRow());
            System.out.println("Row: " + treeSource.getSelectionRows()[0]);
        }
    };
    tree.addTreeSelectionListener(treeSelectionListener);
    JFrame frame = new JFrame("JTree With Multi-Discontiguous selection");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(tree));
    frame.setPreferredSize(new Dimension(380, 320));
    frame.setLocation(150, 150);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) {
    JTree tree = new JTree();
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    int treeSelectedRows[] = { 3, 1 };
    tree.setSelectionRows(treeSelectedRows);
    TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {

        @Override/*from   w  w  w  .j  av a  2 s  . c o m*/
        public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
            JTree treeSource = (JTree) treeSelectionEvent.getSource();
            System.out.println("Min: " + treeSource.getMinSelectionRow());
            System.out.println("Max: " + treeSource.getMaxSelectionRow());
            System.out.println("Lead: " + treeSource.getLeadSelectionRow());
            System.out.println("Row: " + treeSource.getSelectionRows()[0]);
        }
    };
    tree.addTreeSelectionListener(treeSelectionListener);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(tree));
    frame.setPreferredSize(new Dimension(380, 320));
    frame.setLocation(150, 150);
    frame.pack();
    frame.setVisible(true);
}

From source file:TreeSelectionRow.java

public static void main(String args[]) {
    String title = "JTree Sample";
    JFrame frame = new JFrame(title);
    JTree tree = new JTree();
    TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
            JTree treeSource = (JTree) treeSelectionEvent.getSource();
            System.out.println("Min: " + treeSource.getMinSelectionRow());
            System.out.println("Max: " + treeSource.getMaxSelectionRow());
            System.out.println("Lead: " + treeSource.getLeadSelectionRow());
            System.out.println("Row: " + treeSource.getSelectionRows()[0]);
        }//from   ww w.ja  v  a2  s. c om
    };
    tree.addTreeSelectionListener(treeSelectionListener);

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

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

From source file:MainClass.java

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

    Box box = Box.createHorizontalBox();

    JTree tree1 = new JTree();
    JScrollPane scrollPane1 = new JScrollPane(tree1);
    tree1.setAutoscrolls(true);//from  w  w w  .  j  ava  2 s . c o m

    JTree tree2 = new JTree();
    tree2.putClientProperty("JTree.lineStyle", "None");
    JScrollPane scrollPane2 = new JScrollPane(tree2);

    box.add(scrollPane1, BorderLayout.WEST);
    box.add(scrollPane2, BorderLayout.EAST);

    frame.add(box, BorderLayout.CENTER);
    frame.setSize(300, 240);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Changed Renderer");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTree tree = new JTree();
    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();

    renderer.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 32));
    int rowHeight = tree.getRowHeight();
    if (rowHeight <= 0) {
        tree.setRowHeight(rowHeight - 1);
    }/*from   w  w  w  .  ja va  2s  .  co  m*/

    Color backgroundSelection = renderer.getBackgroundSelectionColor();
    renderer.setBackgroundSelectionColor(Color.blue);
    renderer.setBackgroundNonSelectionColor(backgroundSelection);

    // Swap text colors
    Color textSelection = renderer.getTextSelectionColor();
    renderer.setTextSelectionColor(Color.green);
    renderer.setTextNonSelectionColor(textSelection);

    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) {
    JPanel panel = new JPanel(new BorderLayout());
    final JTree tree = new JTree();
    panel.add(new JScrollPane(tree));
    JButton btn = new JButton("Press Me");
    btn.addActionListener(et -> {/*from  w  ww.ja v  a2s . c o  m*/
        for (Enumeration e = ((TreeNode) tree.getModel().getRoot()).children(); e.hasMoreElements();) {
            TreeNode tn = (TreeNode) e.nextElement();
            tree.expandPath(new TreePath(((DefaultTreeModel) tree.getModel()).getPathToRoot(tn)));
        }
    });
    panel.add(btn, BorderLayout.SOUTH);
    JFrame frame = new JFrame("");
    frame.getContentPane().add(panel);
    frame.setSize(300, 300);
    frame.setLocation(100, 100);
    frame.pack();
    frame.show();
}

From source file:TreeLines.java

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

    Container content = frame.getContentPane();
    Box box = Box.createHorizontalBox();

    JTree tree1 = new JTree();
    tree1.putClientProperty("JTree.lineStyle", "Angled");
    JScrollPane scrollPane1 = new JScrollPane(tree1);
    tree1.setAutoscrolls(true);/*from w  w w . jav  a  2s .c o  m*/

    JTree tree2 = new JTree();
    JScrollPane scrollPane2 = new JScrollPane(tree2);

    box.add(scrollPane1, BorderLayout.WEST);
    box.add(scrollPane2, BorderLayout.EAST);

    frame.getContentPane().add(box, BorderLayout.CENTER);
    frame.setSize(300, 240);
    frame.setVisible(true);
}