这是我们用于表格模型的代码。你可以使用它。如您所见,它非常简单干净。
public abstract class BeanTableModel<B> extends ArrayList<B>
implements TableModel {
private List<TableModelListener> listeners = new ArrayList();
private TableModelAttributeList attributeList;
public BeanTableModel() {
super();
}
public BeanTableModel(Object[][] attributes) {
super();
this.attributeList = new TableModelAttributeList(attributes) {
};
}
public TableModelAttributeList getAttributeList() {
return attributeList;
}
public final void setAttributeList(TableModelAttributeList attributeList) {
this.attributeList = attributeList;
}
@Override
public final int getRowCount() {
return size();
}
@Override
public final int getColumnCount() {
if (attributeList != null) {
return attributeList.size();
} else {
return 0;
}
}
@Override
public final String getColumnName(int columnIndex) {
if (attributeList != null) {
return attributeList.getColumnName(columnIndex);
} else {
return null;
}
}
@Override
public final Class<?> getColumnClass(int columnIndex) {
if (attributeList != null) {
return attributeList.getColumnClass(columnIndex);
} else {
return Object.class;
}
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
@Override
public final Object getValueAt(int rowIndex, int columnIndex) {
try {
if (rowIndex >= 0) {
return (attributeList != null) ? attributeList.getValue(this.get(
rowIndex), columnIndex) : null;
} else {
return get(rowIndex);
}
} catch (Exception ex) {
Logger.getLogger(BeanTableModel.class.getName()).log(Level.SEVERE,
null, ex);
return null;
}
}
@Override
public final boolean add(B e) {
boolean result = super.add(e);
if (result) {
fireListeners();
}
return result;
}
@Override
public final boolean addAll(Collection<? extends B> c) {
boolean result = super.addAll(c);
if (result) {
fireListeners();
}
return result;
}
public final void change(Collection<? extends B> c) {
super.clear();
super.addAll(c);
fireListeners();
}
@Override
public final boolean addAll(int index, Collection<? extends B> c) {
boolean result = super.addAll(index, c);
if (result) {
fireListeners();
}
return result;
}
@Override
public final void clear() {
super.clear();
fireListeners();
}
@Override
public final boolean remove(Object o) {
boolean result = super.remove(o);
if (result) {
fireListeners();
}
return result;
}
@Override
public final boolean removeAll(Collection<?> c) {
boolean result = super.removeAll(c);
if (result) {
fireListeners();
}
return result;
}
@Override
protected final void removeRange(int fromIndex, int toIndex) {
super.removeRange(fromIndex, toIndex);
fireListeners();
}
@Override
public final boolean retainAll(Collection<?> c) {
boolean result = super.retainAll(c);
if (result) {
fireListeners();
}
return result;
}
@Override
public final B remove(int index) {
B b = super.remove(index);
fireListeners();
return b;
}
@Override
public final void add(int index, B element) {
super.add(index, element);
fireListeners();
}
@Override
public final B set(int index, B element) {
B b = super.set(index, element);
fireListeners();
return b;
}
@Override
public final void trimToSize() {
super.trimToSize();
fireListeners();
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
//Do Nothing
}
@Override
public final void addTableModelListener(TableModelListener l) {
listeners.add(l);
}
@Override
public final void removeTableModelListener(TableModelListener l) {
listeners.remove(l);
}
public final void fireListeners() {
TableModelEvent evt = new TableModelEvent(this);
for (TableModelListener listener : listeners) {
listener.tableChanged(evt);
}
}
public final ArrayList<B> serialize() {
return new ArrayList<B>(this);
}
}
这是针对格式及其解释方式:
public abstract class TableModelAttributeList {
public static final int COLUMN_NAME = 0;
public static final int CLASS = 1;
public static final int ATTRIBUTE_NAME = 2;
public static final int FORMAT = 3;
public static final int REPLACE = 4;
private final Object[][] attributes;
private final Pattern[] patterns;
public TableModelAttributeList(Object[][] attributes) {
this.attributes = attributes;
patterns = new Pattern[attributes.length];
for (int i = 0, size = attributes.length; i < size; i++) {
if (attributes[i][REPLACE] != null) {
patterns[i] = Pattern.compile((String) attributes[i][FORMAT]);
}
}
}
public final String getColumnName(int column) {
return (String) attributes[column][COLUMN_NAME];
}
public final Class<?> getColumnClass(int column) {
return (Class<?>) attributes[column][CLASS];
}
public final int size() {
return attributes.length;
}
public final Object getValue(Object bean, int column) throws
Exception {
try {
Object value = null;
if (attributes[column][ATTRIBUTE_NAME] == null) {
return "";
}
if (attributes[column][ATTRIBUTE_NAME].equals("this")) {
return bean;
} else {
try {
value = PropertyUtils.getProperty(bean,
(String) attributes[column][ATTRIBUTE_NAME]);
} catch (NullPointerException ex) {
return "";
} catch (NestedNullException ex) {
return "";
}
}
if (value == null) {
return null;
} else if (patterns[column] != null) {
Matcher matcher = patterns[column].matcher(String.valueOf(value));
if (matcher.find()) {
return matcher.replaceAll(
(String) attributes[column][REPLACE]);
} else {
return null;
}
} else if (value instanceof Date) {
if (attributes[column][FORMAT] != null) {
DateFormat format = new SimpleDateFormat(
(String) attributes[column][FORMAT]);
return format.format((Date) value);
} else {
return value;
}
} else if (attributes[column][FORMAT] != null) {
return String.format(
(String) attributes[column][FORMAT], value);
} else {
return value;
}
} catch (IllegalAccessException ex) {
String message = "The property named '"
+ (String) attributes[column][ATTRIBUTE_NAME]
+ "' has a private getter method";
throw new Exception(message, ex);
} catch (InvocationTargetException ex) {
String message = "An exception has ocurred while retrieving value for bean property '"
+ (String) attributes[column][ATTRIBUTE_NAME] + "'";
throw new Exception(message, ex);
} catch (NoSuchMethodException ex) {
String message = "The bean doesn't have a property named '"
+ (String) attributes[column][ATTRIBUTE_NAME] + "'";
throw new InvalidModelAttributesException(message, ex);
}
}
}
这是应该如何创建表模型的:
public class SelectEmployeeTableModel extends BeanTableModel<Empleados> {
public static final String NOMBRE = "Nombre";
public static final String DEPARTAMENTO = "Departamento";
public static final String FECHA_BAJA = "FechaBaja";
public SelectEmployeeTableModel() {
super(new Object[][]{
{NOMBRE , Object.class, "nombreCompleto" , null, null},
{DEPARTAMENTO , Object.class, "departamentoId.nombre" , null, null},
{FECHA_BAJA , Object.class, "fechaBaja" , null, null}
});
}
}
第一列是可见的列名,第二列是数据的类型(如果要显示复选框,请将其替换为 Boolean.class),第三列是不带“get”前缀的名称,因为它使用反射,第四个是格式(对于一个日期,它可以是“dd/MM/yyyy”或“$%.2f”对于十进制数),第五个是用正则表达式替换。
然后在要使用它的面板中执行以下操作:
jtable.setModel(new SelectEmployeeTableModel());
如果您需要更改表模型中的数据
model.change(collectionName);
如果你只是出于某种原因想要刷新,你可以
model.fireListeners();
渲染器从未失败过。至少你没有在实体中设置吸气剂。
如果你有任何疑问,不要害怕问!祝你有美好的一天。