ich hab da mal was zum Spielen: [SPOILER]```public class TableRendererTest {
// Die Interfaces werden nur für die Bestimmung den CellRenderers gebraucht und müssen nirgens implementiert werden.
public interface BlueTag {}
public interface RedTag {}
public interface YellowTag {}
public interface GreenTag {}
// der Zennrendeer wird für die verschiedenen Interfaces undterschiedlich konfiguriert
private static final class DefaultTableCellRendererExtension extends
DefaultTableCellRenderer {
private final Color backGround;
private DefaultTableCellRendererExtension(Color backGround) {
this.backGround = backGround;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component rendererComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
// farbe Abhängig von SpaltenTyp
rendererComponent.setBackground(backGround);
// Rand abhängig von SpaltenIndex
((JComponent) rendererComponent).setBorder(BorderFactory.createLineBorder(backGround.darker().darker(), column));
return rendererComponent;
}
}
private static final TableModel baseModel = createBaseModel();
public static void main(String[] args) {
JTable jTable = new JTable(baseModel);
setRenderer(jTable);
JPanel jPanel = prepareDisplay(createTableModels(), jTable);
JOptionPane.showMessageDialog(null, jPanel);
}
private static JPanel prepareDisplay(final List<TableModel> modelList,
final JTable jTable) {
JPanel jPanel = new JPanel(new BorderLayout());
jPanel.add(jTable);
jPanel.add(new JButton(new AbstractAction("voriges Model") {
@Override
public void actionPerformed(ActionEvent e) {
int indexOfModel = modelList.indexOf(jTable.getModel());
if (indexOfModel < 1)
indexOfModel = modelList.size();
jTable.setModel(modelList.get(indexOfModel - 1));
}
}), BorderLayout.NORTH);
jPanel.add(new JButton(new AbstractAction("nächstes Model") {
@Override
public void actionPerformed(ActionEvent e) {
int indexOfModel = modelList.indexOf(jTable.getModel());
if (indexOfModel + 1 == modelList.size())
indexOfModel = -1;
jTable.setModel(modelList.get(indexOfModel + 1));
}
}), BorderLayout.SOUTH);
return jPanel;
}
private static void setRenderer(final JTable jTable) {
jTable.setDefaultRenderer(BlueTag.class, new DefaultTableCellRendererExtension(Color.BLUE));
jTable.setDefaultRenderer(YellowTag.class, new DefaultTableCellRendererExtension(Color.YELLOW));
jTable.setDefaultRenderer(RedTag.class, new DefaultTableCellRendererExtension(Color.RED));
jTable.setDefaultRenderer(GreenTag.class, new DefaultTableCellRendererExtension(Color.GREEN));
}
private static List<TableModel> createTableModels() {
final List<TableModel> modelList = new ArrayList<>();
modelList.add(createSampleModel(
baseModel,
Arrays.asList(Object.class, RedTag.class,
GreenTag.class, BlueTag.class,
YellowTag.class).toArray(new Class<?>[0])));
modelList.add(createSampleModel(
baseModel,
Arrays.asList(YellowTag.class, RedTag.class).toArray(
new Class<?>[0])));
modelList.add(createSampleModel(
baseModel,
Arrays.asList(GreenTag.class, RedTag.class,
BlueTag.class).toArray(new Class<?>[0])));
modelList.add(createSampleModel(
baseModel,
Arrays.asList(GreenTag.class, BlueTag.class,
Object.class, RedTag.class,
YellowTag.class).toArray(new Class<?>[0])));
return modelList;
}
private static TableModel createSampleModel(final TableModel baseModel,
final Class<?>[] columnClasses) {
TableModel model1 = new DefaultTableModel(15, columnClasses.length) {
@Override
public Class<?> getColumnClass(int columnIndex) {
return columnClasses[columnIndex];
}
@Override
public Object getValueAt(int row, int column) {
return baseModel.getValueAt(row, column % columnClasses.length
+ baseModel.getColumnCount() / columnClasses.length);
}
};
return model1;
}
private static TableModel createBaseModel() {
TableModel baseModel = new DefaultTableModel(15, 5) {
@Override
public Object getValueAt(int row, int column) {
NumberFormat integerInstance = NumberFormat
.getIntegerInstance();
integerInstance.setMinimumIntegerDigits(2);
return MessageFormat.format("{0} - {1}",
integerInstance.format(row),
integerInstance.format(column));
}
};
return baseModel;
}
}```[/SPOILER]
bye
TT