Example usage for javax.swing JTable getColumn

List of usage examples for javax.swing JTable getColumn

Introduction

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

Prototype

public TableColumn getColumn(Object identifier) 

Source Link

Document

Returns the TableColumn object for the column in the table whose identifier is equal to identifier, when compared using equals.

Usage

From source file:Main.java

public static void SetJTableAlignment(javax.swing.JTable jTable, ComponentOrientation componentOrientation) {
    Font tahoma = new Font("Tahoma", Font.PLAIN, 11);
    int labelAlighnment = JLabel.RIGHT;
    int headerAlighnment = JLabel.RIGHT;
    if (componentOrientation == ComponentOrientation.LEFT_TO_RIGHT) {
        labelAlighnment = JLabel.LEFT;
        headerAlighnment = JLabel.LEFT;
    }//w w w .  ja  va 2  s  .c o  m

    DefaultTableCellRenderer defaultTableCellRenderer = new DefaultTableCellRenderer();
    defaultTableCellRenderer.setHorizontalAlignment(labelAlighnment);
    // defaultTableCellRenderer.setFont(tahoma);

    for (int columnIndex = 0; columnIndex < jTable.getColumnCount(); columnIndex++) {
        if (jTable.getModel().getColumnClass(columnIndex).equals(Boolean.class)) {
            jTable.getColumnModel().getColumn(columnIndex).setWidth(60);
            jTable.getColumnModel().getColumn(columnIndex).setMaxWidth(90);
            jTable.getColumnModel().getColumn(columnIndex).setMinWidth(10);
            jTable.getColumnModel().getColumn(columnIndex).setPreferredWidth(60);
            continue;
        }
        if (jTable.getModel().getColumnClass(columnIndex).equals(ImageIcon.class)) {
            continue;
        }
        jTable.getColumnModel().getColumn(columnIndex).setCellRenderer(defaultTableCellRenderer);

    }
    DefaultTableCellRenderer renderer;
    renderer = (DefaultTableCellRenderer) jTable.getTableHeader().getDefaultRenderer();
    renderer.setHorizontalAlignment(headerAlighnment);

    jTable.getTableHeader().setDefaultRenderer(renderer);

}

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

/**
 *
 *
 * @param table/*w  w w.  j  a v  a2 s .c om*/
 * @param pref
 * @param defaultWidths
 *
 * @throws IllegalArgumentException
 */
public static void restoreTableMetrics(JTable table, String pref, int[] defaultWidths) {
    //  Check the table columns may be resized correctly
    if (table.getAutoResizeMode() != JTable.AUTO_RESIZE_OFF) {
        throw new IllegalArgumentException("Table AutoResizeMode must be JTable.AUTO_RESIZE_OFF");
    }

    //  Restore the table column widths and positions
    for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
        try {
            table.moveColumn(table.convertColumnIndexToView(getInt(pref + ".column." + i + ".position", i)), i);
            table.getColumnModel().getColumn(i)
                    .setPreferredWidth(getInt(pref + ".column." + i + ".width",
                            (defaultWidths == null) ? table.getColumnModel().getColumn(i).getPreferredWidth()
                                    : defaultWidths[i]));
        } catch (NumberFormatException nfe) {
        }
    }
}

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);//  ww  w .  j a  v  a  2s. c o m
    getContentPane().add(new JScrollPane(t));
    pack();
}

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.
 *//*from w  ww .  jav a2  s. c o  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 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  ww  w .j a  va2 s. co  m*/
    }
    return -1;
}

From source file:Main.java

public int toModel(JTable table, int vColIndex) {
    if (vColIndex >= table.getColumnCount()) {
        return -1;
    }//w  w w  .  jav 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);/*www  .  j  a  v  a 2 s  . c  o m*/
    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 TableColumn[] getColumnsInView(JTable table) {
    TableColumn[] result = new TableColumn[table.getColumnCount()];

    // Use a for loop
    for (int c = 0; c < table.getColumnCount(); c++) {
        result[c] = table.getColumnModel().getColumn(c);
    }/*from www  .  j  a  va2s. c o m*/

    return result;
}

From source file:MFPIM.NotificationFrame.java

public void ChangeName(JTable table, int col_index, String col_name) {
    table.getColumnModel().getColumn(col_index).setHeaderValue(col_name);
}

From source file:Visuals.RingChart.java

public JPanel addDefenceCharts() {
    lowValue = "Low (" + low + ")";
    ChartPanel ringPanel = drawRingChart();
    ringPanel.setDomainZoomable(true);/*from w w w. ja  va  2 s. c  o m*/
    JPanel thisRingPanel = new JPanel();
    thisRingPanel.setLayout(new BorderLayout());
    String[][] finalRisks = new String[riskCount][4];
    for (int i = 0; i < riskCount; i++) {
        finalRisks[i][0] = risks[i][0];
        finalRisks[i][1] = risks[i][1];
        finalRisks[i][2] = risks[i][2];
        finalRisks[i][3] = risks[i][3];
    }

    JTable table = new JTable(finalRisks, networkDefenceColumns);
    TableColumn tcol = table.getColumnModel().getColumn(2);
    table.removeColumn(tcol);
    table.setEnabled(false);

    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    int width = 0;
    for (int row = 0; row < table.getRowCount(); row++) {
        TableCellRenderer renderer = table.getCellRenderer(row, 1);
        Component comp = table.prepareRenderer(renderer, row, 1);
        width = Math.max(comp.getPreferredSize().width, width);
    }

    table.getColumnModel().getColumn(1).setPreferredWidth(width);

    width = 0;
    for (int row = 0; row < table.getRowCount(); row++) {
        TableCellRenderer renderer = table.getCellRenderer(row, 2);
        Component comp = table.prepareRenderer(renderer, row, 2);
        width = Math.max(comp.getPreferredSize().width, width);
    }

    table.getColumnModel().getColumn(2).setPreferredWidth(width);
    table.setShowHorizontalLines(true);
    table.setRowHeight(40);

    JScrollPane tableScrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    Dimension d = table.getPreferredSize();
    tableScrollPane
            .setPreferredSize(new Dimension((d.width - 400), (table.getRowHeight() + 1) * (riskCount + 1)));

    JLabel right = new JLabel(
            "                                                                                                    ");
    thisRingPanel.add(right, BorderLayout.EAST);
    thisRingPanel.add(ringPanel, BorderLayout.CENTER);
    thisRingPanel.add(tableScrollPane, BorderLayout.SOUTH);
    return thisRingPanel;
}