35.9. Associating Properties with Input Parameters

 
[Page 1086 ( continued )]

Programming Exercises

Sections 31.2 “31.7

31.1* ( Creating a table for a loan schedule ) Exercise 26.5 displays an amortization schedule in a text area. Write a program that enables the user to enter or choose the loan amount, number of years , and interest rate from spinners and displays the schedule in a table, as shown in Figure 31.29.
[Page 1087]
Figure 31.29. The table shows the loan schedule.

31.2* ( Deleting rows and columns ) Listing 31.5, TableModelDemo.java, allows you to delete only the first selected row or column. Enable the program to delete all the selected rows or columns. Also enable the program to delete a row or a column by pressing the DELETE key.
31.3** ( Creating a student table ) Create a table for student records. Each record consists of name, birthday, class status, in-state, and a photo, as shown in Figure 31.30. The name is of the String type; birthday is of the Date type; class status is one of the following five values: Freshman, Sophomore, Junior, Senior, or Graduate; in-state is a boolean value indicating whether the student is a resident of the state; and photo is an image icon. Use the default editors for name, birthday, and in-state. Supply a combo box as custom editor for class status.
Figure 31.30. The table displays student records and supports add, remove, and edit operations.


31.4* ( Displaying a table for data from a text file ) Suppose that a table named Exercise31_4Table.txt is stored in a text file. The first line in the file is the header, and the remaining lines correspond to rows in the table. The elements are separated by commas. Write a program to display the table using the JTable component. For example, the following text file is displayed in a table, as shown in Figure 31.31:

[Page 1088]
  
[Page 1088]
Country, Capitol, Population, Democracy USA, Washington DC, 280, true Canada, Ottawa, 32, true United Kingdom, London, 60, true Germany, Berlin, 83, true France, Paris, 60, true Norway, Oslo, 4.5, true India, New Deli, 1046, true

Figure 31.31. The data in the file is displayed in a JTable .


31.5*** ( Creating a controller using JTable ) In Exercise 30.1, you created a chart model ( ChartModel ) and two views ( PieChart and BarChart ). Create a controller that enables the user to modify the data, as shown in Figure 31.32. You will see the changes take effect in the pie chart view and the bar chart view. Your exercise consists of the following classes:
  • The controller named Exercise31_5ChartController . This class uses a table to display data. You can modify the data in the table. Click the Insert button to insert a new row above the selected row in the table, click the Delete button to delete the selected row in the table, and click the Update button to update the changes you made in the table.

  • The class MyTableModel . This class extends DefaultTableModel to override the getColumnClass method so that you can use the JTable 's default editor for numerical values. This class is same as in Listing 31.5.

  • The classes ChartModel , PieChart , and BarChart from Exercise 28.1.

  • The main class Exercise31_5 . This class creates a user interface with a controller and two buttons , View Pie Chart and View Bar Chart . Click the View Pie Chart button to pop up a frame to display a pie chart, and click the View Bar Chart button to pop up a frame to display a bar chart.

Figure 31.32. You can modify the data in the controller. The views are synchronized with the controller.


[Page 1089]

Sections 31.8 “31.14

31.6* ( Creating a tree for book chapters ) Create a tree to display the table of contents for a book. When a node is selected in the tree, display a paragraph to describe the selected node, as shown in Figure 31.33.
Figure 31.33. The content of the node is displayed in a text area when the node is clicked.

31.7** ( Adding and deleting tree nodes using the INSERT and DELETE keys ) Modify Listing 31.15, ModifyTree.java, to add a new child node by pressing the INSERT key, and delete a node by pressing the DELETE key.
31.8* ( Traversing trees ) Create a tree using the default JTree constructor and traverse the nodes in breadth-first, depth-first, preorder, and postorder.
31.9* ( Storing and restoring trees ) Modify Listing 31.15, ModifyTree.java, to add two buttons, as shown in Figure 31.34 to store and restore trees. Use the object I/O to store the tree model.
Figure 31.34. You can store tree data to a file and restore it later.

 


[Page 1090]
 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net