001package edu.pdx.cs410J.di;
002
003import javax.swing.*;
004import javax.swing.table.DefaultTableCellRenderer;
005import javax.swing.table.TableColumn;
006import javax.swing.table.TableColumnModel;
007import java.awt.*;
008
009/**
010 * A swing table that displays the contents of a {@link CartTableModel shopping cart}
011 */
012public class CartTable extends JTable
013{
014    public CartTable( final CartTableModel model )
015    {
016        super(model);
017        TableColumnModel columns = this.getColumnModel();
018
019        for (int i = 0; i < model.getColumnCount(); i++) {
020            TableColumn column = columns.getColumn( i );
021            column.setCellRenderer( new DefaultTableCellRenderer() {
022
023                @Override
024                public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column )
025                {
026                    Component cell = super.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column );
027                    if (cell instanceof JLabel && row == model.getRowCount() - 1) {
028                        JLabel label = (JLabel) cell;
029                        Font font = label.getFont();
030                        label.setFont( font.deriveFont( Font.BOLD ) );
031                        return label;
032                    }
033                    return cell;
034                }
035            });
036        }
037    }
038}