package presentation; import javax.swing.*; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.AbstractTableModel; import javax.swing.table.TableColumn; import javax.swing.border.*; import java.util.*; import java.awt.Dimension; import java.awt.Color; /*********************************************************************** * Class: Table * Description: This class is responsible for wrapping the * functionality of the JTable class into an easier to use class. * The column level manipulations are handled by this class which * would normally have to be handled by the user of a JTable * directly. The TableModel that holds the rows and columns is also * created in this class and therefore abstracted from the programmer. * @Author Cynthia E. Pilling ***********************************************************************/ public class Table extends JTable { //The TableModel that will hold the rows & columns private LocalTableModel tableModel = new LocalTableModel();; //The ScrollPanel that the Table will be placed on private JScrollPane scrollPane; /** * Constructor * @param Vector columns the column headers for the Table */ public Table(Vector cols) { super(); tableModel.setColumns(cols); tableModel.setRows(new Vector[] {}); this.setModel(tableModel); } /** * Constructor * @param Vector columns the column headers for the Table * @param Vector rows the data to be displayed in the Table */ public Table(Vector columns, Vector[] rows) { this(columns, rows, false); } /** * Constructor * @param Vector columns the column headers for the Table * @param Vector rows the data to be displayed in the Table * @param boolean editable the flag that determines whether * or not a Table is editable or not. */ public Table(Vector columns, Vector[] rows, boolean editable) { super(); tableModel.setRows(rows); tableModel.setColumns(columns); tableModel.setEditFlag(editable); this.setModel(tableModel); } /** * setRowData is responsible for accepting the rows for the * Table. * @param Vector[] rows */ public void setRowData(Vector[] rows) { tableModel.setRows(rows); } /** * setColumnData is responsible for accepting the column headers * for the Table. * @param Vector cols */ public void setColumnData(Vector cols) { tableModel.setColumns(cols); } /** * setTableEditable will set the TableModel's editable property * to true or false. * @param boolean editable the flag to toggle the TableModel between * editable and read-only. */ public void setTableEditable(boolean editable) { tableModel.setEditFlag(editable); } /** * setTableSize accepts the width & height for the Table display size. * @param int width the value for the Table width * @param int height the value for the Table height */ public void setTableSize(int width, int height) { scrollPane.setPreferredSize(new Dimension(width, height)); } /** * setColumnToolTip will set a String as the ToolTip for a Column * @param String col the column the ToolTip is for * @param String tip the phrase for the ToolTip */ public void setColumnToolTip(String col, String tip) { TableColumn aCol = this.getColumn(col); javax.swing.table.DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(); renderer.setToolTipText(tip); aCol.setCellRenderer(renderer); } /** * setCellColor will set a Color for a Column * @param String col the column to set the color for * @param Color color the color for the Column */ public void setCellColor(String col, Color color) { TableColumn tc = this.getColumn(col); DefaultTableCellRenderer defaultCellRend = new DefaultTableCellRenderer(); defaultCellRend.setBackground(color); tc.setCellRenderer(defaultCellRend); } /** * setCellEditor will add an editing component for a Column * @param String col the column that will have the edit component * @param JTextField textField the JTextField that is used to edit * the column */ public void setCellEditor(String col, JTextField textField) { TableColumn tc = this.getColumn(col); tc.setCellEditor(new DefaultCellEditor(textField)); } /** * setCellEditor will add an editing component for a Column * @param String col the column that will have the edit component * @param JComboBox comboBox the JComboBox that is used to edit * the column */ public void setCellEditor(String col, JComboBox comboBox) { TableColumn tc = this.getColumn(col); tc.setCellEditor(new DefaultCellEditor(comboBox)); } /** * setCellEditor will add an editing component for a Column * @param String col the column that will have the edit component * @param JCheckBox checkBox the JCheckBox that is used to edit * the column */ public void setCellEditor(String col, JCheckBox checkBox) { TableColumn tc = this.getColumn(col); tc.setCellEditor(new DefaultCellEditor(checkBox)); } /** * setColumnWidth will set the width for the Column specified. * @param Sring col the column to be set with the new width * @param int width the width value to be set */ public void setColumnWidth(String col, int width) { System.out.println("Inside set ColumnWidth"); TableColumn tc = this.getColumn(col); System.out.println("TC "+tc); tc.setMaxWidth(width); } /** * setValueAt accepts a new Object value for the Table Model within the Table * @param Object aObject the data for the cell * @param int row the row position of the cell * @param int col the column position of the cell */ public void setValueAt(Object aObject, int row, int col) { tableModel.setValueAt(aObject, row, col); } /** * setData accepts a new Vector array value for the Table Model within the Table * @param Vector[] aVector */ public void setValueAt(Vector[] aVec) { //tableModel.setValueAt(aObject, row, col); } /** * getScrollPane returns the JScrollPane that the Table is placed on. * @returns JScrollPane scrollPane */ public JScrollPane getScrollPane() { return scrollPane = this.createScrollPaneForTable(this); } } //This is the class definition for the TableModel that the //Table class will use to hold the data being passed in. class LocalTableModel extends AbstractTableModel { private boolean editFlag; private String[] names; private Object[][] data; private int vecSize = 0, vecLength = 0; //setColumns accepts the Vector of column headers from //the Table class and stores them in this model public void setColumns(Vector inNames) { Enumeration nameEnum = inNames.elements(); int i = 0; names = new String[inNames.size()]; while (nameEnum.hasMoreElements()) { names[i] = nameEnum.nextElement().toString(); i++; } } //setRows accepts the Vector array of data rows from //the Table class and stores them in this model public void setRows(Vector[] inData) { vecLength = inData.length; for (int j=0; j vecSize) { vecSize = vec.size(); } } data = new Object[vecLength][vecSize]; int i = 0; for (i=0; i