Search in sources :

Example 86 with TableColumnModel

use of javax.swing.table.TableColumnModel in project processdash by dtuma.

the class TeamMemberListTable method recreateWeekColumnsToFit.

/**
     * Recreate the columns in the table model, to display data for as many
     * weeks as possible (within the constraints of the width of the table)
     */
private void recreateWeekColumnsToFit() {
    TeamMemberList teamList = getTeamMemberList();
    TableColumnModel tcm = getColumnModel();
    // compute the amount of space allocated to the weekly columns.
    // first, get the width of the entire table
    int tableWidth = getWidth();
    int width = tableWidth;
    // next, subtract out the width of the initial, fixed columns.
    for (int i = COL_WIDTHS.length; i-- > 0; ) width = width - COL_WIDTHS[i];
    // then, subtract out the width of the grippy column.
    width = width - GRIPPY_WIDTH - getIntercellSpacing().width;
    int numWeekColumns = Math.max(0, width / WEEK_COL_WIDTH);
    int extraWidth = width - (numWeekColumns * WEEK_COL_WIDTH);
    TableColumn firstCol = tcm.getColumn(0);
    firstCol.setWidth(COL_WIDTHS[0] + extraWidth);
    int existingNumWeekColumns = teamList.getNumWeekColumns();
    if (existingNumWeekColumns != numWeekColumns) {
        // remove the grippy column.
        removeLastColumn(tcm);
        // remove any weekly columns that are no longer needed
        for (int i = numWeekColumns; i < existingNumWeekColumns; i++) removeLastColumn(tcm);
        // reconfigure the table for the desired number of weekly columns
        teamList.setNumWeekColumns(numWeekColumns);
        // add any weekly columns that are now needed
        int colIdx = tcm.getColumnCount();
        for (int i = existingNumWeekColumns; i < numWeekColumns; i++) tcm.addColumn(createWeekColumn(colIdx++));
        // add a grippy column at the end.
        tcm.addColumn(createGrippyColumn(colIdx));
    }
    repositionHeaderDecorations(tableWidth, numWeekColumns);
}
Also used : TableColumnModel(javax.swing.table.TableColumnModel) TableColumn(javax.swing.table.TableColumn) Point(java.awt.Point)

Example 87 with TableColumnModel

use of javax.swing.table.TableColumnModel in project groovy-core by groovy.

the class TableSorter method addMouseListenerToHeaderInTable.

// There is no-where else to put this.
// Add a mouse listener to the Table to trigger a table sort
// when a column heading is clicked in the JTable.
public void addMouseListenerToHeaderInTable(JTable table) {
    final TableSorter sorter = this;
    final JTable tableView = table;
    tableView.setColumnSelectionAllowed(false);
    MouseAdapter listMouseListener = new MouseAdapter() {

        public void mouseClicked(MouseEvent e) {
            TableColumnModel columnModel = tableView.getColumnModel();
            int viewColumn = columnModel.getColumnIndexAtX(e.getX());
            int column = tableView.convertColumnIndexToModel(viewColumn);
            if (e.getClickCount() == 1 && column != -1) {
                if (lastSortedColumn == column)
                    ascending = !ascending;
                sorter.sortByColumn(column, ascending);
                lastSortedColumn = column;
            }
        }
    };
    JTableHeader th = tableView.getTableHeader();
    th.addMouseListener(listMouseListener);
}
Also used : MouseEvent(java.awt.event.MouseEvent) MouseAdapter(java.awt.event.MouseAdapter) TableColumnModel(javax.swing.table.TableColumnModel) JTableHeader(javax.swing.table.JTableHeader)

Example 88 with TableColumnModel

use of javax.swing.table.TableColumnModel in project smile by haifengl.

the class TableColumnSettings method saveSettings.

