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 an array of date objects

Create an array of Date objects of size 4. Initialize the array by using a loop. In the loop, use the Scanner.nextLine() method to input a date as a string, convert it to a date by using the toDate() method, and assign the result to an element in ..

  Class should have a method that returns the average

Write a class named TestScores. The class constructor should accept an array of test scores as its arguments. The class should have a method that returns the average of the test scores.

  Write a function which takes two string variables

Write a function which takes two string variables not C strings as inputs and compares them. If they are equal, the function should return 0. If the first-string is less than the second string, return a negative number. If the first string is grea..

  Define render a model scene in java

Render a Model Scene in Java - Imagine you are prototyping animation for a Jurassic Park Movie

  Write a class that has the subsequent fields

Write a class that has the subsequent fields: first name, last name, id number, department, payRate, hoursWork with constructors that accepts the subsequent values as arguments and assigns them to the appropriate fields.

  Cascading style sheet to a website

Compare and contrast the process of adding JavaScript and a Cascading Style Sheet to a Website. Determine if they can be used simultaneously in a page.

  Prepare an application that uses an array

Write an application that uses an Array to store 10 messages of type String. You will store this Array with 10 messages of your choosing. For example, a message could be "I love Java the programming language!"

  Write a program arrayprocessing.java

Write a program ArrayProcessing.java that reads a file name from the keyboard. The file contains integers, each on a separate line. You then create a corresponding array with integers from the lines.

  This is a client server monitoring system project

this is a client server monitoring system project design.first in the server side the 1a program is my own and i have

  Alicia creates a button using the code jbutton exit button

Alicia creates a button using the code JButton exitButton = new JButton("exit"); and places it into a JFrame named mainFrame

  Implement an iterative method with a for loop

Different scientists have developed Predator-Prey models to try to understand the interactions and foresee the evolution of the populations.

  Alter the prototype form page by javascript function

Alter the prototype form page so that when JavaScript function has verified that all the required fields have been filled, cookie is added to user's computer.

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