Java Reference
In-Depth Information
the price per piece, the quantity ordered and the price for the ordered quan-
tity. Initially, no products are ordered, i.e. the quantities are 0. The user can
edit the quantities. As a result the price for the ordered quantity in the last col-
umn is updated. Our order form has an additional fourth row where the total
price for all ordered products is displayed and updated with every change of the
order.
The table is implemented by our table model OrderTableModel . This model
is then passed to a JTable which displays it. We organize the data as follows:
the first three rows contain the product information. The first column (column
number 0) contains the product names as String s. The next column contains
the pictures ImageIcons . The following column contains the price per piece as
double . The fourth column (number 3) contains the number of pieces ordered
as int . This column is editable: the user can change the quantities. In the last
column the total price for every item is listed as double . The values of these cells
are computed as (price per piece) times (quantity ordered). The last cell of the
fourth row contains the total amount of the order.
The values are computed in method getValueAt which determines the values
by a large case distinction (switch statement). In particular, the first three rows
containing the three products are treated differently from the last row contain-
ing the total amount. One has to make sure that a re-computation is performed
when the user changes the quantity ordered. This is achieved by using method
fireTableDataChanged() of class AbstractTableModel .Itnot only initiates a
re-computation but also an update of the display of the associated JTable .InJava
the so-called fire method of a class informs the listeners which are associated to
that class. We have not explicitly defined a listener in our application. However,
using a table with table model automatically implements listeners for the basic
functions. These are responsible for the re-computation of the cell values and the
update of the display.
We now list the classes OrderTableModel and OrderTableFrame and show a
screen shot of the application in Figure 16.5.
Figure 16.5 OrderTableFrame after some orders have been placed
Search WWH ::




Custom Search