Search in sources :

Example 1 with TableCellRenderer

use of javax.swing.table.TableCellRenderer in project antlrworks by antlr.

the class XJTableView method resizeColumnToFitContent.

public static void resizeColumnToFitContent(JTable table, int columnIndex, int margin) {
    TableColumn column = table.getColumnModel().getColumn(columnIndex);
    TableCellRenderer renderer = column.getHeaderRenderer();
    if (renderer == null) {
        renderer = table.getTableHeader().getDefaultRenderer();
    }
    Component c = renderer.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, 0, 0);
    int maxWidth = c.getPreferredSize().width;
    for (int row = 0; row < table.getRowCount(); row++) {
        renderer = table.getCellRenderer(row, columnIndex);
        c = renderer.getTableCellRendererComponent(table, table.getValueAt(row, columnIndex), false, false, row, columnIndex);
        maxWidth = Math.max(maxWidth, c.getPreferredSize().width);
    }
    column.setPreferredWidth(maxWidth + margin);
}
Also used : DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) TableCellRenderer(javax.swing.table.TableCellRenderer) TableColumn(javax.swing.table.TableColumn)

Example 2 with TableCellRenderer

use of javax.swing.table.TableCellRenderer in project druid by alibaba.

the class GroupableTableHeaderUI method paintCell.

private void paintCell(Graphics g, Rectangle cellRect, ColumnGroup cGroup) {
    TableCellRenderer renderer = cGroup.getHeaderRenderer();
    //
    if (renderer == null) {
        renderer = new DefaultTableCellRenderer() {

            private static final long serialVersionUID = 1L;

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                JTableHeader header = table.getTableHeader();
                if (header != null) {
                    setForeground(header.getForeground());
                    setBackground(header.getBackground());
                    setFont(header.getFont());
                }
                setHorizontalAlignment(JLabel.CENTER);
                setText((value == null) ? "" : value.toString());
                setBorder(UIManager.getBorder("TableHeader.cellBorder"));
                return this;
            }
        };
    }
    String headerValue = cGroup.getHeaderValue().toString();
    Component component = renderer.getTableCellRendererComponent(header.getTable(), headerValue, false, false, -1, -1);
    rendererPane.add(component);
    rendererPane.paintComponent(g, component, header, cellRect.x, cellRect.y, cellRect.width, cellRect.height, true);
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) JTable(javax.swing.JTable) JTableHeader(javax.swing.table.JTableHeader) JComponent(javax.swing.JComponent) Component(java.awt.Component) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer)

Example 3 with TableCellRenderer

use of javax.swing.table.TableCellRenderer in project druid by alibaba.

the class GroupableTableHeaderUI method paintCell.

private void paintCell(Graphics g, Rectangle cellRect, int columnIndex) {
    TableColumn aColumn = header.getColumnModel().getColumn(columnIndex);
    TableCellRenderer renderer = aColumn.getHeaderRenderer();
    //
    if (renderer == null) {
        renderer = new DefaultTableCellRenderer() {

            private static final long serialVersionUID = 1L;

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                JTableHeader header = table.getTableHeader();
                if (header != null) {
                    setForeground(header.getForeground());
                    setBackground(header.getBackground());
                    setFont(header.getFont());
                }
                setHorizontalAlignment(JLabel.CENTER);
                setText((value == null) ? "" : value.toString());
                setBorder(UIManager.getBorder("TableHeader.cellBorder"));
                return this;
            }
        };
    }
    String headerValue = aColumn.getHeaderValue().toString();
    Component component = renderer.getTableCellRendererComponent(header.getTable(), headerValue, false, false, -1, columnIndex);
    rendererPane.add(component);
    rendererPane.paintComponent(g, component, header, cellRect.x, cellRect.y, cellRect.width, cellRect.height, true);
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) JTable(javax.swing.JTable) JTableHeader(javax.swing.table.JTableHeader) JComponent(javax.swing.JComponent) Component(java.awt.Component) TableColumn(javax.swing.table.TableColumn) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer)

Example 4 with TableCellRenderer

use of javax.swing.table.TableCellRenderer in project gitblit by gitblit.

the class Utils method newTable.

public static JTable newTable(TableModel model, String datePattern, final RowRenderer rowRenderer) {
    JTable table;
    if (rowRenderer == null) {
        table = new JTable(model);
    } else {
        table = new JTable(model) {

            @Override
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                Component c = super.prepareRenderer(renderer, row, column);
                boolean isSelected = isCellSelected(row, column);
                rowRenderer.prepareRow(c, isSelected, row, column);
                return c;
            }
        };
    }
    table.setRowHeight(table.getFont().getSize() + 8);
    table.setCellSelectionEnabled(false);
    table.setRowSelectionAllowed(true);
    table.getTableHeader().setReorderingAllowed(false);
    table.setGridColor(new Color(0xd9d9d9));
    table.setBackground(Color.white);
    table.setDefaultRenderer(Date.class, new DateCellRenderer(datePattern, Color.orange.darker()));
    return table;
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) JTable(javax.swing.JTable) Color(java.awt.Color) Component(java.awt.Component)

Example 5 with TableCellRenderer

use of javax.swing.table.TableCellRenderer in project gitblit by gitblit.

the class Utils method packColumn.

// Sets the preferred width of the visible column specified by vColIndex.
// The column will be just wide enough to show the column head and the
// widest cell in the column. margin pixels are added to the left and right
// (resulting in an additional width of 2*margin pixels).
private static void packColumn(JTable table, int vColIndex, int margin) {
    DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
    TableColumn col = colModel.getColumn(vColIndex);
    int width = 0;
    // Get width of column header
    TableCellRenderer renderer = col.getHeaderRenderer();
    if (renderer == null) {
        renderer = table.getTableHeader().getDefaultRenderer();
    }
    Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
    width = comp.getPreferredSize().width;
    // Get maximum width of column data
    for (int r = 0; r < table.getRowCount(); r++) {
        renderer = table.getCellRenderer(r, vColIndex);
        comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false, r, vColIndex);
        width = Math.max(width, comp.getPreferredSize().width);
    }
    // Add margin
    width += 2 * margin;
    // Set the width
    col.setPreferredWidth(width);
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) DefaultTableColumnModel(javax.swing.table.DefaultTableColumnModel) Component(java.awt.Component) TableColumn(javax.swing.table.TableColumn)

Aggregations

TableCellRenderer (javax.swing.table.TableCellRenderer)85 Component (java.awt.Component)30 TableColumn (javax.swing.table.TableColumn)30 DefaultTableCellRenderer (javax.swing.table.DefaultTableCellRenderer)20 JComponent (javax.swing.JComponent)12 JTable (javax.swing.JTable)12 JTableHeader (javax.swing.table.JTableHeader)12 TableColumnModel (javax.swing.table.TableColumnModel)12 MouseEvent (java.awt.event.MouseEvent)10 ColumnInfo (com.intellij.util.ui.ColumnInfo)8 JLabel (javax.swing.JLabel)8 DefaultTableColumnModel (javax.swing.table.DefaultTableColumnModel)8 JButton (javax.swing.JButton)7 TableCellEditor (javax.swing.table.TableCellEditor)7 ActionEvent (java.awt.event.ActionEvent)6 MouseAdapter (java.awt.event.MouseAdapter)6 List (java.util.List)6 TableModel (javax.swing.table.TableModel)6 JTextField (javax.swing.JTextField)5 ActionListener (java.awt.event.ActionListener)4