Search in sources :

Example 71 with JTableHeader

use of javax.swing.table.JTableHeader in project knime-base by knime.

the class PreviewTableContentView method onMouseClickInHeader.

/**
 * {@inheritDoc}
 */
@Override
protected void onMouseClickInHeader(final MouseEvent e) {
    JTableHeader header = getTableHeader();
    // get column in which event occurred
    int column = header.columnAtPoint(e.getPoint());
    if (column < 0) {
        return;
    }
    int modelIndex = convertColumnIndexToModel(column);
    firePropertyChange(PROPERTY_SPEC_CHANGED, null, modelIndex);
}
Also used : JTableHeader(javax.swing.table.JTableHeader)

Example 72 with JTableHeader

use of javax.swing.table.JTableHeader in project knime-base by knime.

the class PMCCNodeView method changeRenderer.

private void changeRenderer(final String renderer) {
    TableContentView tcv = m_tableView.getContentTable();
    tcv.changeRenderer(DoubleCell.TYPE, renderer);
    Component c = m_tableView.getCorner(JScrollPane.UPPER_LEFT_CORNER);
    LegendCornerAll cornerRenderer = null;
    if (c instanceof JTableHeader) {
        JTableHeader corner = (JTableHeader) c;
        TableCellRenderer ren = corner.getDefaultRenderer();
        if (ren instanceof LegendCornerAll) {
            cornerRenderer = (LegendCornerAll) ren;
        }
    }
    if (cornerRenderer != null) {
        if (renderer.equals(ColorRender.DESCRIPTION)) {
            cornerRenderer.setPaintLegend(true);
            // no need to set column header height, done by label UI
            tcv.setColumnWidth(15);
            TableCellRenderer r = m_tableView.getContentTable().getTableHeader().getDefaultRenderer();
            if (r instanceof JLabel) {
                ((JLabel) r).setUI(new VerticalLabelUI());
            }
        } else {
            cornerRenderer.setPaintLegend(false);
            m_tableView.setColumnHeaderViewHeight(16);
            tcv.setColumnWidth(75);
            TableCellRenderer r = m_tableView.getContentTable().getTableHeader().getDefaultRenderer();
            if (r instanceof JLabel) {
                ((JLabel) r).updateUI();
            }
        }
    }
    m_currentRendererID = renderer;
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) JTableHeader(javax.swing.table.JTableHeader) JLabel(javax.swing.JLabel) TableContentView(org.knime.core.node.tableview.TableContentView) Component(java.awt.Component) JComponent(javax.swing.JComponent)

Example 73 with JTableHeader

use of javax.swing.table.JTableHeader in project knetbuilder by Rothamsted.

the class ResultTableUtil method calcColumnWidths.

/**
 * Calculates and sets the width distribution of columns in a given JTable.
 *
 * @param table - JTable
 */
public static void calcColumnWidths(JTable table) {
    // get table header and renderer
    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();
    int rowCount = data.getRowCount();
    int totalWidth = 0;
    // go through all columns
    for (int i = columns.getColumnCount() - 1; i >= 0; --i) {
        // get current column
        TableColumn column = columns.getColumn(i);
        int columnIndex = column.getModelIndex();
        int width = -1;
        // get header for current column
        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);
            // get header width
            width = (int) (c.getPreferredSize().width * 1.1);
        }
        // go through all rows
        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);
            // max of header width and actual component width
            width = Math.max(width, c.getPreferredSize().width);
        }
        if (width > 200)
            width = 200;
        if (width >= 0)
            column.setPreferredWidth(width + margin);
        // setting and summing preferred width of each column
        totalWidth += column.getPreferredWidth();
    }
    // set width to table
    Dimension size = table.getPreferredScrollableViewportSize();
    size.width = totalWidth;
    table.setPreferredScrollableViewportSize(size);
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) JTableHeader(javax.swing.table.JTableHeader) TableColumnModel(javax.swing.table.TableColumnModel) Dimension(java.awt.Dimension) Component(java.awt.Component) TableColumn(javax.swing.table.TableColumn) TableModel(javax.swing.table.TableModel)

Example 74 with JTableHeader

use of javax.swing.table.JTableHeader in project com.revolsys.open by revolsys.

the class JTableUtil method sizeColumnsToFit.

public static void sizeColumnsToFit(final JTable table) {
    Invoke.later(() -> {
        final JTableHeader tableHeader = table.getTableHeader();
        final TableColumnModel columnModel = table.getColumnModel();
        for (int columnIndex = 0; columnIndex < table.getColumnCount(); columnIndex++) {
            final TableColumn column = columnModel.getColumn(columnIndex);
            final String columnName = table.getColumnName(columnIndex);
            final TableCellRenderer headerRenderer = tableHeader.getDefaultRenderer();
            final int headerWidth = headerRenderer.getTableCellRendererComponent(table, columnName, false, false, 0, 0).getPreferredSize().width;
            int maxwidth = headerWidth + 20;
            for (int rowIndex = 0; rowIndex < table.getRowCount(); rowIndex++) {
                final TableCellRenderer cellRenderer = table.getCellRenderer(rowIndex, columnIndex);
                final Object value = table.getValueAt(rowIndex, columnIndex);
                final Component component = cellRenderer.getTableCellRendererComponent(table, value, false, false, rowIndex, columnIndex);
                maxwidth = Math.max(component.getPreferredSize().width, maxwidth);
            }
            column.setPreferredWidth(maxwidth + 5);
        }
    });
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) JTableHeader(javax.swing.table.JTableHeader) TableColumnModel(javax.swing.table.TableColumnModel) Component(java.awt.Component) TableColumn(javax.swing.table.TableColumn)

Example 75 with JTableHeader

use of javax.swing.table.JTableHeader in project com.revolsys.open by revolsys.

the class SingleRecordTableModel method newTable.

public static BaseJTable newTable(final Record object, final boolean editable) {
    final SingleRecordTableModel model = new SingleRecordTableModel(object, editable);
    final BaseJTable table = newTable(model);
    final JTableHeader tableHeader = table.getTableHeader();
    tableHeader.setReorderingAllowed(false);
    return table;
}
Also used : BaseJTable(com.revolsys.swing.table.BaseJTable) JTableHeader(javax.swing.table.JTableHeader)

Aggregations

JTableHeader (javax.swing.table.JTableHeader)240 TableColumn (javax.swing.table.TableColumn)59 MouseEvent (java.awt.event.MouseEvent)56 JTable (javax.swing.JTable)53 TableColumnModel (javax.swing.table.TableColumnModel)45 TableCellRenderer (javax.swing.table.TableCellRenderer)38 Component (java.awt.Component)35 MouseAdapter (java.awt.event.MouseAdapter)32 DefaultTableModel (javax.swing.table.DefaultTableModel)29 DefaultTableCellRenderer (javax.swing.table.DefaultTableCellRenderer)28 Color (java.awt.Color)25 JPanel (javax.swing.JPanel)25 JScrollPane (javax.swing.JScrollPane)25 JLabel (javax.swing.JLabel)23 Point (java.awt.Point)22 Dimension (java.awt.Dimension)20 BorderLayout (java.awt.BorderLayout)18 JComponent (javax.swing.JComponent)17 TableModel (javax.swing.table.TableModel)17 JButton (javax.swing.JButton)14