Table ColumnModel and TableColumnModelListener : Table Column « Swing JFC « Java






Table ColumnModel and TableColumnModelListener

Table ColumnModel and TableColumnModelListener
  
/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski     
ISBN: 1-893115-78-X
Publisher: APress
*/

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class ColumnModelSample {
  public static void main(String args[]) {
    Object rows[][] = { { "one", "ichi - \u4E00" },
        { "two", "ni - \u4E8C" }, { "three", "san - \u4E09" },
        { "four", "shi - \u56DB" }, { "five", "go - \u4E94" },
        { "six", "roku - \u516D" }, { "seven", "shichi - \u4E03" },
        { "eight", "hachi - \u516B" }, { "nine", "kyu - \u4E5D" },
        { "ten", "ju - \u5341" } };
    Object headers[] = { "English", "Japanese" };
    JFrame frame = new JFrame("Scrollless Table");
    JTable table = new JTable(rows, headers);

    TableColumnModelListener tableColumnModelListener = new TableColumnModelListener() {
      public void columnAdded(TableColumnModelEvent e) {
        System.out.println("Added");
      }

      public void columnMarginChanged(ChangeEvent e) {
        System.out.println("Margin");
      }

      public void columnMoved(TableColumnModelEvent e) {
        System.out.println("Moved");
      }

      public void columnRemoved(TableColumnModelEvent e) {
        System.out.println("Removed");
      }

      public void columnSelectionChanged(ListSelectionEvent e) {
        System.out.println("Selection Changed");
      }
    };

    TableColumnModel columnModel = table.getColumnModel();
    columnModel.addColumnModelListener(tableColumnModelListener);

    columnModel.setColumnMargin(12);

    TableColumn column = new TableColumn(1);
    columnModel.addColumn(column);

    frame.getContentPane().add(table, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}

           
         
    
  








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 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.