org.opentcs.util.gui.StringTableCellRenderer Maven / Gradle / Ivy
/**
* Copyright (c) The openTCS Authors.
*
* This program is free software and subject to the MIT license. (For details,
* see the licensing information (LICENSE.txt) you should have received with
* this copy of the software.)
*/
package org.opentcs.util.gui;
import static java.util.Objects.requireNonNull;
import java.awt.Component;
import java.util.function.Function;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
/**
* Renders values to JLabels.
*
* @param The type of the table cell values that the representer can represent.
*/
public class StringTableCellRenderer
implements
TableCellRenderer {
/**
* The default renderer we delegate to.
*/
private final DefaultTableCellRenderer delegate = new DefaultTableCellRenderer();
/**
* Returns a String representation of E.
*/
private final Function representer;
/**
* Creates an instance.
*
* @param representer a string representation provider for the values of the list.
* Null value as parameter for the representer is possible.
* The result is set as text of the JLabel.
*/
public StringTableCellRenderer(Function representer) {
this.representer = requireNonNull(representer, "representer");
}
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column
) {
JLabel label = (JLabel) delegate.getTableCellRendererComponent(
table,
value,
isSelected,
hasFocus,
row,
column
);
@SuppressWarnings("unchecked")
E val = (E) value;
label.setText(representer.apply(val));
return label;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy