1 19 20 package de.gulden.util.swing; 21 22 import java.util.*; 23 import java.util.Map ; 24 import javax.swing.table.AbstractTableModel ; 25 26 32 public class MapTableModel extends AbstractTableModel { 33 34 38 41 protected Map map; 42 43 46 protected String [] columnNames; 47 48 49 53 56 public MapTableModel() { 57 super(); 58 } 59 60 63 public MapTableModel(Map map) { 64 this(map,"Entry","Value"); 65 } 66 67 70 public MapTableModel(Map map, String keyName, String valueName) { 71 this(); 72 setMap(map); 73 setColumnNames(keyName,valueName); 74 } 75 76 77 81 84 public int getRowCount() { 85 return map.size(); 86 } 87 88 91 public int getColumnCount() { 92 return 2; 93 } 94 95 98 public Object getValueAt(int row, int column) { 99 Object [] entries=map.entrySet().toArray(); 100 Map.Entry entry=(Map.Entry )entries[row]; 101 if (column==0) { 102 return entry.getKey(); 103 } else if (column==1) { return entry.getValue(); 105 } else { 106 throw new IndexOutOfBoundsException ("MapTableModel provides a 2-column table, column-index "+column+" is illegal."); 107 } 108 } 109 110 113 public String getColumnName(int column) { 114 return columnNames[column]; 115 } 116 117 120 public void setColumnNames(String keyName, String valueName) { 121 String [] names={keyName,valueName}; 122 columnNames=names; 123 } 124 125 128 public Map getMap() { 129 return map; 130 } 131 132 135 public void setMap(Map _map) { 136 map = _map; 137 } 138 139 } | Popular Tags |