Example usage for javax.swing JTable convertColumnIndexToModel

List of usage examples for javax.swing JTable convertColumnIndexToModel

Introduction

In this page you can find the example usage for javax.swing JTable convertColumnIndexToModel.

Prototype

public int convertColumnIndexToModel(int viewColumnIndex) 

Source Link

Document

Maps the index of the column in the view at viewColumnIndex to the index of the column in the table model.

Usage

From source file:Main.java

/**
 *
 * @param table/*from www  . ja v  a  2  s . co m*/
 * @param row
 * @param column
 * @param p
 * @return
 */
public static boolean pointOutsidePrefSize(JTable table, int row, int column, Point p) {
    if ((table.convertColumnIndexToModel(column) != 0) || (row == -1)) {
        return true;
    }

    TableCellRenderer tcr = table.getCellRenderer(row, column);
    Object value = table.getValueAt(row, column);
    Component cell = tcr.getTableCellRendererComponent(table, value, false, false, row, column);
    Dimension itemSize = cell.getPreferredSize();
    Rectangle cellBounds = table.getCellRect(row, column, false);
    cellBounds.width = itemSize.width;
    cellBounds.height = itemSize.height;

    // See if coords are inside
    // ASSUME: mouse x,y will never be < cell's x,y
    assert ((p.x >= cellBounds.x) && (p.y >= cellBounds.y));

    if ((p.x > (cellBounds.x + cellBounds.width)) || (p.y > (cellBounds.y + cellBounds.height))) {
        return true;
    }

    return false;
}

From source file:com.sshtools.common.ui.PreferencesStore.java

/**
 *
 *
 * @param table//  ww  w.j  a v a 2 s  .  c om
 * @param pref
 */
public static void saveTableMetrics(JTable table, String pref) {
    for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
        int w = table.getColumnModel().getColumn(i).getWidth();
        put(pref + ".column." + i + ".width", String.valueOf(w));
        put(pref + ".column." + i + ".position", String.valueOf(table.convertColumnIndexToModel(i)));
    }
}

From source file:SetValueAtToSetValue.java

public SetValueAtToSetValue() {
    final AbstractTableModel model = new MyModel();
    final JTable table = new JTable(model);
    getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
    model.setValueAt(new Integer(1), 0, 0);

    JButton button = new JButton("Increment selected cell");
    getContentPane().add(button, BorderLayout.SOUTH);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = table.getSelectedRow();
            int column = table.convertColumnIndexToModel(table.getSelectedColumn());
            int currentValue = ((Integer) model.getValueAt(row, column)).intValue();
            model.setValueAt(new Integer(currentValue + 1), row, column);
        }/* w w  w .j  a va  2  s  .  c  o m*/
    });

    pack();
}

From source file:Main.java

public Main() {
    final AbstractTableModel model = new MyModel();
    final JTable table = new JTable(model);
    getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
    model.setValueAt(new Integer(1), 0, 0);

    JButton button = new JButton("Increment selected cell");
    getContentPane().add(button, BorderLayout.SOUTH);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = table.getSelectedRow();
            int column = table.convertColumnIndexToModel(table.getSelectedColumn());
            int currentValue = ((Integer) model.getValueAt(row, column)).intValue();
            model.setValueAt(new Integer(currentValue + 1), row, column);
        }/*  w w  w .j  av  a  2  s .  c  o  m*/
    });

    pack();
}

From source file:org.drugis.addis.gui.builder.NetworkMetaAnalysisView.java

private MouseAdapter treatmentCategorizationListener(final NetworkRelativeEffectTableCellRenderer renderer) {
    return new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() > 1) {
                JTable table = (JTable) e.getComponent();
                int row = table.convertRowIndexToModel(table.rowAtPoint(e.getPoint()));
                int col = table.convertColumnIndexToModel(table.columnAtPoint(e.getPoint()));
                if (col == row) {
                    Treatment treatment = renderer.getTreatment(table.getModel(), col);
                    TreatmentDefinition treatmentDefinition = d_pm.getTreatmentDefinition(treatment);
                    Category category = treatmentDefinition.getContents().first();
                    if (category != null && !category.isTrivial()) {
                        d_mainWindow.leftTreeFocus(category.getCategorization());
                    }//from   w  w w .  j  a va 2s .  c  o  m
                }
            }
        }
    };
}

