public class MultiLineRenderer extends DefaultTableCellRenderer {
private JEditorPane m_txt;
private Border m_border;
private Border m_emptyBorder;
private Color m_colForeground = null;
private Color m_colBackground = null;
private Color m_colSelectionForeground = null;
private Color m_colSelectionBackground = null;
public MultiLineRenderer( boolean bAutoResize, JTable tblSlave ) {
m_txt = new JEditorPane();
m_txt.setMargin( new Insets(0, 1, 1, 1 ) );
m_border = BorderFactory.createLineBorder( Color.BLUE );
m_emptyBorder = BorderFactory.createEmptyBorder();
initColors();
}
private void initColors() {
m_colSelectionForeground = UIManager.getLookAndFeelDefaults().getColor("Table.selectionForeground");
m_colForeground = UIManager.getLookAndFeelDefaults().getColor("Table.foreground");
m_colSelectionBackground = UIManager.getLookAndFeelDefaults().getColor("Table.selectionBackground");
m_colBackground = UIManager.getLookAndFeelDefaults().getColor("Table.background");
}
public Component getTableCellRendererComponent(
JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
String s = (String)value;
Font font = table.getFont();
if( !( m_txt.getFont().equals( font ) ) ) {
m_txt.setFont( font );
}
m_txt.setText( s );
if (isSelected) {
m_txt.setForeground( m_colSelectionForeground );
m_txt.setBackground( m_colSelectionBackground );
} else {
m_txt.setForeground( m_colForeground );
m_txt.setBackground( m_colBackground );
}
if (hasFocus) {
setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
if (!isSelected && table.isCellEditable(row, column)) {
Color col;
col = UIManager.getColor("Table.focusCellForeground");
if (col != null) {
m_txt.setForeground(col);
}
col = UIManager.getColor("Table.focusCellBackground");
if (col != null) {
m_txt.setBackground(col);
}
}
} else {
setBorder(noFocusBorder);
}
return m_txt;
}
}