Search in sources :

Example 1 with TableColumn

use of javax.swing.table.TableColumn 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 TableColumn

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

the class DruidSqlDetailFrame method addTable.

/**
     * 将表格添加到contentPanel对象内部。
     * 
     * @param contentPanel JPanel对象
     * @param 当前表格的标题
     * @param data 当前表格的数据
     */
private void addTable(JPanel contentPanel, String title, ArrayList<LinkedHashMap<String, Object>> data) {
    final JPanel content1 = new JPanel();
    content1.setLayout(new BorderLayout());
    content1.setBorder((TitledBorder) BorderFactory.createTitledBorder(title));
    contentPanel.add(content1);
    ColumnData colData = TableDataProcessor.row2col(data);
    JTable table = new JTable();
    DruidTableModel tableModel = new DruidTableModel(colData.getData());
    table.setModel(tableModel);
    TableColumn col = table.getColumnModel().getColumn(0);
    col.setCellRenderer(new DruidTableCellRenderer());
    final JTableHeader header1 = table.getTableHeader();
    content1.add(header1, BorderLayout.NORTH);
    content1.add(table);
}
Also used : JPanel(javax.swing.JPanel) BorderLayout(java.awt.BorderLayout) JTable(javax.swing.JTable) JTableHeader(javax.swing.table.JTableHeader) ColumnData(com.alibaba.druid.support.jconsole.util.TableDataProcessor.ColumnData) DruidTableModel(com.alibaba.druid.support.jconsole.model.DruidTableModel) DruidTableCellRenderer(com.alibaba.druid.support.jconsole.model.DruidTableCellRenderer) TableColumn(javax.swing.table.TableColumn)

Example 3 with TableColumn

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

the class ColumnGroup method getSize.

/**
     * 取得合并后的单元格的大小,这个方法需要计算,首先 是取得一个没有合并的最小单元格的JTableHeader 的大小
     * 通过Renderer取得组件
     * 
     * @return
     */
public Dimension getSize(JTable table) {
    Component comp = renderer.getTableCellRendererComponent(table, getHeaderValue(), false, false, -1, -1);
    int height = comp.getPreferredSize().height;
    int width = 0;
    // 宽度需要计算合并的还要加上间隙
    Enumeration<Object> enumeration = vector.elements();
    while (enumeration.hasMoreElements()) {
        Object obj = enumeration.nextElement();
        if (obj instanceof TableColumn) {
            TableColumn aColumn = (TableColumn) obj;
            width += aColumn.getWidth();
            width += margin;
        } else {
            width += ((ColumnGroup) obj).getSize(table).width;
        }
    }
    return new Dimension(width, height);
}
Also used : Dimension(java.awt.Dimension) Component(java.awt.Component) TableColumn(javax.swing.table.TableColumn)

Example 4 with TableColumn

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

the class GroupableTableHeaderUI method getPreferredSize.

@Override
public Dimension getPreferredSize(JComponent c) {
    long width = 0;
    Enumeration<TableColumn> enumeration = header.getColumnModel().getColumns();
    while (enumeration.hasMoreElements()) {
        TableColumn aColumn = enumeration.nextElement();
        width = width + aColumn.getWidth();
    }
    return createHeaderSize(width);
}
Also used : TableColumn(javax.swing.table.TableColumn)

Example 5 with TableColumn

use of javax.swing.table.TableColumn 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)

Aggregations

TableColumn (javax.swing.table.TableColumn)215 TableColumnModel (javax.swing.table.TableColumnModel)66 JTable (javax.swing.JTable)44 JScrollPane (javax.swing.JScrollPane)37 JPanel (javax.swing.JPanel)31 TableCellRenderer (javax.swing.table.TableCellRenderer)30 BoxLayout (javax.swing.BoxLayout)25 JLabel (javax.swing.JLabel)24 Component (java.awt.Component)21 ActionEvent (java.awt.event.ActionEvent)20 FlowLayout (java.awt.FlowLayout)16 ActionListener (java.awt.event.ActionListener)16 DefaultCellEditor (javax.swing.DefaultCellEditor)16 JButton (javax.swing.JButton)16 DefaultTableCellRenderer (javax.swing.table.DefaultTableCellRenderer)15 JComboBox (javax.swing.JComboBox)14 Dimension (java.awt.Dimension)13 DefaultTableModel (javax.swing.table.DefaultTableModel)12 JTableHeader (javax.swing.table.JTableHeader)12 XTableColumnModel (jmri.util.swing.XTableColumnModel)12