Example usage for javax.swing.table TableColumnModel getColumn

List of usage examples for javax.swing.table TableColumnModel getColumn

Introduction

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

Prototype

public TableColumn getColumn(int columnIndex);

Source Link

Document

Returns the TableColumn object for the column at columnIndex.

Usage

From source file:HW3.java

private void displayBusinessTable(ResultSet result) throws SQLException {
    TableColumnModel tableModel = jTable2.getColumnModel();
    tableModel.getColumn(0).setPreferredWidth(200);
    tableModel.getColumn(4).setPreferredWidth(200);
    DefaultTableModel model = (DefaultTableModel) jTable2.getModel();

    while (result.next()) {
        String bid = result.getString(1);
        String bname = result.getString(2);
        String bcity = result.getString(5);
        String bstate = result.getString(4);
        String bstars = result.getFloat(3) + "";
        String bzip = result.getString(6);
        model.addRow(new Object[] { bname, bcity, bstate, bstars, bid, bzip });
        //System.out.println(bid + "  " + bname + bcity + "  " + bstate + "  " + bstars);
    }//  w ww .  j  a v  a2  s  .co m
    jTable2.repaint();
    if (jTable2.getRowCount() == 0) {
        JOptionPane.showMessageDialog(topPanel, "NO DATA TO DISPLAY", "Message",
                JOptionPane.INFORMATION_MESSAGE);
    }
}

From source file:HW3.java

private void displayReviewTable(ResultSet result) throws SQLException {
    TableColumnModel tableModel = jTable1.getColumnModel();
    tableModel.getColumn(2).setPreferredWidth(300);
    tableModel.getColumn(3).setPreferredWidth(200);
    DefaultTableModel model = (DefaultTableModel) jTable1.getModel();

    while (result.next()) {
        String date = result.getDate(7) + "";
        String stars = result.getInt(5) + "";
        String text = result.getString(6);
        String user = result.getString(4);
        String useful = result.getInt(2) + "";
        String cool = result.getInt(3) + "";
        String funny = result.getInt(1) + "";
        model.addRow(new Object[] { date, stars, text, user, useful, cool, funny });
        //System.out.println(date + " "+ stars + " "+ text + " "+ user + " "+ useful + " " + cool + " "+ funny);
    }/* www  . j ava  2 s. co m*/
    topPanel.setVisible(false);
    //reviewPanel.repaint();
    jScrollPane3.setVisible(true);
    jTable1.setVisible(true);
    reviewPanel.setVisible(true);
    if (jTable1.getRowCount() == 0) {
        JOptionPane.showMessageDialog(reviewPanel, "NO Reviews tO DISPLAY", "Message",
                JOptionPane.INFORMATION_MESSAGE);
    }
}

From source file:net.sf.jabref.gui.ImportInspectionDialog.java

private void setWidths() {
    TableColumnModel cm = glTable.getColumnModel();
    cm.getColumn(0).setPreferredWidth(55);
    cm.getColumn(0).setMinWidth(55);/*from w ww . j  a  v  a  2 s .c o m*/
    cm.getColumn(0).setMaxWidth(55);
    for (int i = 1; i < PAD; i++) {
        // Lock the width of icon columns.
        cm.getColumn(i).setPreferredWidth(GUIGlobals.WIDTH_ICON_COL);
        cm.getColumn(i).setMinWidth(GUIGlobals.WIDTH_ICON_COL);
        cm.getColumn(i).setMaxWidth(GUIGlobals.WIDTH_ICON_COL);
    }

    for (int i = 0; i < INSPECTION_FIELDS.size(); i++) {
        int width = InternalBibtexFields.getFieldLength(INSPECTION_FIELDS.get(i));
        glTable.getColumnModel().getColumn(i + PAD).setPreferredWidth(width);
    }
}

From source file:userInterface.EnergySourceBoardSupervisor.ManageEnergyConsumptionsJPanel.java

public void populateTable(Person person) {
    DefaultTableModel model = (DefaultTableModel) applianceTable.getModel();
    TableColumnModel tcm = applianceTable.getColumnModel();
    CustomTableCellRenderer customCellRenderer = new CustomTableCellRenderer();
    model.setRowCount(0);// w  ww  .  j  a va2 s  .c o  m
    if (person.getSensorsList().getSensorsList() != null) {
        for (Sensor sensor : person.getSensorsList().getSensorsList()) {
            Object[] row = new Object[5];
            row[0] = sensor;
            String appName = sensor.getSensorType().getValue();
            row[1] = appName;
            int watts = sensor.getNumberWatt();
            row[2] = watts;
            row[3] = sensor.getHours();
            row[4] = sensor.getConsumptionLevel();
            model.addRow(row);
            tcm.getColumn(4).setCellRenderer(customCellRenderer);
        }
        populateCalculations();
    }

}

