SWING JTable: How do I make each row as high as it's tallest object?

I have a table , where some objects are rendered and are of non-fixed size (list of attributes). I want each row to be as high as it's tallest object , and I was wondering of to do it. I thought about doing something like this (see below) , but I'm sure there's something better..

public Component getTableCellRendererComponent(JTable table, Object value,
                                                 boolean isSelected,
                                                 boolean hasFocus, int row,
                                                 int column)
  {
      /*....*/

      this.setListData((Object[])value);
      int height = new Double(getPreferredSize().getHeight()).intValue();
      if (table.getRowHeight(row) < height)
          table.setRowHeight(row, height);    
      /*....*/

      return this;
  }

Answers


You should not have code like that in a renderer. Instead, when you load the data into the model, do something like:

private void updateRowHeights()
{
    try
    {
        for (int row = 0; row < table.getRowCount(); row++)
        {
            int rowHeight = table.getRowHeight();

            for (int column = 0; column < table.getColumnCount(); column++)
            {
                Component comp = table.prepareRenderer(table.getCellRenderer(row, column), row, column);
                rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
            }

            table.setRowHeight(row, rowHeight);
        }
    }
    catch(ClassCastException e) {}
}

I would stick with the solution you have. I don't think there is a simpler way to do this. No matter what you are going to have the check the height of each cell, so you might as well do that as you render each one.


Need Your Help

How to get customized tab

android tabs

I wanted to customize my tab, which looks similar to below image.. any idea how to do..

Socket.io / Node.js: “value is out of bounds” on socket connection from client

node.js socket.io arm base64 engine.io

I am working on getting nodejs / socket.io / express running on busybox linux on an ARMv5TE processor. I have nodejs up and running and the following code runs without errors.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.