JTable with a custom column model : Table Column « Swing JFC « Java

JTable with a custom column model

JTable with a custom column model
import java.awt.BorderLayout;

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

public class ColumnExample extends JFrame {

  public ColumnExample() {
    super("Abstract Model JTable Test");
    setSize(300, 200);

    DefaultTableModel dtm = new DefaultTableModel(new String[][] {
        { "1", "2", "3" }, { "4", "5", "6" } }, new String[] { "Names",
        "In", "Order" });
    SortingColumnModel scm = new SortingColumnModel();
    JTable jt = new JTable(dtm, scm);

    JScrollPane jsp = new JScrollPane(jt);
    getContentPane().add(jsp, BorderLayout.CENTER);

  public static void main(String args[]) {
    ColumnExample ce = new ColumnExample();

class SortingColumnModel extends DefaultTableColumnModel {

  public void addColumn(TableColumn tc) {
    int newIndex = sortedIndexOf(tc);
    if (newIndex != tc.getModelIndex()) {
      moveColumn(tc.getModelIndex(), newIndex);

  protected int sortedIndexOf(TableColumn tc) {
    // just do a linear search for now
    int stop = getColumnCount();
    String name = tc.getHeaderValue().toString();

    for (int i = 0; i < stop; i++) {
      if (name.compareTo(getColumn(i).getHeaderValue().toString()) <= 0) {
        return i;
    return stop;


Related examples in the same category

1.Combine TableModel and ColumModelCombine TableModel and ColumModel
2.A row header column with the TableColumnModelA row header column with the TableColumnModel
3.Install Table Column to TableColumnModelInstall Table Column to TableColumnModel
4.Swing Table Column ToolTip SampleSwing Table Column ToolTip Sample
5.Table ColumnModel and TableColumnModelListenerTable ColumnModel and TableColumnModelListener
6.Table header: Label Header with iconTable header: Label Header with icon
7.Table Headerless SampleTable Headerless Sample
8.Multiline table headerMultiline table header
9.Row Number Table Header
10.Frozen Column Table Header
11.Packing a Column of a JTable Component according to the header text
12.Packing a Column of a JTable Component according to the row data
13.Set column width based on cell renderer
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.