Home Java JTree Examples in Java swing Netbeans

JTree Examples in Java swing Netbeans

3 min read
0
0
1,109

This JTree example used to select the course name from the JTree relavent  select course name and price will be displayed on the textfield.i shown the screen shot image below.

First Step we have to create the DefaultTreeModel model;   i have written the codes below. i will be able see it.

DefaultTreeModel model;
    public view() 
     {
        initComponents();
        Load();
    }
    DefaultMutableTreeNode courses = new DefaultMutableTreeNode("Courses"); 
    public void Load()
    {
        DefaultMutableTreeNode designNode = new DefaultMutableTreeNode("Graphic Designing");
        designNode.add(new DefaultMutableTreeNode("Photoshop"));
        designNode.add(new DefaultMutableTreeNode("CorelDraw"));
        designNode.add(new DefaultMutableTreeNode("InDesign"));
        designNode.add(new DefaultMutableTreeNode("illustrator"));
        DefaultMutableTreeNode programmingNode = new DefaultMutableTreeNode("Programming");
        programmingNode.add(new DefaultMutableTreeNode("C#"));
        programmingNode.add(new DefaultMutableTreeNode("Java"));
        programmingNode.add(new DefaultMutableTreeNode("Asp.Net"));
        programmingNode.add(new DefaultMutableTreeNode("Python"));
        programmingNode.add(new DefaultMutableTreeNode("Php")); 
        courses.add(programmingNode);
        courses.add(designNode); 
        model = (DefaultTreeModel)jTree1.getModel();
        model.setRoot(courses);
        jTree1.setModel(model);

    }

select the JTree Right Click->Events->Mouse->mouseClicked i attached the screen shot image below.

Paste the code inside the Event

private void jTree1MouseClicked(java.awt.event.MouseEvent evt) 
{                                    
  
        courses = (DefaultMutableTreeNode)jTree1.getSelectionPath().getLastPathComponent();
        txtnode.setText(courses.getUserObject().toString());
        
         String select =  courses.getUserObject().toString();
         
         if(select.equals("Photoshop"))
         {
             txtfee.setText("5000");
         }
         else if(select.equals("CorelDraw"))
         {
              txtfee.setText("6000");
         }
          else if(select.equals("InDesign"))
         {
              txtfee.setText("7000");
         }
         else if(select.equals("illustrator"))
         {
              txtfee.setText("9000");
         }
         
         else if(select.equals("C#"))
         {
             txtfee.setText("10000");
         }
         else if(select.equals("Java"))
         {
              txtfee.setText("15000");
         }
          else if(select.equals("Asp.Net"))
         {
              txtfee.setText("17000");
         }
         else if(select.equals("Python"))
         {
              txtfee.setText("19000");
         }
         
          else if(select.equals("Php"))
         {
              txtfee.setText("20000");
         }
 
}

 

i have attached the video link below. which will do this tutorials step by step.

 

 

 

 

  • Java String Methods

    Java provides a rich set of built-in methods for handling String operations efficiently. S…
  • Is Java Hard to Learn?

    Is Java Hard to Learn is it True? Java is one of the most popular programming languages in…
  • How to generate random with weight java

    Generating Random Numbers with Weights in Java Random number generation with weighted prob…
Load More Related Articles
Load More By admin
  • Java Beans

    A Java Bean is a reusable software component that follows a specific set of conventions. J…
  • Java String Methods

    Java provides a rich set of built-in methods for handling String operations efficiently. S…
  • Java Developer Jobs

    Java remains one of the most in-demand programming languages worldwide, powering everythin…
Load More In Java

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also

Build Crud API with Laravel 12

In this tutorial will teach  Laravel 12 CRUD API  by step. Laravel  10 CRUD Application  w…