.useful Java library

About
License

Documentation

Why dotuseful?
Dynamic Tree Tutorial
XMLTree Tutorial
API Reference

 

Articles

Post-MVC

 

Download
Forums
.useful Team
Contribute

Hosted by

SourceForge.net Logo

Tutorial - Dynamic Tree

//in development

A quick introduction on how to start using dotuseful tree classes.

You can compare it with Java tutorial on Trees here
http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#dynamic

package org.dotuseful.examples.ui.tree;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;

public class DynaTreeApp extends JFrame implements ActionListener {
	protected JButton addButton;

	protected JButton removeButton;

	public DynaTreeApp() {
		super("Dynamic Nodes in Action");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		JPanel p = createPanel();
		setContentPane(p);
		pack();
		setVisible(true);
	}

	private JPanel createPanel() {
		JPanel mainPanel = new JPanel(new BorderLayout());
		mainPanel.add(new JScrollPane(new JTree()));
		mainPanel.add(createControlPanel(), BorderLayout.LINE_END);
		return mainPanel;
	}

	protected JPanel createControlPanel() {
		JPanel cp = new JPanel();
		cp.setLayout(new BoxLayout(cp, BoxLayout.PAGE_AXIS));
		cp.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		//
		// Add Button
		addButton = new JButton("Add");
		addButton.addActionListener(this);
		cp.add(addButton);
		// Space
		cp.add(Box.createRigidArea(new Dimension(0, 10)));
		//
		// Remove Button
		removeButton = new JButton("Remove");
		removeButton.addActionListener(this);
		cp.add(removeButton);
		return cp;
	}

	public static void main(String[] args) {
		new DynaTreeApp();
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(addButton)) {
			//add
		} else {
			//remove
		}
	}
}

[SCREENSHOT]

Now we add some functionality. We need to ADD a node to the SELECTED node of the TREE. Who knows about NODES and SELECTED one? It's our JTree.

Let's create a JTree which could be messaged when button has been pressed.

package org.dotuseful.examples.ui.tree;

import javax.swing.JTree;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;
import org.dotuseful.ui.tree.AutomatedTreeModel;

public class DynaTree extends JTree {
    public DynaTree() {
    }

    public void addPressed() {
        //Notify a node of Add
    }

    public void removePressed() {
        //Notify a node of Remove
    }
}

Now the most important part. Let's create a flexible tree node which will add children to itself and remove them.

package org.dotuseful.examples.ui.tree;

import java.util.Random;
import javax.swing.tree.MutableTreeNode;
import org.dotuseful.ui.tree.AutomatedTreeNode;

public class DynaTreeNode extends AutomatedTreeNode {
    protected int totalChildCound = 0;

    public DynaTreeNode(Object userObject) {
        super(userObject);
    }

    public void addPressed() {
        totalChildCound++;
        DynaTreeNode newChild = new DynaTreeNode(getUserObject() + "'s "
                + totalChildCound + " - " + newName());
        add(newChild);
    }

    protected String newName() {
        String[] names = { "Mercury", "Venus", "Earth", "Mars", "Phaeton",
                "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto" };
        Random rand = new Random();
        return names[(rand.nextInt(10))];
    }

    public void removePressed(MutableTreeNode node) {
        remove(node);
    }
}



package org.dotuseful.examples.ui.tree;

import javax.swing.JTree;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;
import org.dotuseful.ui.tree.AutomatedTreeModel;

public class DynaTree extends JTree {
    public DynaTree() {
        super(new AutomatedTreeModel(new DynaTreeNode("The Mother")));
    }

    public void addPressed() {
        DynaTreeNode selectedNode;
        TreePath parentPath = getSelectionPath();
        if (parentPath != null) {
            selectedNode = (DynaTreeNode) (parentPath.getLastPathComponent());
            selectedNode.addPressed();
        }
    }

    public void removePressed() {
        DynaTreeNode parentNode;
        TreePath parentPath = getSelectionPath();
        if (parentPath != null) {
            parentNode = (DynaTreeNode) (parentPath.getPathComponent(parentPath
                    .getPathCount() - 2));
            parentNode.removePressed((MutableTreeNode) (parentPath
                    .getLastPathComponent()));
        }
    }
}



package org.dotuseful.examples.ui.tree;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class DynaTreeApp extends JFrame implements ActionListener {
    protected DynaTree tree;

    protected JButton addButton;

    protected JButton removeButton;

    public DynaTreeApp() {
        super("Dynamic Nodes in Action");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel p = createPanel();
        setContentPane(p);
        pack();
        setVisible(true);
    }

    private JPanel createPanel() {
        tree = new DynaTree();
        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.add(new JScrollPane(tree));
        mainPanel.add(createControlPanel(), BorderLayout.LINE_END);
        return mainPanel;
    }

    protected JPanel createControlPanel() {
        JPanel cp = new JPanel();
        cp.setLayout(new BoxLayout(cp, BoxLayout.PAGE_AXIS));
        cp.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        //
        // Add Button
        addButton = new JButton("Add");
        addButton.addActionListener(this);
        cp.add(addButton);
        // Space
        cp.add(Box.createRigidArea(new Dimension(0, 10)));
        //
        // Remove Button
        removeButton = new JButton("Remove");
        removeButton.addActionListener(this);
        cp.add(removeButton);
        return cp;
    }

    public static void main(String[] args) {
        new DynaTreeApp();
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(addButton)) {
            tree.addPressed();
        } else {
            tree.removePressed();
        }
    }
}

 

Have thoughts? We want to hear from you! Contact us on our forums.