Java TableCellRenderer resize columns based on the column and data preferred widths

Description

Java TableCellRenderer resize columns based on the column and data preferred widths

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class Main extends JPanel {
   public static void main(String[] args) {
      JFrame frame = new JFrame("java2s.com");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      Main m = new Main();
      frame.add(m);//from  w  w w.j  av  a  2s  . co m
      frame.setSize(300, 210);
      frame.setVisible(true);
   }
   public Main() {
      setLayout(new BorderLayout(5, 5));
      DefaultTableModel model = new DefaultTableModel();

      JTable table = new JTable(model);

      // Add data
      model.addColumn("Col1", new Object[] { "1" });
      model.addColumn("Col2", new Object[] { "2" });

      model.addRow(new Object[] { "a", "b" });
      model.addRow(new Object[] { "c", "d" });

      JScrollPane sp = new JScrollPane(table);

      autoResizeColWidth(table, (AbstractTableModel)table.getModel());
      add(sp, BorderLayout.CENTER);
   }

   /**
    * Resizes the table columns based on the column and data preferred widths.
    * 
    * @param table the original table
    * @param model the data model
    * @return the table itself
    */
   public static JTable autoResizeColWidth(JTable table, AbstractTableModel model) {
      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      table.setModel(model);

      int margin = 5;

      for (int i = 0; i < table.getColumnCount(); i++) {
         int vColIndex = i;
         DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
         TableColumn col = colModel.getColumn(vColIndex);
         int width;

         // Get width of column header
         TableCellRenderer renderer = col.getHeaderRenderer();

         if (renderer == null) {
            renderer = table.getTableHeader().getDefaultRenderer();
         }

         Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);

         width = comp.getPreferredSize().width;

         // Get maximum width of column data
         for (int r = 0; r < table.getRowCount(); r++) {
            renderer = table.getCellRenderer(r, vColIndex);
            comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false, r,
                  vColIndex);
            width = Math.max(width, comp.getPreferredSize().width);
         }

         // Add margin
         width += 2 * margin;

         // Set the width
         col.setPreferredWidth(width);
      }

      ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer())
            .setHorizontalAlignment(SwingConstants.LEFT);

      return table;
   }
}



PreviousNext

Related