How to implement multiple line text renderer in JTable

痴心易碎 提交于 2019-12-11 02:00:04

问题


I faced the same problem as mentioned in the following SO question Wrap multiple lines in JTable. and I found Multile cell rendered to do that job. Now my problem is after implementing the cell renderer my cell is not showing wrapped data. I have custom tableModel and I am not sure how to call datavalidator on that model. Could anyone please suggest me.

My Table model:

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(){

}

}

My cell renderer is :

/**
   * 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);
  }
}

}

and I am setting my cell renderer as

     cnr_DATA.setDefaultRenderer(String.class, new MultiLineTableCellRenderer());

The program is still not wrapping data in multiple line.


回答1:


Absent a complete example, I'm guessing you need to override getColumnClass() in your TableModel to return the same type token that you specified in setDefaultRenderer(), i.e. String.class. Note that the AbstractTableModel implementation returns Object.class unconditionally.



来源:https://stackoverflow.com/questions/16597431/how-to-implement-multiple-line-text-renderer-in-jtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!