How to create and use Java JTree Menu

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...

How to create and use Java JTree Menu
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();
    }
}


Output

How to create and use Java JTree Menu


COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: How to create and use Java JTree Menu
How to create and use Java JTree Menu
https://2.bp.blogspot.com/-x5r0NebZHdE/WWuhuS6OLOI/AAAAAAAAIS8/8NocUPBdchUnC-Rb7zDG9FrSyBlohDxNgCLcBGAs/s320/java-jtree-example-min.PNG
https://2.bp.blogspot.com/-x5r0NebZHdE/WWuhuS6OLOI/AAAAAAAAIS8/8NocUPBdchUnC-Rb7zDG9FrSyBlohDxNgCLcBGAs/s72-c/java-jtree-example-min.PNG
Java Codes
http://www.download-all.net/2017/07/how-to-create-and-use-java-jtree-menu.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/07/how-to-create-and-use-java-jtree-menu.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy