TableModel: getColumnName(int columnIndex) : TableModel « javax.swing.table « Java by API






TableModel: getColumnName(int columnIndex)

 
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class MainClass {
  public static void main(String args[]) {
    String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
    String headers[] = { "Upper", "Lower" };
    
    JFrame frame = new JFrame("Tooltip Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    JTable table = new JTable(rows, headers);

    JLabel headerRenderer = new DefaultTableCellRenderer();
    String columnName = table.getModel().getColumnName(0);
    headerRenderer.setText(columnName);
    headerRenderer.setToolTipText("Wave");
    
    TableColumnModel columnModel = table.getColumnModel();
    TableColumn column = columnModel.getColumn(0);
    column.setHeaderRenderer((TableCellRenderer) headerRenderer);

    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }

}
           
         
  








Related examples in the same category

1.TableModel: addTableModelListener(TableModelListener l)