JTable 单元格多行显示

public class TextAreaCellRenderer extends JTextArea implements
TableCellRenderer
... {

publicTextAreaCellRenderer()...{
setLineWrap(
true);
setWrapStyleWord(
true);
}


publicComponentgetTableCellRendererComponent(JTabletable,Objectvalue,
booleanisSelected,booleanhasFocus,introw,intcolumn)...{
//计算当下行的最佳高度
intmaxPreferredHeight=0;
for(inti=0;i<table.getColumnCount();i++)...{
setText(
""+table.getValueAt(row,i));
setSize(table.getColumnModel().getColumn(column).getWidth(),
0);
maxPreferredHeight
=Math.max(maxPreferredHeight,
getPreferredSize().height);
}


if(table.getRowHeight(row)!=maxPreferredHeight)//少了这行则处理器瞎忙
table.setRowHeight(row,maxPreferredHeight);

setText(value
==null?"":value.toString());
returnthis;
}

}

你可能感兴趣的:(jtable)