Example usage for javax.swing JTable getColumnCount

List of usage examples for javax.swing JTable getColumnCount

Introduction

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

Prototype

@BeanProperty(bound = false)
public int getColumnCount() 

Source Link

Document

Returns the number of columns in the column model.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int rows = 10;
    int cols = 5;
    JTable table = new JTable(rows, cols);

    table.moveColumn(table.getColumnCount() - 1, 0);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object[][] cellData = { { "1-1", "1-2" }, { "2-1", "2-2" } };
    String[] columnNames = { "col1", "col2" };

    JTable table = new JTable(cellData, columnNames);

    int rows = table.getRowCount();
    int cols = table.getColumnCount();

    System.out.println(rows);/*w  w  w.j  a  v  a  2s  .  c  o  m*/
    System.out.println(cols);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int rows = 3;
    int cols = 3;
    JTable table = new JTable(rows, cols);

    table.getTableHeader().setReorderingAllowed(false);

    table.moveColumn(table.getColumnCount() - 1, 0);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int rows = 10;
    int cols = 5;
    JTable table = new JTable(rows, cols);
    JTableHeader header = table.getTableHeader();

    ColumnHeaderToolTips tips = new ColumnHeaderToolTips();
    for (int c = 0; c < table.getColumnCount(); c++) {
        TableColumn col = table.getColumnModel().getColumn(c);
        tips.setToolTip(col, "Col " + c);
    }//from www. ja  v  a 2s  . c o m
    header.addMouseMotionListener(tips);
}

From source file:Main.java

public static void setTableColumnsAlignment(int[] colAlignments, JTable table) {
    for (int i = 0; i < table.getColumnCount(); i++) {
        if (colAlignments[i] == JLabel.LEFT)
            continue;

        TableColumn col = table.getColumn(table.getColumnName(i));
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(colAlignments[i]);
        col.setCellRenderer(renderer);//from  www.  ja  v a 2  s .  c  o  m
    }
}

From source file:Main.java

public static void adjustColumnWidth(JTable table, int buf) {
    int columncount = table.getColumnCount();
    for (int i = 0; i < columncount; i++) {
        sizeWidthToFitData(table, i, buf);
    }//w w w.  j a  v a  2  s  .co m
}

From source file:Main.java

/**
 * Pack all columns. Sets the preferred width of all columns so they will be
 * just wide enough to show the column head and the widest cell in the
 * column + {@code margin} each side.//from  w w  w. j  a v a 2s . c om
 * 
 * NB: If a table has more than 100 rows, then only the width of the column
 * header will be checked and not the widest cell value. Otherwise it could
 * take too long.
 * 
 * @param table
 *            table with columns to pack
 * @param margin
 *            margin to leave at each side of each column (resulting in an
 *            additional width of 2*margin pixels).
 */
public static void packAllColumns(JTable table, int margin) {
    for (int c = 0; c < table.getColumnCount(); c++) {
        packColumn(table, c, margin);
    }
}

From source file:Main.java

/**
 * Resizes the columns to match content while keeping the table the same
 * size. This means that the last column may be larger than the content.
 *//*from w  w w  .  ja v  a 2 s  .com*/
public static void resizeTableColumns(JTable table) {
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    int totalWidth = 0;
    for (int i = 0; i < table.getColumnCount(); i++) {
        DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
        TableColumn col = colModel.getColumn(i);

        if (i == table.getColumnCount() - 1) {
            col.setPreferredWidth(table.getWidth() - totalWidth);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
            return;
        }

        int width = 0;
        TableCellRenderer renderer = col.getHeaderRenderer();
        if (renderer == null) {
            renderer = table.getTableHeader().getDefaultRenderer();
        }
        Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, -1,
                i);
        width = Math.max(width, comp.getPreferredSize().width);
        for (int r = 0; r < table.getRowCount(); r++) {
            renderer = table.getCellRenderer(r, i);
            comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, i), false, false, r, i);
            width = Math.max(width, comp.getPreferredSize().width);
        }
        totalWidth += width + 2;
        col.setPreferredWidth(width + 2);
    }
}

From source file:Main.java

/**
 *  Calculates the optimal width for the header of the given table. The
 *  calculation is based on the preferred width of the header renderer.
 *
 *  @param table    the table to calculate the column width
 *  @param col      the column to calculate the widths
 *  @return         the width, -1 if error
 *//*  w  w w .  java  2  s . c  om*/
public static int calcHeaderWidth(JTable table, int col) {
    if (table == null)
        return -1;

    if (col < 0 || col > table.getColumnCount()) {
        System.out.println("invalid col " + col);
        return -1;
    }

    JTableHeader header = table.getTableHeader();
    TableCellRenderer defaultHeaderRenderer = null;
    if (header != null)
        defaultHeaderRenderer = header.getDefaultRenderer();
    TableColumnModel columns = table.getColumnModel();
    TableModel data = table.getModel();
    TableColumn column = columns.getColumn(col);
    int width = -1;
    TableCellRenderer h = column.getHeaderRenderer();
    if (h == null)
        h = defaultHeaderRenderer;
    if (h != null) {
        // Not explicitly impossible
        Component c = h.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1, col);
        width = c.getPreferredSize().width + 5;
    }

    return width;
}

From source file:Main.java

/**
 * Packs all table rows to their preferred height.
 *
 * @param table table to process/*  ww  w .ja  va  2s .co m*/
 */
public static void packRowHeights(final JTable table) {
    for (int row = 0; row < table.getRowCount(); row++) {
        int maxHeight = 0;
        for (int column = 0; column < table.getColumnCount(); column++) {
            final TableCellRenderer cellRenderer = table.getCellRenderer(row, column);
            final Object valueAt = table.getValueAt(row, column);
            final Component renderer = cellRenderer.getTableCellRendererComponent(table, valueAt, false, false,
                    row, column);
            final int heightPreferable = renderer != null ? renderer.getPreferredSize().height : 0;
            maxHeight = Math.max(heightPreferable, maxHeight);
        }
        table.setRowHeight(row, maxHeight);
    }
}