1 package sample.jms.StockExample; 2 3 52 import javax.swing.table.*; 53 54 import java.util.ArrayList ; 55 import java.util.HashMap ; 56 66 public class StocksTable extends AbstractTableModel { 67 70 private static final long serialVersionUID = 2183680672901551040L; 71 private String [] columnNames = {"Company","Value"}; 72 protected static int NUM_COLUMNS = 2; 73 protected static int START_NUM_ROWS = 0; 74 protected int nextEmptyRow = 0; 75 protected int numRows = 0; 76 77 static final public String COMPANY = "Company"; 78 static final public String VALUE = "Value"; 79 private ArrayList companies; 81 private HashMap companiesToValue; 83 84 public StocksTable() { 85 companies = new ArrayList (); 87 companiesToValue = new HashMap (); 88 } 89 90 93 public String getColumnName(int column) { 94 return columnNames[column]; 95 } 96 97 100 public synchronized int getColumnCount() { 101 return NUM_COLUMNS; 102 } 103 104 107 public synchronized int getRowCount() { 108 if (numRows < START_NUM_ROWS) { 109 return START_NUM_ROWS; 110 } else { 111 return numRows; 112 } 113 } 114 115 118 public synchronized Object getValueAt(int row, int column) { 119 try { 120 switch (column) { 121 case 0: 123 return companies.get(row); 124 case 1: 126 return companiesToValue.get(companies.get(row)); 127 } 128 } catch (Exception e) { 129 } 130 return "N/A"; 131 } 133 138 public synchronized void updateCompany(String companyName, String value) { 139 boolean addedRow = !companies.contains(companyName) ; 141 companiesToValue.put(companyName, value); 142 if (addedRow) { 144 numRows++; 145 companies.add(companyName); 146 fireTableRowsInserted(companies.size(), companies.size()); 147 148 } else { 149 fireTableRowsUpdated(companies.indexOf(companyName), companies.indexOf(companyName)); 150 } 151 } 153 } | Popular Tags |