我遇到了与以下 SO 问题Wrap multiple lines in JTable中提到的相同的问题。我发现渲染了多个单元格来完成这项工作。现在我的问题是在实现单元格渲染器后,我的单元格没有显示包装数据。我有自定义 tableModel,但我不确定如何在该模型上调用 datavalidator。任何人都可以请建议我。
我的表模型:
public class KeywordTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
Logger logger = Logger.getLogger(KeywordTableModel.class.getName());
KeywordList keywordList ;
public KeywordTableModel(KeywordList keywordList){
this.keywordList = keywordList;
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return keywordList.getKeywords().size();
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return keywordList.getTitles().length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
TypeRec objectRec = (TypeRec) keywordList.getKeywords().elementAt(rowIndex);
return objectRec.getColumnData(columnIndex);
}
@Override
public String getColumnName(int column){
return keywordList.getTitles()[column];
}
public void setDataValidator(){
}
}
我的单元格渲染器是:
/**
* Multiline Table Cell Renderer.
*/
public class MultiLineTableCellRenderer extends JTextArea
implements TableCellRenderer {
/**
*
*/
Logger logger = Logger.getLogger(MultiLineTableCellRenderer.class.getName());
private static final long serialVersionUID = 1L;
private List<List<Integer>> rowColHeight = new ArrayList<List<Integer>>();
public MultiLineTableCellRenderer() {
setLineWrap(true);
setWrapStyleWord(true);
setOpaque(true);
logger.debug("inside multilinetablecellrenderer constructor");
}
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
logger.debug("inside multilinetablecellrenderer renderer");
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setFont(table.getFont());
if (hasFocus) {
setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
if (table.isCellEditable(row, column)) {
setForeground(UIManager.getColor("Table.focusCellForeground"));
setBackground(UIManager.getColor("Table.focusCellBackground"));
}
} else {
setBorder(new EmptyBorder(1, 2, 1, 2));
}
if (value != null) {
setText(value.toString());
} else {
setText("");
}
adjustRowHeight(table, row, column);
return this;
}
/**
* Calculate the new preferred height for a given row, and sets the height on the table.
*/
private void adjustRowHeight(JTable table, int row, int column) {
//The trick to get this to work properly is to set the width of the column to the
//textarea. The reason for this is that getPreferredSize(), without a width tries
//to place all the text in one line. By setting the size with the with of the column,
//getPreferredSize() returnes the proper height which the row should have in
//order to make room for the text.
logger.debug("inside adjustRowheight method for adjusting the row height");
int cWidth = table.getTableHeader().getColumnModel().getColumn(column).getWidth();
setSize(new Dimension(cWidth, 1000));
int prefH = getPreferredSize().height;
while (rowColHeight.size() <= row) {
rowColHeight.add(new ArrayList<Integer>(column));
}
List<Integer> colHeights = rowColHeight.get(row);
while (colHeights.size() <= column) {
colHeights.add(0);
}
colHeights.set(column, prefH);
int maxH = prefH;
for (Integer colHeight : colHeights) {
if (colHeight > maxH) {
maxH = colHeight;
}
}
if (table.getRowHeight(row) != maxH) {
table.setRowHeight(row, maxH);
}
}
}
我将我的单元格渲染器设置为
cnr_DATA.setDefaultRenderer(String.class, new MultiLineTableCellRenderer());
该程序仍未将数据包装在多行中。