Set column width based on cell renderer : Table Column « Swing JFC « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Swing JFC » Table Column 




Set column width based on cell renderer
 

import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class Main {
  public Main() {
    JTable table = new JTable(33);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

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

      TableCellRenderer renderer = col.getHeaderRenderer();
      for (int r = 0; r < table.getRowCount(); r++) {
        renderer = table.getCellRenderer(r, i);
        Component comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, i),
            false, false, r, i);
        width = Math.max(width, comp.getPreferredSize().width);
      }
      col.setPreferredWidth(width + 2);
    }
    JFrame f = new JFrame();
    f.add(new JScrollPane(table));
    f.setSize(300300);
    f.setVisible(true);
  }

  public static void main(String[] argv) {
    new Main();

  }
}

   
  














Related examples in the same category
1.Combine TableModel and ColumModelCombine TableModel and ColumModel
2.JTable with a custom column modelJTable with a custom column model
3.A row header column with the TableColumnModelA row header column with the TableColumnModel
4.Install Table Column to TableColumnModelInstall Table Column to TableColumnModel
5.Swing Table Column ToolTip SampleSwing Table Column ToolTip Sample
6.Table ColumnModel and TableColumnModelListenerTable ColumnModel and TableColumnModelListener
7.Table header: Label Header with iconTable header: Label Header with icon
8.Table Headerless SampleTable Headerless Sample
9.Multiline table headerMultiline table header
10.Row Number Table Header
11.Frozen Column Table Header
12.Packing a Column of a JTable Component according to the header text
13.Packing a Column of a JTable Component according to the row data
14.Set table column auto-resize off
15.Get the columns from TableColumnModel in the order that they appear in the view
16.Get column count
17.Returns the TableColumn associated with the specified column index in the model
18.Setting the Width of a Column in a JTable Component
19.Disable auto resizing, Set the first visible column to 100 pixels wide
20.Setting the Column Resize Mode of a JTable Component: Disable auto resizing
21.When the width of a column is changed, the width of the right-most column is changed
22.When the width of a column is changed, all columns to the right are resized
23.When the width of a column is changed, only the columns to the left and right of the margin change
24.When the width of a column is changed, the widths of all columns are changed
25.Locking the Width of a Column in a JTable Component
26.This program demonstrates how to work with rows and columns in a table.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.