Question

Good Morning everyone. I have a Multilinecellrenenderer which is changing the height of the jtable row based on the content of the cell.

    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;
} 

I understand that modifying the jtable inside the renderer make it run in infinite loop and that is happening in my code which I verified using the logs statements. I also understand that I can modify the row height during adding content of the model and I have tried everything I knew but I am not able to set the height of the row outside the renenderer. Could anyone please help me how can I call table.setRowHeight(int,int) outside the cell renenderer.

I am adding the content of the model dynamically using following statement

   for(ProductRow object: data){
       model.addRow(new Object[]{object.getProductNumber(),object.getDescription(),
               object.getLastTouchedOn(),object.getLastTouchedBy()});
       }

I just do not know how to get the current row number and preferred height. Please help me. Thanks

Was it helpful?

Solution

As @mKorbel shows in this example, you can derive the desired size for setRowHeight() from the preferred sizes of enclosed components. Be sure to include a subsequent call to pack() the enclosing frame.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top