private void saveSettings() {
    TableColumnModel columnModel = table.getColumnModel();
    prefs.putInt(id + "-visible-column-count", columnModel.getColumnCount());
    for (int i = 0; i < columnModel.getColumnCount(); i++) {
        TableColumn col = columnModel.getColumn(i);
        int idx = col.getModelIndex();
        int width = col.getWidth();
        prefs.putInt(id + "-column-width-" + idx, width);
        prefs.putInt(id + "-column-order-" + i, idx);
    }
}
Also used : TableColumnModel(javax.swing.table.TableColumnModel) TableColumn(javax.swing.table.TableColumn)

Example 89 with TableColumnModel

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

the class XJTableView method autoresizeColumns.

public void autoresizeColumns() {
    resizeTableColumnsToFitContent(table, 20);
    TableColumnModel model = table.getColumnModel();
    int columnTotalWidth = 0;
    for (int i = 0; i < model.getColumnCount() - 1; i++) {
        columnTotalWidth += model.getColumn(i).getPreferredWidth();
    }
    // Note: this offset should be UI dependent
    int offset;
    if (getVerticalScrollBar().isVisible()) {
        if (XJSystem.isWindows()) {
            offset = 20;
        } else {
            offset = 20;
        }
    } else {
        if (XJSystem.isWindows()) {
            offset = 3;
        } else {
            offset = 4;
        }
    }
    int spWidth = getWidth();
    if (spWidth == 0) {
        spWidth = getPreferredSize().width;
    }
    if (model.getColumnCount() > 0) {
        TableColumn c = model.getColumn(model.getColumnCount() - 1);
        int prefWidth = c.getPreferredWidth();
        c.setPreferredWidth(Math.max(spWidth - columnTotalWidth - offset, prefWidth));
    }
}
Also used : TableColumnModel(javax.swing.table.TableColumnModel) TableColumn(javax.swing.table.TableColumn)

Example 90 with TableColumnModel

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

the class GroupableTableHeaderUI method getHeaderHeight.

public int getHeaderHeight() {
    int height = 0;
    TableColumnModel columnModel = header.getColumnModel();
    for (int column = 0; column < columnModel.getColumnCount(); column++) {
        TableColumn aColumn = columnModel.getColumn(column);
        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;
                }
            };
        }
        Component comp = renderer.getTableCellRendererComponent(header.getTable(), aColumn.getHeaderValue(), false, false, -1, column);
        int cHeight = comp.getPreferredSize().height;
        Enumeration<ColumnGroup> enumeration = ((GroupableTableHeader) header).getColumnGroups(aColumn);
        if (enumeration != null) {
            while (enumeration.hasMoreElements()) {
                ColumnGroup cGroup = (ColumnGroup) enumeration.nextElement();
                cHeight += cGroup.getSize(header.getTable()).height;
            }
        }
        height = Math.max(height, cHeight);
    }
    height = Math.max(height, m_height);
    return height;
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) TableColumnModel(javax.swing.table.TableColumnModel) JTableHeader(javax.swing.table.JTableHeader) TableColumn(javax.swing.table.TableColumn) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) JTable(javax.swing.JTable) JComponent(javax.swing.JComponent) Component(java.awt.Component)

Aggregations

TableColumnModel (javax.swing.table.TableColumnModel)168 TableColumn (javax.swing.table.TableColumn)66 JTable (javax.swing.JTable)34 JScrollPane (javax.swing.JScrollPane)31 JButton (javax.swing.JButton)30 BoxLayout (javax.swing.BoxLayout)24 TableCellEditor (javax.swing.table.TableCellEditor)23 ButtonEditor (jmri.util.table.ButtonEditor)23 ButtonRenderer (jmri.util.table.ButtonRenderer)23 JLabel (javax.swing.JLabel)22 JPanel (javax.swing.JPanel)22 ActionEvent (java.awt.event.ActionEvent)20 ActionListener (java.awt.event.ActionListener)16 FlowLayout (java.awt.FlowLayout)15 Border (javax.swing.border.Border)13 Iterator (java.util.Iterator)12 Set (java.util.Set)12 JTableHeader (javax.swing.table.JTableHeader)12 TableCellRenderer (javax.swing.table.TableCellRenderer)12 Container (java.awt.Container)10