Example usage for javax.swing.table TableColumn getModelIndex

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

Introduction

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

Prototype

public int getModelIndex() 

Source Link

Document

Returns the model index for this column.

Usage

From source file:edu.ku.brc.stats.StatGroupTable.java

/**
 * Calculates and sets the each column to it preferred size
 * @param table the table to fix ups//from w  w w  .j  ava 2s  .c om
 */
public static void calcColumnWidths(JTable table) {
    JTableHeader header = table.getTableHeader();

    TableCellRenderer defaultHeaderRenderer = null;

    if (header != null) {
        defaultHeaderRenderer = header.getDefaultRenderer();
    }

    TableColumnModel columns = table.getColumnModel();
    TableModel data = table.getModel();

    int margin = columns.getColumnMargin(); // only JDK1.3

    int rowCount = data.getRowCount();

    int totalWidth = 0;

    for (int i = columns.getColumnCount() - 1; i >= 0; --i) {
        TableColumn column = columns.getColumn(i);

        int columnIndex = column.getModelIndex();

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

            width = c.getPreferredSize().width;
        }

        for (int row = rowCount - 1; row >= 0; --row) {
            TableCellRenderer r = table.getCellRenderer(row, i);

            Component c = r.getTableCellRendererComponent(table, data.getValueAt(row, columnIndex), false,
                    false, row, i);

            width = Math.max(width, c.getPreferredSize().width + 10); // adding an arbitray 10 pixels to make it look nicer
        }

        if (width >= 0) {
            column.setPreferredWidth(width + margin); // <1.3: without margin
        }

        totalWidth += column.getPreferredWidth();
    }

    // If you like; This does not make sense for two many columns!
    Dimension size = table.getPreferredScrollableViewportSize();
    //if (totalWidth > size.width)
    {
        size.height = Math.min(size.height, table.getRowHeight() * visibleRows);
        size.width = totalWidth;
        table.setPreferredScrollableViewportSize(size);
    }

}

From source file:edu.ku.brc.specify.utilapps.SetUpBuildDlg.java

/**
 * @param table//  w  w w.j a  v  a2 s.c  o  m
 */
public static void calcColumnWidths(JTable table) {
    JTableHeader header = table.getTableHeader();

    TableCellRenderer defaultHeaderRenderer = null;

    if (header != null) {
        defaultHeaderRenderer = header.getDefaultRenderer();
    }

    TableColumnModel columns = table.getColumnModel();
    TableModel data = table.getModel();

    int margin = columns.getColumnMargin(); // only JDK1.3

    int rowCount = data.getRowCount();

    int totalWidth = 0;

    for (int i = columns.getColumnCount() - 1; i >= 0; --i) {
        TableColumn column = columns.getColumn(i);

        int columnIndex = column.getModelIndex();

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

            width = c.getPreferredSize().width;
        }

        for (int row = rowCount - 1; row >= 0; --row) {
            TableCellRenderer r = table.getCellRenderer(row, i);

            Component c = r.getTableCellRendererComponent(table, data.getValueAt(row, columnIndex), false,
                    false, row, i);

            width = Math.max(width, c.getPreferredSize().width + 10); // adding an arbitray 10 pixels to make it look nicer
        }

        if (width >= 0) {
            column.setPreferredWidth(width + margin); // <1.3: without margin
        } else {
            // ???
        }

        totalWidth += column.getPreferredWidth();
    }
}

From source file:Main.java

public TableColumn findTableColumn(JTable table, int columnModelIndex) {
    Enumeration e = table.getColumnModel().getColumns();
    for (; e.hasMoreElements();) {
        TableColumn col = (TableColumn) e.nextElement();
        if (col.getModelIndex() == columnModelIndex) {
            return col;
        }//from   w ww . j  a  v a2  s .  co  m
    }
    return null;
}

From source file:Main.java

public int toView(JTable table, int mColIndex) {
    for (int c = 0; c < table.getColumnCount(); c++) {
        TableColumn col = table.getColumnModel().getColumn(c);
        if (col.getModelIndex() == mColIndex) {
            return c;
        }//from   w ww . j av a  2  s  .co  m
    }
    return -1;
}

From source file:Main.java

public int compare(Object a, Object b) {
    TableColumn c1 = (TableColumn) a;
    TableColumn c2 = (TableColumn) b;
    if (c1.getModelIndex() < c2.getModelIndex()) {
        return -1;
    } else if (c1.getModelIndex() == c2.getModelIndex()) {
        return 0;
    } else {/*from  w w w  . j  a v a2s .c  o  m*/
        return 1;
    }
}

