Example usage for javax.swing JTable addColumn

List of usage examples for javax.swing JTable addColumn

Introduction

In this page you can find the example usage for javax.swing JTable addColumn.

Prototype

public void addColumn(TableColumn aColumn) 

Source Link

Document

Appends aColumn to the end of the array of columns held by this JTable's column model.

Usage

From source file:Main.java

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

    model = (DefaultTableModel) table.getModel();
    TableColumn col = new TableColumn(model.getColumnCount());

    // Ensure that auto-create is off
    if (table.getAutoCreateColumnsFromModel()) {
        throw new IllegalStateException();
    }//from  w  ww .j a  v  a  2 s  .  com
    col.setHeaderValue("Col3");
    table.addColumn(col);
    model.addColumn("Col3", new Object[] { "v3" });

}

From source file:org.yccheok.jstock.gui.JTableUtilities.java

public static void insertTableColumnFromModel(JTable jTable, Object value, int clickedColumnIndex) {
    boolean isVisible = true;

    try {//  w  ww .j a v  a 2  s  .  c  om

        TableColumn tableColumn = jTable.getColumn(value);

    }

    catch (java.lang.IllegalArgumentException exp) {

        isVisible = false;

    }

    if (isVisible)
        return;

    TableModel tableModel = jTable.getModel();

    final int modelIndex = getModelColumnIndex(jTable, value);

    Class c = tableModel.getColumnClass(modelIndex);

    TableColumn tableColumn = new javax.swing.table.TableColumn(modelIndex, 0, jTable.getDefaultRenderer(c),
            jTable.getDefaultEditor(c));

    jTable.addColumn(tableColumn);

    makeTableColumnWidthFit(jTable, jTable.getColumnCount() - 1, 5);

    // If we right clicked on the 3rd column, and select a new column, we
    // would like the new column to be inserted into 4th column. Note that,
    // clickedColumnIndex will be < 0, if we right clicked on empty area.
    if (clickedColumnIndex < 0) {
        // Have it in the last column when we right clicked on empty area.
        jTable.moveColumn(jTable.getColumnCount() - 1, jTable.getColumnCount() - 1);
    } else {
        // +1, as we want our newly inserted column to be at the right of
        // clicked column.
        jTable.moveColumn(jTable.getColumnCount() - 1,
                Math.min(jTable.getColumnCount() - 1, clickedColumnIndex + 1));
    }
}