From source file:SortableTable.java

/**
 * Returns the renderer component.//  w w  w.j  a va 2 s  .  co m
 *
 * @param table      the table.
 * @param value      the value.
 * @param isSelected selected?
 * @param hasFocus   focussed?
 * @param row        the row.
 * @param column     the column.
 * @return the renderer.
 */
public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected,
        final boolean hasFocus, final int row, final int column) {

    if (table == null) {
        throw new NullPointerException("Table must not be null.");
    }

    final JComponent component;
    final SortableTableModel model = (SortableTableModel) table.getModel();
    final int cc = table.convertColumnIndexToModel(column);
    final boolean isSorting = (model.getSortingColumn() == cc);
    final boolean isAscending = model.isAscending();

    final JTableHeader header = table.getTableHeader();
    final boolean isPressed = (cc == this.pressedColumn);

    if (this.useLabels) {
        final JLabel label = getRendererLabel(isSorting, isAscending);
        label.setText((value == null) ? "" : value.toString());
        component = label;
    } else {
        final JButton button = getRendererButton(isSorting, isAscending);
        button.setText((value == null) ? "" : value.toString());
        button.getModel().setPressed(isPressed);
        button.getModel().setArmed(isPressed);
        component = button;
    }

    if (header != null) {
        component.setForeground(header.getForeground());
        component.setBackground(header.getBackground());
        component.setFont(header.getFont());
    }
    return component;
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopAbstractTable.java

protected String getStylename(JTable table, int row, int column) {
    if (styleProviders == null) {
        return null;
    }//from  www  . j a va 2  s.  com

    Entity item = tableModel.getItem(row);
    int modelColumn = table.convertColumnIndexToModel(column);
    Object property = columnsOrder.get(modelColumn).getId();

    String joinedStyle = null;
    for (StyleProvider styleProvider : styleProviders) {
        //noinspection unchecked
        String styleName = styleProvider.getStyleName(item, property.toString());
        if (styleName != null) {
            if (joinedStyle == null) {
                joinedStyle = styleName;
            } else {
                joinedStyle += " " + styleName;
            }
        }
    }

    return joinedStyle;
}

From source file:org.nuclos.client.genericobject.GenericObjectCollectController.java

/**
 * sets up a modified renderer for the result table header that uses the HTML formatted toString() value of the
 * column's entity field rather than its unformatted getLabel() value.
 *//* www .j  ava  2  s  . c  o m*/
private void setupResultTableHeaderRenderer() {
    final JTableHeader header = getResultTable().getTableHeader();
    final TableCellRenderer headerrendererDefault = header.getDefaultRenderer();
    header.setDefaultRenderer(new TableCellRenderer() {
        @Override
        public Component getTableCellRendererComponent(JTable tbl, Object oValue, boolean bSelected,
                boolean bHasFocus, int iRow, int iColumn) {
            final CollectableEntityField f = GenericObjectCollectController.this.getResultTableModel()
                    .getCollectableEntityField(tbl.convertColumnIndexToModel(iColumn));
            final String value;
            if (f instanceof CollectableEOEntityField) {
                // In the pivot case, we want to separate the column name
                // from the tool top.
                final CollectableEOEntityField field = (CollectableEOEntityField) f;
                final EntityFieldMetaDataVO ef = field.getMeta();
                if (ef.getPivotInfo() != null) {
                    value = ef.getField() + ":" + ef.getPivotInfo().getValueField();
                } else {
                    value = f.getLabel();
                }
            } else {
                value = f.getLabel();
            }
            return headerrendererDefault.getTableCellRendererComponent(tbl, value, bSelected, bHasFocus, iRow,
                    iColumn);
        }
    });
}