java programing new lab

| May 26, 2015

Goals

  1. Be able to create a program using a GUI interface.
  2. Understand how to use Swing components and panels.
  3. Understand the Computer Science concept of a tree.
  4. Answer questions about the Java GUI interface.
  5. Practice using Swing JTabbedPane and JTree components.

 

 

Description       

1)   In your lab5 folder, compile the file that is a starting point for this assignment. The listing and image of this Java class is on the bottom of this lab description. You can create the programs by cutting and pasting. Practice using the resulting program until you understand the concept of a displayable tree. Check what happens when you single click on any node and double click on non-leaf nodes of the displayed tree. Study the program until you completely understand what it does.

2)   Create three JPanels with a horizontal box layout. The first will contain three buttons (Add, Change, and Remove) that will be used to modify the structure of the tree; the second will contain two buttons (Expand and Collapse) to expand and collapse the entire tree; the third will also contain two buttons (Expand and Collapse) to expand and collapse a single path on the tree. Add horizontal glue to the right of the button panels, so the buttons will be packed to the left.

3)   Attach the program’s action listener to each of the buttons (ex: addButton.addActionLister(this)). Remember that using the keyword this means that we must provide an actionPerformed method in the class that we are modifying. You can refer to the examples in the text to see how this is done. The phrase, implements ActionListener, must also be on the class signature line.

4)   Instantiate a Swing JTabbedPane(). Add the three panels created in the above step to this component (ex: tabs.addTab(“Modify”, modifyPanel). Add theJTabbedPane to the North portion of JFrame container provided in the original program. Indicate with the statement tabs.setSelectedIndex(0) that the first panel is to be displayed when the program begins to execute.

5)   The instantiation of the JTree will need to be slightly changed to complete this lab. We have to work with a class called DefaultTreeModel that controls how the tree is shown on the display as changes are made. Without utilizing this class, modifications to the tree will not show.

Declare treeModel with the statement: DefaultTreeModel treeModel;

The statements to instantiate the initial tree are:

treeModel = new DefaultTreeModel(root);

tree      = new JTree(treeModel);

Instead of: tree = new JTree(root);

6)   We will now consider each of the options that we need to add to the program. The appropriate logic is to be inserted into the actionPerformed method.

  1. Validate the operation.

    Make sure that the lastSelectedPathComponentis not null when processing add, change, remove, expand path, and collapse path options. The following statement gets the last selected path component.

    DefaultMutableTreeNode selection =

    (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();

    If no node has been selected, place an appropriate message in the label field. The remove option requires additional validation. Users should not be able to remove non-root leaf nodes. The statement selection.isLeaf() returns true if selection specifies a leaf node (assuming ‘selection’ is the name you give to the variable holding the last selected component.

  2. Add a node to the tree.

    You can instantiate DefaultMutableTreeNodeobjects in the same way as done in the original program. The variable, childName, sequentially numbers the nodes. You can number nodes that users add to add to the tree in the same way.

    The add method used in the original program won’t work correctly for dynamically changing the tree; The treeModel object won’t correctly display them. Instead, use the insertNodeInto method of the TreeModel class to add a node. The following is an example of using this method, assuming that newN is the identifier you use for naming the new node and selection is the identifier for the last selected path component.

    treeModel.insertNodeInto(newN,selection,selection.getChildCount());

    After adding the new node, you need to make the new node visible. Assuming the newly instantiated node is newNode, the statement follows.

    tree.scrollPathToVisible(new TreePath(newNode.getPath));

    Finally, make sure to call the setSelectionPath() method so the new node is now selected, and update the text shown in the label.

  3. Remove a node from the tree.

    Remember that we only allow users to remove non-root leaf nodes. After removing a node, we want to have the parent node to become the last selected node and update the label with an appropriate error message. Use the statement to get the parent:

    parent = (DefaultMutableTreeNode)selection.getParent();

    To cause the parent node to highlight, use the statement:

    tree.setSelectionPath(new TreePath(parent.getPath()));

    The statement to remove a node from the tree is:

    treeModel.removeNodeFromParent(selection);

  4. Change the label that identifies a node on the tree.

    The statement to change the label of a node is:

    selected.setUserObject(“new identifier”);

    You’ll also need the statement treeModel.nodeChanged(selected); to force the modified node to display correctly. Make sure that each time you change a node, you give it a unique new identifier name.

  5. Expand the entire tree.

    You can use the loop in the original program to implement this option.

  6. Collapse the entire tree.

    The logic is very similar to that of expand tree except you need to count down instead of up. Use the method tree.collapseRow(row)instead of tree.expandRow(row).

  7. Expand the path after a selected node of the tree.

    The statement that will expand the path after selectedis:    tree.expandPath(new TreePath(selected.getPath());

  8. Collapse the path after a selected node of the tree.

    The statement that will collapse the path after selected is

    tree.collapsePath(new TreePath(selected.getPath());

 

12)  Answer the synthesis questions in an rtf or doc file (answers.rtf or answers.doc). Type your name and the lab number on this file and include the questions with the answers.

13)  Zip your Eclipse project along with the synthesis answers and email to harveyd@sou.edu.

 

 

// Trees.java

// Program to demonstrate implementation of trees.

// Provides the framework for cs258 lab number 5.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.tree.*;

import javax.swing.event.*;

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.

tree = new JTree(root);

tree.addTreeSelectionListener(this);

for (int row = 0; row < tree.getRowCount(); row++) { tree.expandRow(row); }

// Construct the frame that will be displayed.

Container content = getContentPane();         // JFrame default root panel.

scrollArea        = new JScrollPane(tree);    // Instantiate scroll area.

selectField       = new JLabel(“Current Selection: NONE”); // Output Label.

// Add the scroll pane and the text field to the JFrame container.

content.add(scrollArea, BorderLayout.CENTER);

content.add(selectField, BorderLayout.SOUTH);

setSize(250, 275);  // 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());

}

}

Get a 5 % discount on an order above $ 150
Use the following coupon code :
2018DISC
Computer Ethics in the Workplace
Data Privacy and Security

Category: Computer Science

Our Services:
Order a customized paper today!