From source file:it.cnr.icar.eric.client.ui.swing.RegistryObjectsTable.java

/**
 * Creates default columns for the table from
 * the data model using the <code>getColumnCount</code> method
 * defined in the <code>TableModel</code> interface.
 *
 * Clears any existing columns before creating the
 * new columns based on information from the model.
 *
 * Overrides base class behaviour by setting the column width as a % of the
 * viewport width.// www.j av  a2  s  .  co  m
 */
public void createDefaultColumnsFromModel() {
    TableModel m = getModel();
    if (m != null) {
        // Remove any current columns
        TableColumnModel cm = getColumnModel();
        while (cm.getColumnCount() > 0) {
            cm.removeColumn(cm.getColumn(0));
        }

        // get parent width
        int parentWidth = 0;
        Component parent = getParent();
        if (parent != null) {
            parentWidth = parent.getWidth();
        }

        // Create new columns from the data model info
        int columnCount = m.getColumnCount();
        for (int i = 0; i < m.getColumnCount(); i++) {
            int width = tableModel.getColumnWidth(i);
            if (width == 0) {
                width = parentWidth / columnCount;
            } else {
                //Width is a % of the viewport width
                width = (width * parentWidth) / 100;
            }
            TableColumn newColumn = new TableColumn(i);
            newColumn.setPreferredWidth(width);
            addColumn(newColumn);
        }
    }
}

From source file:gov.noaa.ncdc.iosp.avhrr.util.AvhrrLevel1B2Netcdf.java

private void setTableCellRenderer(JTable table, TableCellRenderer renderer) {
    TableColumnModel columnModel = table.getColumnModel();
    int columnCount = columnModel.getColumnCount();

    for (int i = 0; i < columnCount; i++) {
        TableColumn column = columnModel.getColumn(i);

        if (i == 0) {
            column.setMaxWidth(90);/*w ww.  j  av  a 2  s.  c o m*/
        } else {
            if (i > 1) {
                column.setMaxWidth(90);
            }
            if (i == (columnCount - 1)) {
                column.setMaxWidth(0);
            }
            if (i == (columnCount - 2)) {
                column.setMaxWidth(150);
            }
            column.setCellRenderer(renderer);
        }
    }
}

From source file:com.aw.swing.mvp.binding.component.BndSJTable.java

public void resetHeader() {
    TableColumnModel colModel = jTable.getColumnModel();
    int colsNumber = getColumnsInfo().length;
    int colsModel = colModel.getColumnCount() + fixedColumns;
    if (colsNumber == colsModel) {
        if (getSelectorColumn() != null) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JTable fixedJTable = (JTable) jTable.getClientProperty(CP_FIXED_TABLE);
                    if (fixedJTable != null) {
                        fixedJTable.getTableHeader().repaint();
                    } else {
                        jTable.getTableHeader().repaint();
                    }//  w  w  w .ja va  2s . c  o m
                }
            });
        }
        return;
    }
    if (colsNumber > colsModel) {
        int delta = colsNumber - colModel.getColumnCount();
        for (int i = 0; i < delta; i++) {
            colModel.addColumn(new TableColumn());
        }
    }
    for (int i = 0; i < colsNumber; i++) {
        TableColumn column = colModel.getColumn(i);
        column.setHeaderValue(getColumnsInfo()[i].getColumnHeader());
    }
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JTable fixedJTable = (JTable) jTable.getClientProperty(CP_FIXED_TABLE);
            if (fixedJTable != null) {
                fixedJTable.getTableHeader().repaint();
            }
            jTable.getTableHeader().repaint();
        }
    });
}

From source file:userInterface.HospitalAdminRole.ManagePatientsJPanel.java

