Java Video Tutorial 27

JTree JavaIn this part of the Java Video Tutorial, I introduce the last Swing component called the JTree. I won’t just be introducing the JTree however. I will also be reviewing much of what you have learned.

It is my belief that if you see tools being used in many different ways, that you will just remember them. I will review dimensions, frames, panels, buttons, scroll bars, listeners and so much more.

Use the code after the video to better learn how to use Java Swing.

If you like videos like this share it

Code From the Video

import javax.swing.*;

import java.awt.event.*;

import java.awt.Dimension;

// Enumerations are used to store related items together

import java.util.Enumeration;

import javax.swing.tree.*;

public class Lesson27 extends JFrame{

	JButton button1;
	String outputString = "";
	
	// A Tree contains nodes that can contain other nodes
	
	JTree theTree;
	
	// If a node holds other nodes it is called a parent node
	// The nodes inside of a parent node are children nodes
	// Nodes on the same level are called siblings
	
	DefaultMutableTreeNode documents, work, games, emails;
	
	DefaultMutableTreeNode fileSystem = new DefaultMutableTreeNode("C Drive");
	
	public static void main(String[] args){
		
		new Lesson27();
		
	}
	
	public Lesson27(){
		
		this.setSize(400,400);
		
		this.setLocationRelativeTo(null);
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.setTitle("My Sixth Frame");
		
		JPanel thePanel = new JPanel();
		
		// Create a button
		
		button1 = new JButton("Get Answer");
		
		ListenForButton lForButton = new ListenForButton();
		
		button1.addActionListener(lForButton);
		
		thePanel.add(button1);
		
		// Create the JTree by passing it the top tree node
		
		theTree = new JTree(fileSystem);
		
		// Makes sure only one item can be selected at a time
		// By default you can make multiple selections
		
		theTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
		
		// Only show 8 rows of the tree at a time
		
		theTree.setVisibleRowCount(8);
		
		// Add the items to the tree documents, work, games
		// We first add the documents parent node
		
		documents = addAFile("Docs", fileSystem);
		
		// Now we add children nodes to the documents parent node
		
		addAFile("Taxes.exl", documents);
		emails = addAFile("Emails", documents);
		addAFile("Story.txt", documents);
		addAFile("Schedule.txt", documents);
		
		// Add a child node to the email node
		
		addAFile("CallBob.txt", emails);
		
		// Create the work node and its children
		
		work = addAFile("Work Applications", fileSystem);
		addAFile("Spreadsheet.exe", work);
		addAFile("Wordprocessor.exe", work);
		addAFile("Presentation.exe", work);
		
		// Create the games node and its children
		
		games = addAFile("Games", fileSystem);
		addAFile("SpaceInvaders.exe", games);
		addAFile("PacMan.exe", games);
		
		// Put the tree in a scroll component
		
		JScrollPane scrollBox = new JScrollPane(theTree);
		
		// Set the size for the JScrollPane so that everything fits
		
		Dimension d = scrollBox.getPreferredSize();
		d.width = 200;
		scrollBox.setPreferredSize(d);
		
		thePanel.add(scrollBox);
		
		this.add(thePanel);
		
		this.setVisible(true);
	
	}
	
	private DefaultMutableTreeNode addAFile(String fileName, DefaultMutableTreeNode parentFolder){
		
		// Creates a new node for the tree
		DefaultMutableTreeNode newFile = new DefaultMutableTreeNode(fileName);
		
		// Add attaches a name to the node
		
		parentFolder.add(newFile);
		
		// return the new node
		
		return newFile;
		
	}
	
	private class ListenForButton implements ActionListener{
		
		public void actionPerformed(ActionEvent e){
		
			if(e.getSource() == button1){
				
				// How to get the selected node
				// Returns the last selected node in the tree
				Object treeObject = theTree.getLastSelectedPathComponent();
				
				// Cast the Object into a DefaultMutableTreeNode
				
				DefaultMutableTreeNode theFile = (DefaultMutableTreeNode) treeObject;
				
				// Returns the object stored in this node and casts it to a string
				
				String treeNode = (String) theFile.getUserObject();
				
				outputString = "The Selected Node: " + treeNode + "\n";
				
				// Get the number of children this node has
				
				outputString += "Number of Children: " + theFile.getChildCount() + "\n";
				
				// Get the number of siblings
				
				outputString += "Number of Siblings: " + theFile.getSiblingCount() + "\n";
				
				// Get the parent of this node
				
				outputString += "The parent: " + theFile.getParent() + "\n";
				
				// Get the next node
				
				outputString += "Next Node: " + theFile.getNextNode() + "\n";
				
				// Get the previous node
				
				outputString += "Next Node: " + theFile.getPreviousNode() + "\n";
				
				// Get the children for the node
				
				outputString += "\nChildren of Node\n";
				
				// children() returns an enumeration that contains all the children
				// This for loop will continue to run as long as there are more elements
				// nextElement() returns the next element in the list
				
				for (Enumeration enumValue = theFile.children(); enumValue.hasMoreElements(); ) {
				
					outputString += enumValue.nextElement() + "\n";
					
				}
				
				// Get the path from the root
				
				outputString += "\nPath From Root\n";
				
				// getPath returns an array of TreeNodes
				
				TreeNode[] pathNodes = theFile.getPath();
				
				// Cycle through the TreeNodes
				
				for(TreeNode indivNodes: pathNodes){
					outputString += indivNodes + "\n";
				}
				
				JOptionPane.showMessageDialog(Lesson27.this, outputString, "Information", JOptionPane.INFORMATION_MESSAGE);
				
				outputString = "";
				
			}
		}
	}
	
}

9 Responses to “Java Video Tutorial 27”

  1. Punit says:

    hello darek, can you please tell me how to generate reports, i mean i have to generate a report in swing, i mean i have fetch some data on panel and have to print that, how can i do this??

    Thank You

  2. Punit says:

    no actually i want to ask is that, i have a page(hard copy), which has fix header and footer(in all the pages, (hard copy)), and now i want to print report in between header and footer, the report is simple, i fetched some data from database(ie. name, age and all).
    how do i print that on paper??

  3. reuven says:

    how do i place the tree to the right?

  4. Aamir says:

    hi derek i have a question,
    whenever u are calling addAFile(“Taxes.exl”, documents);
    here i know what happens taxes.exl gets added to documents.
    but i have a confusion, when we call the method like this
    addAFile(“Taxes.exl”, documents);
    the method returns a node and in this case its taxes.exl but when the method returns the node we are not holding it anywhere when control returns from addaFile method and even compiler does not show the error. can u explain whaz is going on here plz

    Regards,
    Aamir
    Banglore,India.

Leave a Reply

Your email address will not be published.

Google+