Java Usage of jTree and jScrollPane #2
We extend the original tree test by placing it inside a ScrollPane.
All this is done in the IDE.
Next, we make the nodes editable, and monitor any changes that are made.
In general, what is shown in blue is stuff generated by the IDE (not completely, however), and what is shown in black is my typing (I hope you know what I mean).
One starts with the JFrame, and then adds the ScrollPane.
Inside the ScollPane one adds the jTree, and that's all there is.
I will annotate the text in color comments which should be self evident
as to whether they are code or comments.
/*
* TreeTest3.java
* jTree with jScrollPane with Editable tree leaves and nodes
*
* Created on August 20, 2003, 2:21 PM
*/
/**
*
* @author C. W. David
*/
import javax.swing.tree.*;
import javax.swing.event.*;// needed to get TreeModelListener
public class TreeTest3 extends javax.swing.JFrame implements java.awt.event.WindowListener {
/** Creates new form TreeTest3 */
public TreeTest3() {
initComponents();
DefaultMutableTreeNode root=new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode child, grandchild;
for (int childIndex=1;childIndex<4;childIndex++){
child=new DefaultMutableTreeNode("Child "+childIndex);
root.add(child);//add each created child to root
for (int grandChildIndex=1;grandChildIndex<4;grandChildIndex++){
grandchild = new DefaultMutableTreeNode("GrandChild "+childIndex+"."+grandChildIndex);
child.add(grandchild);//add each grandchild to each child
}
}
DefaultTreeModel treeModel = new DefaultTreeModel(root);
jTree1.setModel(treeModel);
/*
*the following (which shows below in its exact proper place)
*was inserted by clicking the editable flag to true in the
*other properties of the tree panel on the IDE.
* >>>>>jTree1.setEditable(true);<<<<<<
*It means that we can edit the leaves and nodes, and
*their alphanumeric representation stays the same.
*/
jTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
treeModel.addTreeModelListener(new MyTreeModelListener());
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {//GEN-BEGIN:initComponents
jScrollPane1 = new javax.swing.JScrollPane();
jTree1 = new javax.swing.JTree();
addWindowListener(this);
jTree1.setEditable(true);
jScrollPane1.setViewportView(jTree1);
getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
pack();
}
// Code for dispatching events from components to event handlers.
public void windowActivated(java.awt.event.WindowEvent evt) {
}
public void windowClosed(java.awt.event.WindowEvent evt) {
}
public void windowClosing(java.awt.event.WindowEvent evt) {
if (evt.getSource() == TreeTest3.this) {
TreeTest3.this.exitForm(evt);
}
}
public void windowDeactivated(java.awt.event.WindowEvent evt) {
}
public void windowDeiconified(java.awt.event.WindowEvent evt) {
}
public void windowIconified(java.awt.event.WindowEvent evt) {
}
public void windowOpened(java.awt.event.WindowEvent evt) {
}//GEN-END:initComponents
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
System.exit(0);
}//GEN-LAST:event_exitForm
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new TreeTest3().show();
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTree jTree1;
// End of variables declaration//GEN-END:variables
class MyTreeModelListener implements TreeModelListener{
//subroutine inserted by IDE:
public void treeNodesChanged(TreeModelEvent e){
/*text from java.sun.com/docs.tutorial/uiswing/components/tree.html
* hand copied directly
*/
DefaultMutableTreeNode node;
node = (DefaultMutableTreeNode)(e.getTreePath().getLastPathComponent());
/*
*If the event lists children, then the changed node
*is the child of the node we've already gotten.
*Otherwise, the changed node and the specified node
*are the same
*/
try{
int index= e.getChildIndices()[0];
node = (DefaultMutableTreeNode)(node.getChildAt(index));
} catch (NullPointerException exc){}
System.out.println("The User has finished editing the node.");
System.out.println("The New Value is "+node.getUserObject());
}
//inserted by IDE:
public void treeNodesInserted(TreeModelEvent e) {
}
public void treeNodesRemoved(TreeModelEvent e) {
}
public void treeStructureChanged(TreeModelEvent e) {
}
}
}
Return to the main book TOC.