Here is my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import javax.swing.JPanel;
public class Trees
{ public static void main(String[] args)
{ SelectableTree tree = new SelectableTree();
}
}
// The tree class to be used as the framework for cs258 lab3.
class SelectableTree extends JFrame implements TreeSelectionListener
{ private JTree tree; // Reference tree that will be created.
private JLabel selectField; // Reference label for messages.
private JScrollPane scrollArea; // Reference scroll area to display tree.
private int childName = 1;// Node name.
// The constructor method.
public SelectableTree()
{ super("JTree Selections"); // Set the title line.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Enable the exit button.
// Create the tree nodes.
DefaultMutableTreeNode grandChild, child; // References to tree nodes.
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
// In a loop create children and grandchildren.
// Link the children to the root and the grandchildren to the children.
for (int childX=1; childX<=4; childX++)
{ child = new DefaultMutableTreeNode("Node " + childName++);
root.add(child);
for (int grandX=1; grandX<=4; grandX++)
{ grandChild = new DefaultMutableTreeNode("Node " + childName++);
child.add(grandChild);
}
}
// Instantiate the tree and add the selection listener.
DefaultTreeModel treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
tree.addTreeSelectionListener(this);
for (int row = 0; row < tree.getRowCount(); row++) { tree.expandRow(row); }
// Construct the frame that will be displayed.
//container buttons = getcontentpane();
Container content = getContentPane(); // JFrame default root panel.
JTabbedPane tabs = new JTabbedPane();
JPanel treeChanges = new TreeChangePanel(treeModel, tree);
JPanel CollapseNodes = new CollapseNodesPane(treeModel, tree);
JPanel CollapseTree = new CollapseTreePane(treeModel, tree);
scrollArea = new JScrollPane(tree); // Instantiate scroll area.
selectField = new JLabel("Current Selection: NONE"); // Output Label.
tabs.addTab("Modify", treeChanges);
tabs.setSelectedIndex(0);
tabs.addTab("Expand/Collapse Nodes", CollapseNodes);
tabs.addTab("Expand/Collapse Tree", CollapseTree);
// Add the scroll pane and the text field to the JFrame container.
content.add(tabs, BorderLayout.NORTH);
content.add(scrollArea, BorderLayout.CENTER);
content.add(selectField, BorderLayout.SOUTH);
setSize(500, 700); // Set the JFrame window size.
setVisible(true); // Make everything visible.
}
// Method to respond to tree selection changes.
public void valueChanged(TreeSelectionEvent event)
{ selectField.setText( "Current Selection: " +
tree.getLastSelectedPathComponent().toString());
}
}
class TreeChangePanel extends JPanel implements ActionListener
{
JButton add = new JButton("Add");
JButton remove = new JButton("Remove");
JButton change = new JButton("Change");
JTree tree = null;
DefaultTreeModel TreeModel = null;
TreePath p = null;
DefaultMutableTreeNode selection;
JLabel selectField = new JLabel("Current Selection: NONE");
int count =0;
public TreeChangePanel(DefaultTreeModel TreeModelX, JTree JTreex)
{
TreeModel = TreeModelX;
tree = JTreex;
add(add);
add(remove);
add(change);
add.addActionListener(this);
remove.addActionListener(this);
change.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
selection = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if(selection == null){ selectField.setText( "Nothing Selected");return;}
//if(selection.toString().equals("Root")){System.out.println("cannot modify root!!"); return;}
if (event.getSource()==add)
{
DefaultMutableTreeNode newN = new DefaultMutableTreeNode("Node"+ selection.getChildCount()+1);
TreeModel.insertNodeInto(newN,selection,selection.getChildCount());
p = new TreePath(newN.getPath());
tree.scrollPathToVisible(p);
}
else if(event.getSource()==remove)
{
if(selection.toString().equals("Root")){selectField.setText("cannot modify root!!");return;}
count = count - 1;
if(count < 0){count = 0;}
if(selection.isLeaf())
{
DefaultMutableTreeNode parent = (DefaultMutableTreeNode)selection.getParent();
tree.setSelectionPath(new TreePath(parent.getPath()));
TreeModel.removeNodeFromParent(selection);
}else{selectField.setText("Must select leaf.");return;}
}
else if(event.getSource()==change)
{
if(selection.toString().equals("Root")){selectField.setText("cannot modify root!!"); return;}
count =count + 1;
selection.setUserObject("ModifiedNode "+ count);
TreeModel.nodeChanged(selection);
}
}
}
class CollapseNodesPane extends JPanel implements ActionListener
{
JButton Expand = new JButton("Expand");
JButton Collapse = new JButton("Collapse");
JTree tree = null;
DefaultTreeModel TreeModel = null;
TreePath p = null;
DefaultMutableTreeNode selection;
JLabel selectField = new JLabel();
public CollapseNodesPane(DefaultTreeModel TreeModelX, JTree JTreex)
{
TreeModel = TreeModelX;
tree = JTreex;
add(Expand);
add(Collapse);
Expand.addActionListener(this);
Collapse.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
selection = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if(selection == null){selectField.setText("nothing selected");}
if(selection.toString().equals("Root")){selectField.setText("cannot modify root!!"); return;}
if (event.getSource()==Expand)
{
tree.expandPath(new TreePath(selection.getPath()));
}
else if(event.getSource()==Collapse)
{
tree.collapsePath(new TreePath(selection.getPath()));
}
}
}
class CollapseTreePane extends JPanel implements ActionListener
{
JButton Expand = new JButton("Expand");
JButton Collapse = new JButton("Collapse");
JTree tree = null;
DefaultTreeModel TreeModel = null;
TreePath p = null;
DefaultMutableTreeNode selection;
JLabel selectField = new JLabel();
public CollapseTreePane(DefaultTreeModel TreeModelX, JTree JTreex)
{
TreeModel = TreeModelX;
tree = JTreex;
add(Expand);
add(Collapse);
Expand.addActionListener(this);
Collapse.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
selection = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if(selection == null){selectField.setText("nothing selected");return;}
if(selection.toString().equals("Root"))
{
if (event.getSource()==Expand)
{
for (int row = 0; row < tree.getRowCount(); row++)
{
tree.expandRow(row);
}
}
else if(event.getSource()==Collapse)
{
for (int row = 0; row < tree.getRowCount(); row++)
{
tree.collapseRow(row);
}
}
}else{selectField.setText("select the tree root.");return; }
}
}

New Topic/Question
Reply



MultiQuote




|