Example usage for javax.swing JTable getColumnModel

List of usage examples for javax.swing JTable getColumnModel

Introduction

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

Prototype

public TableColumnModel getColumnModel() 

Source Link

Document

Returns the TableColumnModel that contains all column information of this table.

Usage

From source file:MainClass.java

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

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(rows, headers);

    TableColumnModelListener tableColumnModelListener = new TableColumnModelListener() {
        public void columnAdded(TableColumnModelEvent e) {
            System.out.println("Added");
        }/* www. j ava  2s .c o  m*/

        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);

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

From source file:Main.java

public static void setPrefColumnWidth(JTable table, int index, int width) {
    table.getColumnModel().getColumn(index).setMinWidth(width);
    table.getColumnModel().getColumn(index).setPreferredWidth(width);
}

From source file:Main.java

public static void setColumnWidth(JTable table, int index, int width) {
    table.getColumnModel().getColumn(index).setMinWidth(width);
    table.getColumnModel().getColumn(index).setMaxWidth(width);
    table.getColumnModel().getColumn(index).setPreferredWidth(width);
}

From source file:Main.java

public static String headerString(JTable t) {
    return headerString(t.getColumnModel());
}

From source file:Main.java

public static int getPreferredTableWidth(final JTable table) {
    TableColumnModel columnModel = table.getColumnModel();
    int width = 0;
    for (int col = 0; col < columnModel.getColumnCount(); col++) {
        width += columnModel.getColumn(col).getPreferredWidth();
        if (col != 0) {
            width += columnModel.getColumnMargin();
        }//from   w  w  w.j  a v  a2s  . c  o m
    }
    return width;
}

From source file:Main.java

public static void setMinimumColumnWidths(final JTable table, final int... widths) {
    TableColumnModel columnModel = table.getColumnModel();
    for (int col = 0; col < widths.length; col++) {
        columnModel.getColumn(col).setMinWidth(widths[col]);
    }/*  w  ww  . j  a v  a 2s.  c om*/
}

From source file:Main.java

public static void setMaxnimumColumnWidths(final JTable table, final int... widths) {
    TableColumnModel columnModel = table.getColumnModel();
    for (int col = 0; col < widths.length; col++) {
        columnModel.getColumn(col).setMaxWidth(widths[col]);
    }//  ww w  .  j  a v a2 s.  co m
}

From source file:Main.java

public static void setPreferredColumnWidths(final JTable table, final int... widths) {
    TableColumnModel columnModel = table.getColumnModel();
    for (int col = 0; col < widths.length; col++) {
        columnModel.getColumn(col).setPreferredWidth(widths[col]);
    }//from   w w  w. j a v  a2 s  . co m
}

From source file:Main.java

public static void insertComponentInColumn(JTable jTable, int col, JComponent jComponent) {
    TableColumn sportColumn = jTable.getColumnModel().getColumn(col);
    if (jComponent instanceof JComboBox) {
        sportColumn.setCellEditor(new DefaultCellEditor((JComboBox) jComponent));
    } else if (jComponent instanceof JCheckBox) {
        sportColumn.setCellEditor(new DefaultCellEditor((JCheckBox) jComponent));
    } else if (jComponent instanceof JTextField) {
        sportColumn.setCellEditor(new DefaultCellEditor((JTextField) jComponent));
    }/* w  w  w. j  a v  a  2 s.c  o  m*/
}

From source file:Main.java

public static void sizeWidthToFitData(JTable table, int vc, int buf) {
    TableColumn tc = table.getColumnModel().getColumn(vc);

    int max = table.getTableHeader().getDefaultRenderer()
            .getTableCellRendererComponent(table, tc.getHeaderValue(), false, false, 0, vc)
            .getPreferredSize().width;// www.  j ava2s. co  m

    int vrows = table.getRowCount();
    for (int i = 0; i < vrows; i++) {
        TableCellRenderer r = table.getCellRenderer(i, vc);
        Object value = table.getValueAt(i, vc);
        Component c = r.getTableCellRendererComponent(table, value, false, false, i, vc);
        int w = c.getPreferredSize().width;
        if (max < w) {
            max = w;
        }
    }

    tc.setPreferredWidth(max + buf);
}