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

  Design an object hierarchy

Design an object hierarchy and design your own UML class diagram to reflect the classes and relationship required by your program.

  Please write the code in java

Please write the code in java for  Recursion,  Sorting and Searching

  Write a functions that takes an array of doubles

1. Write a function that is passed a single integer and then calculates the factorial of that number. A factorial is the product of the integer times all of the integers below it stopping at 1. So n!= n*(n-1)*(n-2).......3.2.1

  Write the definition of the class inventory

Write the definition of the class Inventory such that object of this class can store an item's id, name, number of pieces in stock, manufacturer's price, and selling price. The class should include constructors, setters, getters, and toString meth..

  1design an abstract data type in java that represents a

1.design an abstract data type in java that represents a musical pitch noteadt.java. the adt should store the note or

  Design a class named magazinesubscription

Design a class named MagazineSubscription that has fields for a subscriber's name, the magazine name, and number of months remaining in the subscription.

  Create a constructor in the subclass why

Assume there is a base class with multiple constructors, if the subclass inherits from the base class, do we need to create a constructor in the subclass? Why?

  Algorithm analysis with advanced data structures

Write a program to help the merchants devise a sequence of transmutations that would change silver into gold with the smallest possible total fee paid to the alchemists.

  Write down java console application to add new student

Write down java console application presents the following menu. Add new student and scores in Biology, Physics and chemistry Search for a given student.

  Part - a1nbsp to compile and run a java source file

part - a1.nbsp to compile and run a java source file hello.java that contains a main method which of the following are

  Create an application that provides a solution

Create an application that provides a solution for problem 20.8 In addition to requirements specified in the description.

  Personalize the time zone application of section 24.3

Personalize the time zone application of Section 24.3. Prompt the user to log in and specify a city to be stored in a profile. The next time the user logs in, the time of their favorite city is displayed automatically.

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