Example usage for javax.swing.table TableColumn setHeaderRenderer

List of usage examples for javax.swing.table TableColumn setHeaderRenderer

Introduction

In this page you can find the example usage for javax.swing.table TableColumn setHeaderRenderer.

Prototype

@BeanProperty(description = "The header renderer.")
public void setHeaderRenderer(TableCellRenderer headerRenderer) 

Source Link

Document

Sets the TableCellRenderer used to draw the TableColumn's header to headerRenderer.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    JTable table = new JTable();
    int vColIndex = 0;
    TableColumn col = table.getColumnModel().getColumn(vColIndex);
    col.setHeaderRenderer(new MyTableHeaderRenderer());

}

From source file:MainClass.java

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);//  w  w  w . j  a  va  2  s.  co  m
    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);
}

From source file:LabelHeaderSample.java

public static void main(String args[]) {
    final Object rows[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" } };
    JFrame frame = new JFrame("Label Header");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String headers[] = { "English", "#" };
    JTable table = new JTable(rows, headers);
    JScrollPane scrollPane = new JScrollPane(table);

    JLabel headerRenderer = new DefaultTableCellRenderer();
    String columnName = table.getModel().getColumnName(0);
    headerRenderer.setText(columnName);/*from   w  w  w . ja  va2s .  c  om*/
    headerRenderer.setToolTipText("text");
    TableColumnModel columnModel = table.getColumnModel();
    TableColumn englishColumn = columnModel.getColumn(0);
    englishColumn.setHeaderRenderer((TableCellRenderer) headerRenderer);

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

From source file:SwingToolTipSample.java

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("Tooltip Sample");
    JTable table = new JTable(rows, headers);

    JLabel headerRenderer = new DefaultTableCellRenderer();
    String columnName = table.getModel().getColumnName(0);
    headerRenderer.setText(columnName);//from w  w  w  .j  a  v  a  2 s  . co  m
    headerRenderer.setToolTipText("Wave");
    TableColumnModel columnModel = table.getColumnModel();
    TableColumn englishColumn = columnModel.getColumn(0);
    englishColumn.setHeaderRenderer((TableCellRenderer) headerRenderer);

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

From source file:MainClass.java

public static void main(String args[]) {
    String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
    String headers[] = { "Upper", "Lower" };

    JFrame frame = new JFrame("Label Header");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(rows, headers);
    JScrollPane scrollPane = new JScrollPane(table);

    Border headerBorder = UIManager.getBorder("TableHeader.cellBorder");

    JLabel headerLabel1 = new JLabel(headers[0], JLabel.CENTER);
    headerLabel1.setBorder(headerBorder);
    JLabel headerLabel2 = new JLabel(headers[1], JLabel.CENTER);
    headerLabel2.setBorder(headerBorder);

    TableCellRenderer renderer = new JComponentTableCellRenderer();

    TableColumnModel columnModel = table.getColumnModel();

    TableColumn column0 = columnModel.getColumn(0);
    TableColumn column1 = columnModel.getColumn(1);

    column0.setHeaderRenderer(renderer);
    column0.setHeaderValue(headerLabel1);

    column1.setHeaderRenderer(renderer);
    column1.setHeaderValue(headerLabel2);

    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);/*from  www.  jav a2s . c om*/
    frame.setVisible(true);
}

From source file:JComponentTableCellRenderer.java

public static void main(String args[]) {
    final Object rows[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" } };
    JFrame frame = new JFrame("Label Header");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String headers[] = { "English", "#" };
    JTable table = new JTable(rows, headers);
    JScrollPane scrollPane = new JScrollPane(table);

    Icon redIcon = new ImageIcon("yourFile.gif");
    Icon blueIcon = new ImageIcon("yourFile.gif");

    Border headerBorder = UIManager.getBorder("TableHeader.cellBorder");

    JLabel blueLabel = new JLabel(headers[0], blueIcon, JLabel.CENTER);
    blueLabel.setBorder(headerBorder);//w ww. jav  a  2  s .  c om
    JLabel redLabel = new JLabel(headers[1], redIcon, JLabel.CENTER);
    redLabel.setBorder(headerBorder);

    TableCellRenderer renderer = new JComponentTableCellRenderer();

    TableColumnModel columnModel = table.getColumnModel();

    TableColumn column0 = columnModel.getColumn(0);
    TableColumn column1 = columnModel.getColumn(1);

    column0.setHeaderRenderer(renderer);
    column0.setHeaderValue(blueLabel);

    column1.setHeaderRenderer(renderer);
    column1.setHeaderValue(redLabel);

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

From source file:LabelHeaderSample.java

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" } };
    JFrame frame = new JFrame("Label Header");
    String headers[] = { "English", "Japanese" };
    JTable table = new JTable(rows, headers);
    JScrollPane scrollPane = new JScrollPane(table);

    Icon redIcon = new DiamondIcon(Color.red);
    Icon blueIcon = new DiamondIcon(Color.blue);

    Border headerBorder = UIManager.getBorder("TableHeader.cellBorder");

    JLabel blueLabel = new JLabel(headers[0], blueIcon, JLabel.CENTER);
    blueLabel.setBorder(headerBorder);/*from   w  w w  . j a  v  a2 s . c o m*/
    JLabel redLabel = new JLabel(headers[1], redIcon, JLabel.CENTER);
    redLabel.setBorder(headerBorder);

    TableCellRenderer renderer = new JComponentTableCellRenderer();

    TableColumnModel columnModel = table.getColumnModel();

    TableColumn column0 = columnModel.getColumn(0);
    TableColumn column1 = columnModel.getColumn(1);

    column0.setHeaderRenderer(renderer);
    column0.setHeaderValue(blueLabel);

    column1.setHeaderRenderer(renderer);
    column1.setHeaderValue(redLabel);

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

From source file:TableCellRenderTest.java

public TableCellRenderFrame() {
    setTitle("TableCellRenderTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    TableModel model = new PlanetTableModel();
    JTable table = new JTable(model);
    table.setRowSelectionAllowed(false);

    // set up renderers and editors

    table.setDefaultRenderer(Color.class, new ColorTableCellRenderer());
    table.setDefaultEditor(Color.class, new ColorTableCellEditor());

    JComboBox moonCombo = new JComboBox();
    for (int i = 0; i <= 20; i++)
        moonCombo.addItem(i);//from   w ww .  j  a va2s  .c  om

    TableColumnModel columnModel = table.getColumnModel();
    TableColumn moonColumn = columnModel.getColumn(PlanetTableModel.MOONS_COLUMN);
    moonColumn.setCellEditor(new DefaultCellEditor(moonCombo));
    moonColumn.setHeaderRenderer(table.getDefaultRenderer(ImageIcon.class));
    moonColumn.setHeaderValue(new ImageIcon("Moons.gif"));

    // show table

    table.setRowHeight(100);
    add(new JScrollPane(table), BorderLayout.CENTER);
}

From source file:com.sec.ose.osi.ui.frm.main.manage.AddProjectTableModel.java

public void setColumnView(JTable table) {
    table.setShowVerticalLines(false);/*  w  ww . ja va 2  s. c  om*/
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.setColumnSelectionAllowed(false);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
    table.setRowHeight(30);

    JTableHeader header = table.getTableHeader();
    header.setPreferredSize(new java.awt.Dimension(table.getTableHeader().getWidth(), 30));
    header.setFont(new Font("Arial", Font.BOLD, 12));
    header.setReorderingAllowed(false);

    TableColumnModel cm = table.getColumnModel();
    TableColumn col = null;

    searchHeader = new CheckBoxHeader(new CheckboxHeaderItemListener(table, COL_ISSELECT), "");
    col = cm.getColumn(COL_ISSELECT);
    col.setHeaderRenderer(searchHeader);
}

From source file:SortedTableModel.java

public SimpleTableTestMultilineHeader() {
    Container pane = getContentPane();
    pane.setLayout(new BorderLayout());
    TableValues tv = new TableValues();
    SortedTableModel stm = new SortedTableModel(tv);
    stm.sortRows(TableValues.ACCOUNT_BALANCE, true);
    table = new JTable(stm);
    table.setRowSelectionAllowed(false);
    table.setColumnSelectionAllowed(true);
    TableColumnModel tcm = table.getColumnModel();
    TableColumn tc = tcm.getColumn(TableValues.GENDER);
    MultiLineHeaderRenderer mlhr = new MultiLineHeaderRenderer();
    tc = tcm.getColumn(TableValues.ACCOUNT_BALANCE);
    tc.setHeaderRenderer(mlhr);
    renderer = new SortedColumnHeaderRenderer(stm, mlhr);
    int count = tcm.getColumnCount();
    for (int i = 0; i < count; i++) {
        tc = tcm.getColumn(i);//from www.j  a va  2 s . c  om
        tc.setHeaderRenderer(renderer);
    }
    JTableHeaderToolTips jthtt = new JTableHeaderToolTips(table.getColumnModel());
    jthtt.setToolTips(new String[] { "Customer's First Name", "Customer's Last Name",
            "Customer's Date of Birth", "Customer's Account Balance", "Customer's Gender" });
    table.setTableHeader(jthtt);
    JScrollPane jsp = new JScrollPane(table);
    pane.add(jsp, BorderLayout.CENTER);
    addHeaderListener();
}