1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| public class MultiLineTableCellRenderer implements TableCellRenderer {
private static final Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
private static final Border DEFAULT_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
protected static Border noFocusBorder = DEFAULT_NO_FOCUS_BORDER;
private Color unselectedForeground;
private Color unselectedBackground;
private JTextArea textarea;
private JPanel panel;
public MultiLineTableCellRenderer() {
panel = new JPanel(new GridBagLayout());
textarea = new JTextArea();
textarea.setEditable(false);
textarea.setOpaque(false);
panel.setBorder(getNoFocusBorder());
textarea.setBorder(null);
textarea.setName("Table.cellRenderer");
textarea.setLineWrap(true);
textarea.setWrapStyleWord(true);
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(textarea, gbc);
panel.setOpaque(true);
}
public MultiLineTableCellRenderer setWrapStyleWord(boolean style) {
textarea.setWrapStyleWord(style);
return this;
}
public MultiLineTableCellRenderer setInsets(int thickness) {
return setInsets(thickness,thickness,thickness,thickness);
}
public MultiLineTableCellRenderer setInsets(int top, int left,
int bottom, int right) {
textarea.setBorder(BorderFactory.createEmptyBorder(top,left,bottom,right));
return this;
}
private Border getNoFocusBorder() {
Border border = UIManager.getBorder("Table.cellNoFocusBorder");
if (System.getSecurityManager() != null) {
if (border != null) return border;
return SAFE_NO_FOCUS_BORDER;
} else if (border != null) {
if (noFocusBorder == null || noFocusBorder == DEFAULT_NO_FOCUS_BORDER) {
return border;
}
}
return noFocusBorder;
}
public void setForeground(Color c) {
if ( textarea!=null ) textarea.setForeground(c);
unselectedForeground = c;
}
public void setBackground(Color c) {
textarea.setBackground(c);
panel.setBackground(c);
unselectedBackground = c;
}
public void updateUI() {
textarea.updateUI();
setForeground(null);
setBackground(null);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (table == null) {
return panel;
}
TableColumn tableColumn = table.getColumnModel().getColumn(table.convertColumnIndexToModel(column));
Color fg = null;
Color bg = null;
JTable.DropLocation dropLocation = table.getDropLocation();
if (dropLocation != null
&& !dropLocation.isInsertRow()
&& !dropLocation.isInsertColumn()
&& dropLocation.getRow() == row
&& dropLocation.getColumn() == column) {
fg = UIManager.getColor("Table.dropCellForeground");
bg = UIManager.getColor("Table.dropCellBackground");
isSelected = true;
}
if (isSelected) {
textarea.setForeground(fg == null ? table.getSelectionForeground()
: fg);
panel.setBackground(bg == null ? table.getSelectionBackground()
: bg);
} else {
Color background = unselectedBackground != null
? unselectedBackground
: table.getBackground();
if (background == null || background instanceof javax.swing.plaf.UIResource) {
Color alternateColor = UIManager.getColor("Table.alternateRowColor");
if (alternateColor != null && row % 2 != 0) {
background = alternateColor;
}
}
textarea.setForeground(unselectedForeground != null
? unselectedForeground
: table.getForeground());
panel.setBackground(background);
}
textarea.setFont(table.getFont());
if (hasFocus) {
Border border = null;
if (isSelected) {
border = UIManager.getBorder("Table.focusSelectedCellHighlightBorder");
}
if (border == null) {
border = UIManager.getBorder("Table.focusCellHighlightBorder");
}
panel.setBorder(border);
if (!isSelected && table.isCellEditable(row, column)) {
Color col;
col = UIManager.getColor("Table.focusCellForeground");
if (col != null) {
textarea.setForeground(col);
}
col = UIManager.getColor("Table.focusCellBackground");
if (col != null) {
panel.setBackground(col);
}
}
} else {
panel.setBorder(getNoFocusBorder());
}
setValue(value);
textarea.setMaximumSize(new Dimension(tableColumn.getWidth(),0));
textarea.doLayout();
textarea.setSize(new Dimension(tableColumn.getWidth(),(int)textarea.getPreferredSize().getHeight()));
panel.setSize(new Dimension(tableColumn.getWidth(),(int)textarea.getPreferredSize().getHeight()));
return panel;
}
public boolean isOpaque() {
Color back = panel.getBackground();
Component p = panel.getParent();
if (p != null) {
p = p.getParent();
}
// p should now be the JTable.
boolean colorMatch = (back != null) && (p != null) &&
back.equals(p.getBackground()) &&
p.isOpaque();
return !colorMatch && panel.isOpaque();
}
protected void setValue(Object value) {
textarea.setText((value == null) ? "" : value.toString());
}
} |