有阵子没来了,今天空点上来写点东西,是关于JTable 每行自适应行高,我的JTable只有一列,多列的话就是取该行的最高列值
首先实现一个自已的TableCellRenderer类
public class TableCellTextAreaRenderer extends JTextArea implements TableCellRenderer {
private static final long serialVersionUID = 1L;
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 final static Color usedColor = new Color(160, 160, 160);
private Color unselectedForeground;
private Color unselectedBackground;
public TableCellTextAreaRenderer() {
setEditable(false);
setOpaque(true);
setLineWrap(true);
setWrapStyleWord(true);
setBorder(getNoFocusBorder());
setName("Table.ASTableCellTextAreaRenderer");
}
private Border getNoFocusBorder() {
Border border = DefaultLookup.getBorder(this, ui, "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) {
super.setForeground(c);
unselectedForeground = c;
}
public void setBackground(Color c) {
super.setBackground(c);
unselectedBackground = c;
}
public void updateUI() {
super.updateUI();
setForeground(null);
setBackground(null);
}
public Component getTableCellRendererComponent(JTable table, Objectvalue, boolean isSelected, boolean hasFocus, int row, int 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 = DefaultLookup.getColor(this, ui, "Table.dropCellForeground");
bg = DefaultLookup.getColor(this, ui, "Table.dropCellBackground");
isSelected = true;
}
if (isSelected) {
super.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 = DefaultLookup.getColor(this, ui, "Table.alternateRowColor");
if (alternateColor != null && row % 2 == 0)
background = alternateColor;
}
super.setBackground(background);
}
setFont(table.getFont());
if (hasFocus) {
Border border = null;
if (isSelected) {
border = DefaultLookup.getBorder(this, ui,
"Table.focusSelectedCellHighlightBorder");
}
if (border == null) {
border = DefaultLookup.getBorder(this, ui,
"Table.focusCellHighlightBorder");
}
setBorder(border);
if (!isSelected && table.isCellEditable(row, column)) {
Color col;
col = DefaultLookup.getColor(this, ui,
"Table.focusCellForeground");
if (col != null) {
super.setForeground(col);
}
col = DefaultLookup.getColor(this, ui,
"Table.focusCellBackground");
if (col != null) {
super.setBackground(col);
}
}
} else {
setBorder(getNoFocusBorder());
}
setValue(value);
setToolTipText(value.toString());
//下面这条语句是实现每行自适应的关键
table.setRowHeight(row, this.getMinimumSize().height);
return this;
}
public boolean isOpaque() {
Color back = getBackground();
Component p = getParent();
if (p != null) {
p = p.getParent();
}
boolean colorMatch = (back != null) && (p != null)
&& back.equals(p.getBackground()) && p.isOpaque();
return !colorMatch && super.isOpaque();
}
public void invalidate() {
}
public void validate() {
}
public void revalidate() {
}
public void repaint(long tm, int x, int y, int width, int height) {
}
public void repaint(Rectangle r) {
}
public void repaint() {
}
protected void firePropertyChange(String propertyName, Object oldValue,
Object newValue) {
if (propertyName == "text"
|| propertyName == "labelFor"
|| propertyName == "displayedMnemonic"
|| ((propertyName == "font" || propertyName == "foreground")
&& oldValue != newValue &&getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey) !=null)) {
super.firePropertyChange(propertyName, oldValue, newValue);
}
}
public void firePropertyChange(String propertyName, boolean oldValue,
boolean newValue) {
}
protected void setValue(Object value) {
setText((value == null) ? "" : value.toString());
}
}
其次创建自已的TableCellRenderer类
TableCellRenderer renderer = new TableCellTextAreaRenderer();
最后将renderer设置为各列的单元格
JTable.getColumnModel().getColumn(0).setCellRenderer(renderer);