public void populateTable(Person person) {
    DefaultTableModel model = (DefaultTableModel) vitalSignjTable.getModel();
    TableColumnModel tcm = vitalSignjTable.getColumnModel();
    CustomTableCellRenderer customCellRenderer = new CustomTableCellRenderer();
    model.setRowCount(0);/*w ww  . j a  v  a2s. c o m*/
    //if(houseOrg.getPersonDir().getPersonDir()!=null){
    //for(Person person:houseOrg.getPersonDir().getPersonDir()){
    if (person.getMemberDir().getMemberDirectory() != null) {
        for (Member member : person.getMemberDir().getMemberDirectory()) {
            int patientAge = member.getAge();
            if (member.getVitalSignList().getVitalSignList() != null) {
                for (VitalSign vs : member.getVitalSignList().getVitalSignList()) {
                    Object[] row = new Object[8];
                    //            vitalSignjTable.getColumnModel().getColumn(7).setMinWidth(0);
                    //            vitalSignjTable.getColumnModel().getColumn(7).setMaxWidth(0);
                    //            vitalSignjTable.getColumnModel().getColumn(7).setWidth(0);
                    row[0] = member;
                    row[1] = vs;
                    row[2] = vs.getRespiratoryRate();
                    row[3] = vs.getHeartRate();
                    row[4] = vs.getBloodPressure();
                    row[5] = vs.getTemperature();
                    row[6] = member.getVitalSignList().VitalSignStatus(patientAge, vs);
                    row[7] = vs.getAlertStatus();
                    model.addRow(row);
                    tcm.getColumn(6).setCellRenderer(customCellRenderer);
                }
            }
        }
    }
}

From source file:edu.ku.brc.af.ui.forms.TableViewObj.java

/**
 * Sets all the Columns to be center justified this COULD be set up in the table info.
 *
 *//*  w  w  w . j  a  v  a  2s. co  m*/
protected void configColumns() {
    ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer())
            .setHorizontalAlignment(SwingConstants.CENTER);

    //DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    //renderer.setHorizontalAlignment(SwingConstants.CENTER);

    DefaultTableCellRenderer renderer = new BiColorTableCellRenderer(true); // Centered

    TableColumnModel tableColModel = table.getColumnModel();
    for (int i = 0; i < tableColModel.getColumnCount(); i++) {
        tableColModel.getColumn(i).setCellRenderer(renderer);
    }
}

From source file:edu.ku.brc.ui.UIHelper.java

/**
 * Calculates and sets the each column to it preferred size.  NOTE: This
 * method also sets the table height to 10 rows.
 * /*w  w w .ja va 2s . com*/
 * @param table the table to fix up
 * @param numRowsHeight the number of rows to make the table height (or null not to set it)
 */
public static void calcColumnWidths(final JTable table, final Integer numRowsHeight, final Integer maxWidth) {
    if (table != null) {
        JTableHeader header = table.getTableHeader();

        TableCellRenderer defaultHeaderRenderer = null;

        if (header != null) {
            defaultHeaderRenderer = header.getDefaultRenderer();
        }

        TableColumnModel columns = table.getColumnModel();
        TableModel data = table.getModel();

        int margin = columns.getColumnMargin(); // only JDK1.3

        int rowCount = data.getRowCount();

        int totalWidth = 0;

        for (int i = columns.getColumnCount() - 1; i >= 0; --i) {
            TableColumn column = columns.getColumn(i);

            int columnIndex = column.getModelIndex();

            int width = -1;

            TableCellRenderer h = column.getHeaderRenderer();

            if (h == null)
                h = defaultHeaderRenderer;

            if (h != null) // Not explicitly impossible
            {
                Component c = h.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1,
                        i);

                width = c.getPreferredSize().width;
            }

            for (int row = rowCount - 1; row >= 0; --row) {
                TableCellRenderer r = table.getCellRenderer(row, i);

                Component c = r.getTableCellRendererComponent(table, data.getValueAt(row, columnIndex), false,
                        false, row, i);

                width = Math.max(width, c.getPreferredSize().width + 10); // adding an arbitray 10 pixels to make it look nicer

                if (maxWidth != null) {
                    width = Math.min(width, maxWidth);
                }
            }

            if (width >= 0) {
                column.setPreferredWidth(width + margin); // <1.3: without margin
            } else {
                // ???
            }

            totalWidth += column.getPreferredWidth();
        }

        // If you like; This does not make sense for two many columns!
        Dimension size = table.getPreferredScrollableViewportSize();
        //if (totalWidth > size.width)
        {
            if (numRowsHeight != null) {
                size.height = Math.min(size.height, table.getRowHeight() * numRowsHeight);
            }
            size.width = totalWidth;
            table.setPreferredScrollableViewportSize(size);
        }
    }
}