From source file:ColumnExample.java

public void addColumn(TableColumn tc) {
    super.addColumn(tc);
    int newIndex = sortedIndexOf(tc);
    if (newIndex != tc.getModelIndex()) {
        moveColumn(tc.getModelIndex(), newIndex);
    }//from   w ww. ja v a2  s. co  m
}

From source file:com.github.fritaly.dualcommander.event.ColumnEventHelper.java

@Override
public void mousePressed(MouseEvent e) {
    // capture start of resize
    if (e.getSource() instanceof JTableHeader) {
        final TableColumn column = ((JTableHeader) e.getSource()).getResizingColumn();

        if (column != null) {
            resizing = true;//from  ww w  .  ja  va2 s. c o m
            resizingColumn = column.getModelIndex();
            oldWidth = column.getPreferredWidth();
        } else {
            resizingColumn = -1;
            oldWidth = -1;
        }
    }
}

From source file:com.mirth.connect.client.ui.components.MirthTable.java

private void saveColumnOrder() {
    try {/*from w w w . j a  va2s .co m*/
        if (StringUtils.isNotEmpty(prefix)) {
            columnOrderMap.clear();

            for (TableColumn column : getColumns(true)) {
                columnOrderMap.put((String) column.getHeaderValue(),
                        convertColumnIndexToView(column.getModelIndex()));
            }

            userPreferences.put(prefix + "ColumnOrderMap",
                    ObjectXMLSerializer.getInstance().serialize(columnOrderMap));
        }
    } catch (Exception e) {
    }
}

From source file:com.mirth.connect.client.ui.components.MirthTreeTable.java

public void setMirthTransferHandlerEnabled(boolean enable) {
    setDragEnabled(enable);//  w w w. j  av  a 2  s.  c o  m

    if (transferHandler == null) {
        transferHandler = new TransferHandler() {
            @Override
            public int getSourceActions(JComponent c) {
                return COPY_OR_MOVE;
            }

            @Override
            protected Transferable createTransferable(JComponent c) {
                int row = getSelectedRow();

                List columnValuesList = new ArrayList<Object>();
                for (TableColumn column : getColumns()) {
                    Object value = getValueAt(row, convertColumnIndexToView(column.getModelIndex()));
                    if (value != null) {
                        if (value instanceof Calendar) {
                            Calendar calendar = (GregorianCalendar) value;
                            SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
                            dateFormat.setCalendar(calendar);
                            value = (String) dateFormat.format(calendar.getTime());
                        } else if (value instanceof Long) {
                            value = String.valueOf(value);
                        } else if (value instanceof Status) {
                            value = Status.fromChar(((Status) value).getStatusCode());
                        } else {
                            value = String.valueOf(value);
                        }
                    } else {
                        value = "-";
                    }

                    columnValuesList.add(value);
                }

                return new StringSelection(StringUtils.join(columnValuesList, " "));
            }
        };

        setTransferHandler(transferHandler);
    }
}

From source file:com.mirth.connect.client.ui.components.MirthTreeTable.java

public void saveColumnOrder() {
    try {//from ww  w.ja va2 s.c om
        if (StringUtils.isNotEmpty(prefix)) {
            int metaDataColumnOffset = 0;
            columnOrderMap.clear();

            for (TableColumn column : getColumns()) {
                String columnName = (String) column.getHeaderValue();

                if (metaDataColumns != null && metaDataColumns.contains(columnName)) {
                    metaDataColumnOffset++;
                }

                if (metaDataColumns == null || !metaDataColumns.contains(columnName)) {
                    columnOrderMap.put(columnName,
                            convertColumnIndexToView(column.getModelIndex()) - metaDataColumnOffset);
                }
            }

            // Finally, loop again to store the hidden columns in the map
            for (TableColumn column : getColumns(true)) {
                String columnName = (String) column.getHeaderValue();

                if (!columnOrderMap.keySet().contains(columnName)
                        && (metaDataColumns == null || !metaDataColumns.contains(columnName))) {
                    columnOrderMap.put(columnName, -1);
                }
            }

            userPreferences.put(prefix + "ColumnOrderMap",
                    ObjectXMLSerializer.getInstance().serialize(new HashMap<String, Integer>(columnOrderMap)));
        }
    } catch (Exception e) {
    }
}