Set column width based on cell renderer : JTable Column « Swing « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Swing » JTable Column 
14. 62. 25. 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();

  }
}
14. 62. JTable Column
14. 62. 1. Resizable JTable ColumnResizable JTable Column
14. 62. 2. Specifying Fixed JTable ColumnsSpecifying Fixed JTable Columns
14. 62. 3. Change Column WidthsChange Column Widths
14. 62. 4. Setting the Width of a Column in a JTable Component
14. 62. 5. Shading Rows and Columns in a JTable Component
14. 62. 6. Shades every other column yellow
14. 62. 7. Converts a visible column index to a column index in the model.
14. 62. 8. Insert a new column to a table
14. 62. 9. Converts a column index in the model to a visible column index
14. 62. 10. Returns the visible columns in the order that they appear in the model
14. 62. 11. Get the columns from TableColumnModel in the order that they appear in the view
14. 62. 12. Get column count
14. 62. 13. Returns the TableColumn associated with the specified column index in the model
14. 62. 14. Setting the Column Resize Mode of a JTable Component: Disable auto resizing
14. 62. 15. Locking the Width of a Column in a JTable Component
14. 62. 16. Appending a Column to a JTable Component using DefaultTableModel
14. 62. 17. Add a column with values.
14. 62. 18. Disable autoCreateColumnsFromModel
14. 62. 19. Add a column without affecting existing columns
14. 62. 20. Remove the first visible column without removing the underlying data
14. 62. 21. Move the last visible column so it becomes the first visible column
14. 62. 22. the last column is moved to the first position
14. 62. 23. Packing a Column of a JTable Component according to the header text
14. 62. 24. Packing a Column of a JTable Component according to the row data
14. 62. 25. Set column width based on cell renderer
14. 62. 26. Set table column auto-resize off
14. 62. 27. Changing the Name of a Column in a JTable Component
14. 62. 28. Disable auto resizing, Set the first visible column to 100 pixels wide
14. 62. 29. Change a cell in the 2nd visible column
14. 62. 30. Change a cell in the 3rd column in the model
14. 62. 31. Displaying an Icon in a Column Head of a JTable Component
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.