Create a program using a gui interface

Assignment Help JAVA Programming
Reference no: EM13765539

java programing

Goals

  • Be able to create a program using a GUI interface.
  • Understand how to use Swing components and panels.
  • Understand the Computer Science concept of a tree.
  • Answer questions about the Java GUI interface.
  • 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.

a. Validate the operation.

Make sure that the lastSelectedPathComponent is 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.

b. Add a node to the tree.

You can instantiate DefaultMutableTreeNode objects 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.

c. 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);

d. 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.

e. Expand the entire tree.

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

f. 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)

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

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

h. 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 [email protected].

// 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());

}

}

Reference no: EM13765539

Questions Cloud

Operating at a break-even point : The short run should produce versus shut down if the total revenue that it can generate is sufficient -  Suppose that a monopolist finds itself to be operating at a break-even point - A profit-maximizing monopolist that sells all units of its output ..
Organizational structures of globalization : Use the Internet or the Strayer Library to research articles on organizations that successfully go global and adopt global information systems. Next, select one (1) organization that successfully went global and adopted global information systems
Issue related to cultural analysis : Given your understanding of the cultural similarities and differences, what would you recommend that each side do (or avoid doing) to resolve the situation?
Procedures for handling criminal matters : Procedures for handling criminal matters are often thought to be cumbersome and heavily weighted in favor of the accused. It is important to emphasize that our country was founded with the idea that a person is absolutely considered innocent until..
Create a program using a gui interface : 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.
Explain how the components of information technology systems : Explain how the components of information technology systems interrelate. Illustrate the use of information and communication technologies to solve problems as an information technology professional.
Hospice care in the us : How might cultural differences affect the experience of dying and death for a patient, his/her family, and the health care professionals who care for them?
Why did martha stewart receive jail time : What is the SEC? Why was Martha Stewart accused of insider trading? Was Martha Stewart convicted of insider trading? Why did Martha Stewart receive jail time? In the author's opinion, where did her attorney go wrong? Do you agree with the aut..
Issue of gm''s structure : Recognizing GM's current state, how do you see the new GM strategy and structure relationship? How do you see it evolving?

Reviews

Write a Review

JAVA Programming Questions & Answers

  Create a complete java application that meets these

create a complete java application that meets these requirements1. the program has an opening comments section using

  Method splitstack that takes a stack

Write a method splitStack that takes a stack of integers as a parameter and splits it into negatives and non-negatives. The numbers in the stack should be rearranged so that all the negatives appear on the bottom of the stack and all the non-negat..

  Minimal spanning tree decreasing edge

minimal spanning tree decreasing edge dismissalreverse-delete algorithm. develop an implementation that computes the

  Write a java statement to have the object from manage

Write a Java statement to have the object from manage its display area by dividing it into 3 rows which have 4 columns each.

  Using the java development kit and jgrasp

Using the Java Development Kit and jGrasp, develop a program in response to the following prompt:

  Calculates the total annual compensation of a salesperson

Write a Java application using NetBeans Integrated Development Environment IDE that calculates the total annual compensation of a salesperson.

  Developing a java application

You have been requested to develop a Java application for the local library as part of an upgrade initiative. As in all libraries, this is a place where one can check out books

  Write a program called product1ton

Write a program called Product1ToN to compute the product of integers 1 to 10 (i.e., 1×2×3×...×10). Try computing the product from 1 to 11, 1 to 12, 1 to 13 and 1 to 14.

  Calculate the cross product of two numeric vectors function

Create a function that calculate the cross product of two numeric vectors Function Name: calcCrossProduct

  Create an application that will allow a new customer

Create an application that will allow a new customer to order a house. You'll allow the customer to choose among four models (Aspen, Britattany, Colonial, and Dartmoor) by creating separate ButtonGroups.

  The license plate game and csv parsing

Resulting method will correctly estimate that there are two syllables in quokka and two syllables in springbok, though it does get the number of syllables

  Difference for class linked bag and test into main program

write a method difference for the class linked bag and test into main program.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd