自定义 Table 模型不工作
Custom Table Model not working
我写了一个自定义 table 模型。它的目标是将值绑定到列表。该列表在整个应用程序中使用。如果在 table 中进行了更改,该更改也会反映在应用程序中的其他任何地方。我的问题是,我的 table JTable 不显示行。
这是我的第一个自定义 table 模型,所以我不确定是否遗漏了什么。
table 使用此代码创建:
public CBTable(List<T> elements, Class<T> classType) {
super(new BindingTableModel(elements));
this.tableModel = (BindingTableModel<T>) getModel();
this.classType = classType;
setBindings();
repaint();
}
setBindings 的一个例子是这样的:
@Override
protected void setBindings() {
addBinding(ProfessionalDelegate.DELEGAGE_PROPERTY_TYPE,
"Type",
String.class,
false);
addBinding(ProfessionalDelegate.ENTITY_PROPERTY_NAME,
"Name",
String.class,
true);
...
实际模型本身是这样的:
public class BindingTableModel<T> extends AbstractTableModel implements TableModel {
private List<T> elements;
private List<BindingTableModelInfo> bindingInfo;
private Map<Point, Object> valueMap;
public BindingTableModel(List<T> elements) {
this.elements = elements;
this.bindingInfo = new ArrayList<>();
valueMap = new HashMap<>();
bind();
for(int i = 0; i < getRowCount(); i++){
fireTableRowsInserted(i, i);
}
fireTableStructureChanged();
}
public void addBindingInfo(String propertyName, String columnName, Class colClass, boolean isEditable) {
bindingInfo.add(new BindingTableModelInfo(propertyName,
columnName,
colClass,
isEditable));
}
private void bind() {
for (int col = 0; col < getColumnCount(); col++) {
for (int row = 0; row < getRowCount(); row++) {
getValueAt(row, col);//this will init the map
}
}
}
public void addElement(T element){
elements.add(element);
}
public List<T> getElements(){
return elements;
}
public void removeElement(T element){
int index = elements.indexOf(element);
elements.remove(element);
fireTableRowsDeleted(index, index);
}
@Override
public int getRowCount() {
return elements.size();
}
@Override
public int getColumnCount() {
return bindingInfo.size();
}
@Override
public String getColumnName(int i) {
return bindingInfo.get(i).getColumnName();
}
@Override
public Class<?> getColumnClass(int i) {
return bindingInfo.get(i).getColumnClass();
}
@Override
public boolean isCellEditable(int row, int col) {
return bindingInfo.get(col).isIsEditable();
}
@Override
public Object getValueAt(int row, int col) {
Point point = new Point(row, col);
if (valueMap.containsKey(point)) {
return valueMap.get(point);
}
String property = bindingInfo.get(col).getProperty();
Class classType = bindingInfo.get(col).getColumnClass();
T element = elements.get(row);
Object retVal = Pojo.getValueByName(classType, element, property);
valueMap.put(point, retVal);
return retVal;
}
@Override
public void setValueAt(Object value, int row, int col) {
Point point = new Point(row, col);
if (valueMap.containsKey(point)) {
Object oldValue = valueMap.get(point);
if (oldValue != null && oldValue.equals(value)) {
return;
}
}
String property = bindingInfo.get(col).getProperty();
Class classType = bindingInfo.get(col).getColumnClass();
T element = elements.get(row);
Pojo.setValueByName(classType, element, property, value);
}
}
一定要fireTableCellUpdated()
或其他合适的活动;这通常在 setValueAt()
:
的实现中完成
@Override
public void setValueAt(Object value, int row, int col) {
…
fireTableCellUpdated(row, col);
}
已查看完整示例here and here。
我写了一个自定义 table 模型。它的目标是将值绑定到列表。该列表在整个应用程序中使用。如果在 table 中进行了更改,该更改也会反映在应用程序中的其他任何地方。我的问题是,我的 table JTable 不显示行。
这是我的第一个自定义 table 模型,所以我不确定是否遗漏了什么。
table 使用此代码创建:
public CBTable(List<T> elements, Class<T> classType) {
super(new BindingTableModel(elements));
this.tableModel = (BindingTableModel<T>) getModel();
this.classType = classType;
setBindings();
repaint();
}
setBindings 的一个例子是这样的:
@Override
protected void setBindings() {
addBinding(ProfessionalDelegate.DELEGAGE_PROPERTY_TYPE,
"Type",
String.class,
false);
addBinding(ProfessionalDelegate.ENTITY_PROPERTY_NAME,
"Name",
String.class,
true);
...
实际模型本身是这样的:
public class BindingTableModel<T> extends AbstractTableModel implements TableModel {
private List<T> elements;
private List<BindingTableModelInfo> bindingInfo;
private Map<Point, Object> valueMap;
public BindingTableModel(List<T> elements) {
this.elements = elements;
this.bindingInfo = new ArrayList<>();
valueMap = new HashMap<>();
bind();
for(int i = 0; i < getRowCount(); i++){
fireTableRowsInserted(i, i);
}
fireTableStructureChanged();
}
public void addBindingInfo(String propertyName, String columnName, Class colClass, boolean isEditable) {
bindingInfo.add(new BindingTableModelInfo(propertyName,
columnName,
colClass,
isEditable));
}
private void bind() {
for (int col = 0; col < getColumnCount(); col++) {
for (int row = 0; row < getRowCount(); row++) {
getValueAt(row, col);//this will init the map
}
}
}
public void addElement(T element){
elements.add(element);
}
public List<T> getElements(){
return elements;
}
public void removeElement(T element){
int index = elements.indexOf(element);
elements.remove(element);
fireTableRowsDeleted(index, index);
}
@Override
public int getRowCount() {
return elements.size();
}
@Override
public int getColumnCount() {
return bindingInfo.size();
}
@Override
public String getColumnName(int i) {
return bindingInfo.get(i).getColumnName();
}
@Override
public Class<?> getColumnClass(int i) {
return bindingInfo.get(i).getColumnClass();
}
@Override
public boolean isCellEditable(int row, int col) {
return bindingInfo.get(col).isIsEditable();
}
@Override
public Object getValueAt(int row, int col) {
Point point = new Point(row, col);
if (valueMap.containsKey(point)) {
return valueMap.get(point);
}
String property = bindingInfo.get(col).getProperty();
Class classType = bindingInfo.get(col).getColumnClass();
T element = elements.get(row);
Object retVal = Pojo.getValueByName(classType, element, property);
valueMap.put(point, retVal);
return retVal;
}
@Override
public void setValueAt(Object value, int row, int col) {
Point point = new Point(row, col);
if (valueMap.containsKey(point)) {
Object oldValue = valueMap.get(point);
if (oldValue != null && oldValue.equals(value)) {
return;
}
}
String property = bindingInfo.get(col).getProperty();
Class classType = bindingInfo.get(col).getColumnClass();
T element = elements.get(row);
Pojo.setValueByName(classType, element, property, value);
}
}
一定要fireTableCellUpdated()
或其他合适的活动;这通常在 setValueAt()
:
@Override
public void setValueAt(Object value, int row, int col) {
…
fireTableCellUpdated(row, col);
}
已查看完整示例here and here。