Changing the Name of a Column in a JTable Component - Java Swing

Java examples for Swing:JTable Column

Description

Changing the Name of a Column in a JTable Component

Demo Code

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Main {
  public static void main(String[] args) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);

    model.addColumn("Col1");
    model.addColumn("Col2");

    // Change name of first visible column
    int vColIndex = 0;
    table.getColumnModel().getColumn(vColIndex).setHeaderValue("New Name");

    // Force the header to resize and repaint itself
    table.getTableHeader().resizeAndRepaint();
  }// w w  w. j  a v a  2  s.c  om
}

Related Tutorials