Example usage for javax.swing JTable getColumnModel

List of usage examples for javax.swing JTable getColumnModel

Introduction

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

Prototype

public TableColumnModel getColumnModel() 

Source Link

Document

Returns the TableColumnModel that contains all column information of this table.

Usage

From source file:Main.java

public Main() {
    DefaultTableModel m = new DefaultTableModel(new Object[][] { { "2", 2, 3 }, { "1", 4, 5 } },
            new Object[] { 1, 2, 3 });
    JTable t = new JTable(m);
    t.getColumnModel().getColumn(0)
            .setCellEditor(new DefaultCellEditor(new JComboBox(new String[] { "1", "2" })));
    t.getColumnModel().getColumn(0).setCellRenderer(getCellRenderer());
    t.setRowHeight(25);/*  w  w w  .  j a  va  2 s  . c  om*/
    getContentPane().add(new JScrollPane(t));
    pack();
}

From source file:Main.java

public TableColumn findTableColumn(JTable table, int columnModelIndex) {
    Enumeration e = table.getColumnModel().getColumns();
    for (; e.hasMoreElements();) {
        TableColumn col = (TableColumn) e.nextElement();
        if (col.getModelIndex() == columnModelIndex) {
            return col;
        }/* ww w. j a v  a2  s.com*/
    }
    return null;
}

From source file:Main.java

/**
 * Setups the given table for usage as row-header. This method setups the background color to
 * the same one than the column headers.
 *
 * {@note In a previous version, we were assigning to the row headers the same cell renderer than
 *        the one created by <cite>Swing</cite> for the column headers. But it produced strange
 *        effects when the L&F uses a vertical grandiant instead than a uniform color.}
 *
 * @param  table The table to setup as row headers.
 * @return The renderer which has been assigned to the table.
 *//* w ww.j a  va2  s.co m*/
public static TableCellRenderer setupAsRowHeader(final JTable table) {
    final JTableHeader header = table.getTableHeader();
    Color background = header.getBackground();
    Color foreground = header.getForeground();
    if (background == null || background.equals(table.getBackground())) {
        if (!SystemColor.control.equals(background)) {
            background = SystemColor.control;
            foreground = SystemColor.controlText;
        } else {
            final Locale locale = table.getLocale();
            background = UIManager.getColor("Label.background", locale);
            foreground = UIManager.getColor("Label.foreground", locale);
        }
    }
    final DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setBackground(background);
    renderer.setForeground(foreground);
    renderer.setHorizontalAlignment(DefaultTableCellRenderer.RIGHT);
    final TableColumn column = table.getColumnModel().getColumn(0);
    column.setCellRenderer(renderer);
    column.setPreferredWidth(60);
    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.setCellSelectionEnabled(false);
    return renderer;
}

From source file:Main.java

public TableColumn[] getColumnsInModel(JTable table) {
    List result = new ArrayList();
    for (Enumeration e = table.getColumnModel().getColumns(); e.hasMoreElements();) {
        result.add((TableColumn) e.nextElement());
    }// www  .ja  v a  2  s. c o  m
    Collections.sort(result, new TableColumnComparator());
    return (TableColumn[]) result.toArray(new TableColumn[result.size()]);
}

From source file:Main.java

public void mouseMoved(MouseEvent evt) {
    JTableHeader header = (JTableHeader) evt.getSource();
    JTable table = header.getTable();
    TableColumnModel colModel = table.getColumnModel();
    int vColIndex = colModel.getColumnIndexAtX(evt.getX());
    TableColumn col = null;/*from ww  w .  j a  va 2 s  .  c om*/
    if (vColIndex >= 0) {
        col = colModel.getColumn(vColIndex);
    }
    if (col != curCol) {
        header.setToolTipText((String) tips.get(col));
        curCol = col;
    }
}

From source file:Main.java

public int toView(JTable table, int mColIndex) {
    for (int c = 0; c < table.getColumnCount(); c++) {
        TableColumn col = table.getColumnModel().getColumn(c);
        if (col.getModelIndex() == mColIndex) {
            return c;
        }/*from   w  ww.j  ava  2  s .c om*/
    }
    return -1;
}

From source file:Main.java

public int toModel(JTable table, int vColIndex) {
    if (vColIndex >= table.getColumnCount()) {
        return -1;
    }/*  w w  w . j av a  2 s . c o  m*/
    return table.getColumnModel().getColumn(vColIndex).getModelIndex();
}

From source file:Main.java

public Main() {
    TableModel dataModel = new MyTableModel();
    JTable table = new JTable(dataModel);
    table.setAutoCreateRowSorter(true);/*from w w w.  j  a v a2  s .c om*/
    table.getColumnModel().getColumn(0).setPreferredWidth(100);
    table.getColumnModel().getColumn(1).setPreferredWidth(150);
    table.getColumnModel().getColumn(2).setPreferredWidth(200);
    JScrollPane jsp = new JScrollPane(table);
    this.add(jsp);
}

From source file:Main.java

public void mouseClicked(MouseEvent evt) {
    JTable table = ((JTableHeader) evt.getSource()).getTable();
    TableColumnModel colModel = table.getColumnModel();

    int index = colModel.getColumnIndexAtX(evt.getX());
    if (index == -1) {
        return;/*  ww  w. ja va2s . com*/
    }
    Rectangle headerRect = table.getTableHeader().getHeaderRect(index);
    if (index == 0) {
        headerRect.width -= 10;
    } else {
        headerRect.grow(-10, 0);
    }
    if (!headerRect.contains(evt.getX(), evt.getY())) {
        int vLeftColIndex = index;
        if (evt.getX() < headerRect.x) {
            vLeftColIndex--;
        }
    }
}

From source file:Main.java

public TableColumn[] getColumnsInView(JTable table) {
    TableColumn[] result = new TableColumn[table.getColumnCount()];

    // Use an enumeration
    Enumeration e = table.getColumnModel().getColumns();
    for (int i = 0; e.hasMoreElements(); i++) {
        result[i] = (TableColumn) e.nextElement();
    }/*from w  w w .  j  a  v  a 2 s .c om*/

    return result;
}