Consider the following Java program: class CountryTreeDemo extends JFrame { JButton addButton, removeButton, loadButton; JTextField cityTextfield, filenameTextfield;
Question:
Consider the following Java program:
class CountryTreeDemo extends JFrame {
JButton addButton, removeButton, loadButton;
JTextField cityTextfield, filenameTextfield;
JTree tree;
DefaultTreeModel tm;
DefaultMutableTreeNode root;
public CountryTreeDemo() {
root = new DefaultMutableTreeNode("Countries");
root.add(createCountryNode("New Zealand", new String[]{"Auckland","Hamilton","Christchurch","Dunedin"}));
This example creates a JTree using an instance of DefaultTreeModel, a class provided by Swing. The tree contains a root which displays a list of cities.
Define a method named createCountryNode(String countryName, String[] cities) which takes a country name and an array of cities as parameters. The method should create and return a DefaultMutableTreeNode which contains a list of cities which belongs to the parameter country. The type of each node is DefaultMutableTreeNode.
Download the given Java file and complete method. Compile and run the Java program. Submit the method in the answer box below for marking purpose.
For example:
Test | Result |
f.new MyMockListener().actionPerformed(new MockEvent(f.loadButton, ActionEvent.ACTION_PERFORMED, "countries.txt")); f.show(); | Countries New Zealand Auckland Hamilton Christchurch Dunedin Italy Rome Milan Naples Turin Japan Nagoya Toyohashi Okazaki Ichinomiya |
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.io.*;
import java.util.*;
class CountryTreeDemo extends JFrame {
JButton addButton, removeButton, loadButton;
JTextField cityTextfield, filenameTextfield;
JTree tree;
DefaultTreeModel tm;
DefaultMutableTreeNode root;
public CountryTreeDemo() {
root = new DefaultMutableTreeNode("Countries");
root.add(createCountryNode("New Zealand", new String[]{"Auckland","Hamilton","Christchurch","Dunedin"}));
tm = new DefaultTreeModel(root);
tree = new JTree(tm);
addButton = new JButton("Add");
removeButton = new JButton("Remove");
cityTextfield = new JTextField("City");
loadButton = new JButton("Load");
filenameTextfield = new JTextField("countries.txt");
addButton.addActionListener(new MyActionListener());
removeButton.addActionListener(new MyActionListener());
loadButton.addActionListener(new MyActionListener());
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(cityTextfield);
buttonsPanel.add(addButton);
buttonsPanel.add(removeButton);
buttonsPanel.add(filenameTextfield);
buttonsPanel.add(loadButton);
getContentPane().add(buttonsPanel, BorderLayout.NORTH);
getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { new CountryTreeDemo(); }
});
}
}
Continuing on from the previous question, define an inner member class named MyActionListener which implements the ActionListener interface and implements the actionPerformed method.
- When user clicks the 'Load' button, your program should read a list of cities from an input text file. The name of the text file is given by the filenameTextfield.
You should use the insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index) to insert each child node at location index in parents children.
- Get the filename from the text field.
- Create a Scanner object to read the contents in the text file. Use hasNextLine() and nextLine() to read the list.
- Read the name of the country and list of cites
- Use the split(":") method to split the country name and a list of cities
- Use the split(",") method to split the list of cities.
- Invoke the createCountryNode() defined previously to add a new node into the tree.
- The parent is the root of the tree model.
- Use tm.getChildCount(root) to get the index location.
Define an inner member class named MyActionListener which implements the ActionListener interface and implements the actionPerformed method. Download the given java file and complete the inner member class. Compile and run the Java program. Submit the inner member class in the answer box below. Click here to download the sample text file.
For example:
Test | Result |
f.filenameTextfield.setText("countries.txt"); f.new MyMockListener().actionPerformed(new MockEvent(f.loadButton, ActionEvent.ACTION_PERFORMED, "countries.txt")); f.show(); | Countries New Zealand Auckland Hamilton Christchurch Dunedin Italy Rome Milan Naples Turin Japan Nagoya Toyohashi Okazaki Ichinomiya |
sample text file:
Italy:Rome,Milan,Naples,Turin
Japan:Nagoya,Toyohashi,Okazaki,Ichinomiya
Continuing on from the previous question, add the following to the inner member class to handle the following events:
You should use the insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index) to insert a new child node at location index in parents children
You should use the removeNodeFromParent(MutableTreeNode node) to remove node from its parent node.
- When user clicks the 'Add' button, the program should add a new city into the tree model.
- Get the city name from the cityTextfield
- Use the tree.getSelectionPath() to get the selected path
- use the getLastPathComponent() to get the selected node. (i.e. for the parent parameter)
- use the tm.getChildCount(current) to get the index position. (i.e. for the index parameter)
- When user clicks the 'Remove' button, the program should remove a selected city from the tree model.
- Use the getSelectionPath() to get the selected path
- Use the getLastPathComponent() to get the selected tree node. (i.e. for the parameter node)
- Invoke the removeNodeFromParent() method to remove the selected node.
Define an inner member class named MyActionListener which implements the ActionListener interface and implements the actionPerformed method. Download the given java file and complete the inner member class. Compile and run the Java program. Submit the inner member class in the answer box below.
For example:
Test | Result |
f.new MyMockListener().actionPerformed(new MockEvent(f.loadButton, ActionEvent.ACTION_PERFORMED, "countries.txt")); f.show(); f.tree.setSelectionRow(1); f.cityTextfield.setText("Wellington"); f.new MyMockListener().actionPerformed(new MockEvent(f.addButton, ActionEvent.ACTION_PERFORMED, "Wellington")); | Countries New Zealand Auckland Hamilton Christchurch Dunedin Italy Rome Milan Naples Turin Japan Nagoya Toyohashi Okazaki Ichinomiya |
f.new MyMockListener().actionPerformed(new MockEvent(f.loadButton, ActionEvent.ACTION_PERFORMED, "countries.txt")); f.tree.setSelectionRow(2); f.new MyMockListener().actionPerformed(new MockEvent(f.removeButton, ActionEvent.ACTION_PERFORMED, "")); f.tree.setSelectionRow(1); f.cityTextfield.setText("Nelson"); f.new MyMockListener().actionPerformed(new MockEvent(f.addButton, ActionEvent.ACTION_PERFORMED, "")); f.show(); | Countries New Zealand Auckland Hamilton Christchurch Dunedin Nelson Japan Nagoya Toyohashi Okazaki Ichinomiya |
Horngrens Financial and Managerial Accounting The Financial Chapters
ISBN: 978-0134486857
6th edition
Authors: Tracie L. Miller-Nobles, Brenda L. Mattison, Ella Mae Matsumura