Java JTree is a swing component that is used to display data in hierarchy manner.JTree mainly comprises of root node which is the par...
Java JTree is a swing component that is used to display data in hierarchy manner.JTree mainly comprises of root node which is the parent.A node may have children's.
Code to develop a simple JTree
import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.SwingUtilities; import javax.swing.tree.DefaultMutableTreeNode; public class JTreeMenu { public JTree tree; public JTreeMenu() { //creating root node DefaultMutableTreeNode root=new DefaultMutableTreeNode("POS"); //creating child nodes DefaultMutableTreeNode transactions=new DefaultMutableTreeNode("Transactions"); DefaultMutableTreeNode reports= new DefaultMutableTreeNode("Reports"); DefaultMutableTreeNode users= new DefaultMutableTreeNode("Users"); DefaultMutableTreeNode stock= new DefaultMutableTreeNode("Stock"); DefaultMutableTreeNode taxes= new DefaultMutableTreeNode("Taxes"); DefaultMutableTreeNode payments= new DefaultMutableTreeNode("Payments Methods"); root.add(transactions); root.add(reports); root.add(users); root.add(stock); root.add(taxes); root.add(payments); //create a tree and pass the root node tree=new JTree(root); JFrame frame=new JFrame("JTree Example"); frame.add(tree); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("JTree Example"); frame.pack(); frame.setVisible(true); } public static void main(String args[]) { new JTreeMenu(); } }
Adding more children to a node
Below code adds more children to reports node
import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.SwingUtilities; import javax.swing.tree.DefaultMutableTreeNode; public class JTreeMenu { public JTree tree; public JTreeMenu() { //creating root node DefaultMutableTreeNode root=new DefaultMutableTreeNode("POS"); //creating child nodes DefaultMutableTreeNode transactions=new DefaultMutableTreeNode("Transactions"); DefaultMutableTreeNode reports= new DefaultMutableTreeNode("Reports"); //adding more children to reports node DefaultMutableTreeNode sales=new DefaultMutableTreeNode("Sales"); DefaultMutableTreeNode customers= new DefaultMutableTreeNode("Customers"); DefaultMutableTreeNode items=new DefaultMutableTreeNode("Items"); reports.add(sales); reports.add(customers); reports.add(items); DefaultMutableTreeNode users= new DefaultMutableTreeNode("Users"); DefaultMutableTreeNode stock= new DefaultMutableTreeNode("Stock"); DefaultMutableTreeNode taxes= new DefaultMutableTreeNode("Taxes"); DefaultMutableTreeNode payments= new DefaultMutableTreeNode("Payments Methods"); root.add(transactions); root.add(reports); root.add(users); root.add(stock); root.add(taxes); root.add(payments); //create a tree and pass the root node tree=new JTree(root); JFrame frame=new JFrame("JTree Example"); frame.add(tree); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("JTree Example"); frame.pack(); frame.setVisible(true); } public static void main(String args[]) { new JTreeMenu(); } }