Java JTable set column width and add margin

Description

Java JTable set column width and add margin

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.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class Main extends JPanel {
   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" });
      //from   w  w w.  j  a va  2  s . co m
      model.addRow(new Object[] { "a","b"});
      model.addRow(new Object[] { "c","d"});

      packAllColumns(table); 

      // Force the header to resize and repaint itself
      table.getTableHeader().resizeAndRepaint();
      
      JScrollPane sp = new JScrollPane(table);

      add(sp, BorderLayout.CENTER);
   }
   private static final int DEFAULT_COLUMN_MARGIN = 5;


   public static void packAllColumns(JTable table) {
       packAllColumns(table, DEFAULT_COLUMN_MARGIN);
   }


   public static void packAllColumns(JTable table, int margin) {
       for (int c = 0; c < table.getColumnCount(); c++) {
           packColumn(table, c, margin);
       }
   }

   public static void packColumn(JTable table, int vColIndex, int margin) {

       DefaultTableColumnModel colModel = (DefaultTableColumnModel) table
               .getColumnModel();
       TableColumn col = colModel.getColumn(vColIndex);
       int width = 0;

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

       // because checking the width of each row can be time consuming,
       // only do so if less than 101 rows.
       boolean checkDataWidth = (table.getRowCount() < 101);

       if (checkDataWidth) {
           // Get maximum width from all 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);
   }
   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);
      frame.setSize(300, 210);
      frame.setVisible(true);
   }

}



PreviousNext

Related