【发布时间】:2014-10-24 10:31:59
【问题描述】:
如何从我的 ArrayList 创建一个 DefaultTable:
public class FileModel extends AbstractTableModel implements TableModel{
List<Object[]> data = new ArrayList<>();
String titles[] = new String[] { "File Name", "Pages", "Media Box Height", "Media Box Width", "Trim Box Height",
"Trim Box Width", "Path", "Error" };
Class<?> types[] = new Class[] { String.class, Integer.class, Integer.class, Integer.class, Integer.class, Integer.class, String.class, String.class };
我想将它用于颜色渲染器,因为目前渲染器只是为“字符串”着色...... DefaultModel 应如下所示:
DefaultTableModel model = new DefaultTableModel(data, columnNames) {
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
我当前的 Colorrenderer 看起来是这样的:
public class ColorRenderer extends DefaultTableCellRenderer {
static final long serialVersionUID = 1L;
final int STATUS_COL = 7;
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int col) {
Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
String type = (String) table.getModel().getValueAt(row, 7);
if ("Error" == type) {
component.setBackground(Color.RED);
component.setForeground(Color.WHITE);
} else if (isSelected) {
component.setBackground(table.getSelectionBackground());
component.setForeground(table.getSelectionForeground());
} else {
component.setBackground(table.getBackground());
component.setForeground(table.getForeground());
}
return component;
}
}
【问题讨论】:
标签: java swing arraylist jtable renderer