我有一个MultilineCellRenderer
应该将多行包装在JTable
单元格中。
public class MultiLineTableCellRenderer
extends JTextArea implements TableCellRenderer {
/**
*
*/
private static final long serialVersionUID = 1L;
public MultiLineTableCellRenderer() {
setLineWrap(true);
setWrapStyleWord(true);
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
setText(value.toString());//or something in value, like value.getNote()..
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setSize(table.getColumnModel().getColumn(column).getWidth(),
getPreferredSize().height);
if (table.getRowHeight(row) != getPreferredSize().height) {
table.setRowHeight(row, getPreferredSize().height);
}
return this;
}
}
我正在创建和分配cell renderer
这个default cell renderer
myjtable(cnr_DATA)
MultiLineTableCellRenderer r = new MultiLineTableCellRenderer();
cnr_DATA.setDefaultRenderer(String.class, r);
cnr_DATA.setModel(new DefaultTableModel(data,columns){
public Class getColumnClass(int col){
return String.class;
};
});
我也在从数据库中动态更新 jtable 的内容
DefaultTableModel model = (DefaultTableModel)cnr_DATA.getModel();
删除所有当前行(模型);
Vector<DocumentRow> data =
RecycleSQL.readRecycledDocuments();//this line returning vector of objects
for(DocumentRow object:data)
model.addRow(new string[]{object.getFilename(),
object.getTitle(),object.getLastTouched()
,object.getLastTouchedBy()});
model.setRowCount(data.size());
cnr_DATA.revalidate();
我的问题是mycellrenderer
没有被调用并且没有包装数据。有人可以建议我在更新JTable
.
根据Madprogrammer和气垫船的建议更新
根据建议修改代码后。现在cellrenderer
正在调用,但它将行高设置为默认值。这是来自调试语句的日志
[2013-08-02 01:20:53,335] [AWT-EventQueue-0] DEBUG MultiLineTableCellRenderer - setting row height 3 128
[2013-08-02 01:20:53,335] [AWT-EventQueue-0] DEBUG MultiLineTableCellRenderer - setting row height 3 16
这意味着,在正确设置行的高度后,它会将其恢复为默认值