Search in sources :

Example 41 with TableColumnModel

use of javax.swing.table.TableColumnModel in project intellij-community by JetBrains.

the class BaseTableView method restore.

public static void restore(final Storage storage, final JTable table) {
    final TableColumnModel columnModel = table.getTableHeader().getColumnModel();
    int index = 0;
    final ArrayList<String> columnIndices = new ArrayList<>();
    while (true) {
        final String order = storage.get(orderPropertyName(index));
        if (order == null)
            break;
        columnIndices.add(order);
        index++;
        if (index == table.getColumnCount())
            break;
    }
    index = 0;
    for (final String columnIndex : columnIndices) {
        final int modelColumnIndex = indexbyModelIndex(columnModel, Integer.parseInt(columnIndex));
        if (modelColumnIndex > 0 && modelColumnIndex < columnModel.getColumnCount()) {
            columnModel.moveColumn(modelColumnIndex, index);
        }
        index++;
    }
    for (int i = 0; i < columnIndices.size(); i++) {
        final String width = storage.get(widthPropertyName(i));
        if (width != null && width.length() > 0) {
            try {
                columnModel.getColumn(i).setPreferredWidth(Integer.parseInt(width));
            } catch (NumberFormatException e) {
                LOG.error("Bad width: " + width + " at column: " + i + " from: " + storage + " actual columns count: " + columnModel.getColumnCount() + " info count: " + columnIndices.size(), e);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) TableColumnModel(javax.swing.table.TableColumnModel)

Example 42 with TableColumnModel

use of javax.swing.table.TableColumnModel in project intellij-community by JetBrains.

the class BaseTableView method store.

public static void store(final Storage storage, final JTable table) {
    final TableColumnModel model = table.getTableHeader().getColumnModel();
    final int columnCount = model.getColumnCount();
    final boolean[] storedColumns = new boolean[columnCount];
    Arrays.fill(storedColumns, false);
    for (int i = 0; i < columnCount; i++) {
        final TableColumn column = model.getColumn(i);
        storage.put(widthPropertyName(i), String.valueOf(column.getWidth()));
        final int modelIndex = column.getModelIndex();
        storage.put(orderPropertyName(i), String.valueOf(modelIndex));
        if (storedColumns[modelIndex]) {
            LOG.error("columnCount: " + columnCount + " current: " + i + " modelINdex: " + modelIndex);
        }
        storedColumns[modelIndex] = true;
    }
}
Also used : TableColumnModel(javax.swing.table.TableColumnModel) TableColumn(javax.swing.table.TableColumn)

Example 43 with TableColumnModel

use of javax.swing.table.TableColumnModel in project android by JetBrains.

the class TranslationsEditorTest method setModel.

@Test
public void setModel() {
    StringResourceTable table = (StringResourceTable) myTranslationsEditor.getTable().target();
    OptionalInt optionalWidth = table.getKeyColumnPreferredWidth();
    TableColumnModel model = table.getColumnModel();
    assertTrue(optionalWidth.isPresent());
    assertEquals(optionalWidth.getAsInt(), model.getColumn(KEY_COLUMN).getPreferredWidth());
    optionalWidth = table.getDefaultValueAndLocaleColumnPreferredWidths();
    int width = optionalWidth.getAsInt();
    assertTrue(optionalWidth.isPresent());
    IntStream.range(DEFAULT_VALUE_COLUMN, table.getColumnCount()).mapToObj(model::getColumn).forEach(column -> assertEquals(width, column.getPreferredWidth()));
}
Also used : TableColumnModel(javax.swing.table.TableColumnModel) OptionalInt(java.util.OptionalInt) StringResourceTable(com.android.tools.idea.editors.strings.table.StringResourceTable) Test(org.junit.Test)

Example 44 with TableColumnModel

use of javax.swing.table.TableColumnModel in project android by JetBrains.

the class SdkUpdaterConfigPanel method resizeColumnsToFit.

/**
   * Helper to size a table's columns to fit their normal contents.
   */
protected static void resizeColumnsToFit(JTable table) {
    TableColumnModel columnModel = table.getColumnModel();
    for (int column = 1; column < table.getColumnCount(); column++) {
        int width = 50;
        for (int row = 0; row < table.getRowCount(); row++) {
            TableCellRenderer renderer = table.getCellRenderer(row, column);
            Component comp = table.prepareRenderer(renderer, row, column);
            width = Math.max(comp.getPreferredSize().width + 1, width);
        }
        columnModel.getColumn(column).setPreferredWidth(width);
    }
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) TableColumnModel(javax.swing.table.TableColumnModel)

Example 45 with TableColumnModel

use of javax.swing.table.TableColumnModel in project JMRI by JMRI.

the class LRouteTableAction method makeColumns.

/*
     * Utility for addPressed
     */
JScrollPane makeColumns(JTable table, JComboBox<String> box, boolean specialBox) {
    table.setRowSelectionAllowed(false);
    //table.setPreferredScrollableViewportSize(new java.awt.Dimension(250,450));
    TableColumnModel columnModel = table.getColumnModel();
    TableColumn sNameColumnT = columnModel.getColumn(RouteElementModel.SNAME_COLUMN);
    sNameColumnT.setResizable(true);
    sNameColumnT.setMinWidth(75);
    //sNameColumnT.setMaxWidth(110);
    TableColumn uNameColumnT = columnModel.getColumn(RouteElementModel.UNAME_COLUMN);
    uNameColumnT.setResizable(true);
    uNameColumnT.setMinWidth(75);
    //uNameColumnT.setMaxWidth(260);
    TableColumn typeColumnT = columnModel.getColumn(RouteElementModel.TYPE_COLUMN);
    typeColumnT.setResizable(true);
    typeColumnT.setMinWidth(50);
    //typeColumnT.setMaxWidth(110);
    TableColumn includeColumnT = columnModel.getColumn(RouteElementModel.INCLUDE_COLUMN);
    includeColumnT.setResizable(false);
    includeColumnT.setMinWidth(30);
    includeColumnT.setMaxWidth(60);
    TableColumn stateColumnT = columnModel.getColumn(RouteElementModel.STATE_COLUMN);
    if (specialBox) {
        box = new JComboBox<>();
        stateColumnT.setCellEditor(new ComboBoxCellEditor(box));
    } else {
        stateColumnT.setCellEditor(new DefaultCellEditor(box));
    }
    stateColumnT.setResizable(false);
    stateColumnT.setMinWidth(75);
    return new JScrollPane(table);
}
Also used : JScrollPane(javax.swing.JScrollPane) TableColumnModel(javax.swing.table.TableColumnModel) TableColumn(javax.swing.table.TableColumn) DefaultCellEditor(javax.swing.DefaultCellEditor)

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