java属性编辑器实现
效果演示:
主要代码:
public WPropertyEditor() {
super(model);
model.addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
int iCol = e.getColumn();
int iRow = e.getFirstRow();
if (iCol > -1 && iRow > -1) {
PropertyDescriptor propertyDescriptor = WPropertyEditor.this.props.get(iRow);
Method writeMethod = propertyDescriptor.getWriteMethod();
Object oValue = model.getValueAt(iRow, iCol);
try {
writeMethod.invoke(widget, oValue);
} catch (IllegalAccessException | InvocationTargetException ex) {
ex.printStackTrace();
}
}
}
});
this.setRowHeight(24);
TableColumnModel columnModel = this.getColumnModel();
columnModel.getColumn(getColumnCount() - 1).setCellRenderer(new WTableCellRenderer());
columnModel.getColumn(getColumnCount() - 1).setCellEditor(new WTableCellEditor());
}