Example usage for javax.swing.table TableColumn getHeaderValue

List of usage examples for javax.swing.table TableColumn getHeaderValue

Introduction

In this page you can find the example usage for javax.swing.table TableColumn getHeaderValue.

Prototype

public Object getHeaderValue() 

Source Link

Document

Returns the Object used as the value for the header renderer.

Usage

From source file:ColumnExample.java

protected int sortedIndexOf(TableColumn tc) {
    // just do a linear search for now
    int stop = getColumnCount();
    String name = tc.getHeaderValue().toString();

    for (int i = 0; i < stop; i++) {
        if (name.compareTo(getColumn(i).getHeaderValue().toString()) <= 0) {
            return i;
        }//from  ww w .  ja  va2 s. c  o  m
    }
    return stop;
}

From source file:edu.ku.brc.specify.utilapps.SetUpBuildDlg.java

/**
 * @param table//from www  . ja va 2  s . com
 */
public static void calcColumnWidths(JTable table) {
    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 (width >= 0) {
            column.setPreferredWidth(width + margin); // <1.3: without margin
        } else {
            // ???
        }

        totalWidth += column.getPreferredWidth();
    }
}

From source file:Main.java

public Main() {
    JTable table = new JTable(3, 3);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    for (int i = 0; i < table.getColumnCount(); i++) {
        DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
        TableColumn col = colModel.getColumn(i);
        int width = 0;

        TableCellRenderer renderer = col.getHeaderRenderer();
        if (renderer == null) {
            renderer = table.getTableHeader().getDefaultRenderer();
        }//  w w  w. j a v  a2 s.  co  m
        Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0,
                0);
        width = comp.getPreferredSize().width;
        col.setPreferredWidth(width + 2);
    }
    JFrame f = new JFrame();
    f.add(new JScrollPane(table));
    f.setSize(300, 300);
    f.setVisible(true);
}

From source file:com.att.aro.ui.model.ImageBPTable.java

/**
 * Returns a default table header for the DataTable.
 * //from  ww  w .j av a  2s  .co  m
 * @return A JTableHeader object with default properties.
 */
@Override
public JTableHeader createDefaultTableHeader() {
    return new JTableHeader(columnModel) {
        private static final long serialVersionUID = 1L;

        @Override
        public String getToolTipText(MouseEvent mEvent) {
            int column = columnAtPoint(mEvent.getPoint());

            // Locate the renderer under the event location
            if (column != -1) {
                TableColumn aColumn = columnModel.getColumn(column);
                Object tip = aColumn.getHeaderValue();
                if (tip != null) {
                    return tip.toString();
                }
            }
            return null;
        }
    };
}

From source file:com.mirth.connect.client.ui.components.MirthTable.java

private void saveColumnOrder() {
    try {/* ww w .j av  a2  s .c  o m*/
        if (StringUtils.isNotEmpty(prefix)) {
            columnOrderMap.clear();

            for (TableColumn column : getColumns(true)) {
                columnOrderMap.put((String) column.getHeaderValue(),
                        convertColumnIndexToView(column.getModelIndex()));
            }

            userPreferences.put(prefix + "ColumnOrderMap",
                    ObjectXMLSerializer.getInstance().serialize(columnOrderMap));
        }
    } catch (Exception e) {
    }
}

From source file:GeMSE.GS.Analysis.Stats.OneSampleCovariancePanel.java

public void ResizeColumnWidth(JTable table) {
    final TableColumnModel columnModel = table.getColumnModel();
    for (int column = 0; column < table.getColumnCount(); column++) {
        int width = 50; // Min width

        TableColumn tableColumn = columnModel.getColumn(column);
        TableCellRenderer renderer = tableColumn.getHeaderRenderer();
        if (renderer == null)
            renderer = table.getTableHeader().getDefaultRenderer();

        Component component = renderer.getTableCellRendererComponent(table, tableColumn.getHeaderValue(), false,
                false, -1, column);/*w w  w  .j a  v  a2s.c  o m*/
        width = Math.max(component.getPreferredSize().width + 5, width);

        for (int row = 0; row < table.getRowCount(); row++) {
            renderer = table.getCellRenderer(row, column);
            Component comp = table.prepareRenderer(renderer, row, column);
            width = Math.max(comp.getPreferredSize().width + 5, width);
        }
        if (width > 400)
            width = 400;
        columnModel.getColumn(column).setPreferredWidth(width);
    }
}

From source file:TableRenderDemo.java

private void initColumnSizes(JTable table) {
    MyTableModel model = (MyTableModel) table.getModel();
    TableColumn column = null;
    Component comp = null;/*  www.  j ava  2  s .c o m*/
    int headerWidth = 0;
    int cellWidth = 0;
    Object[] longValues = model.longValues;
    TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();

    for (int i = 0; i < 5; i++) {
        column = table.getColumnModel().getColumn(i);

        comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
        headerWidth = comp.getPreferredSize().width;

        comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table,
                longValues[i], false, false, 0, i);
        cellWidth = comp.getPreferredSize().width;

        if (DEBUG) {
            System.out.println("Initializing width of column " + i + ". " + "headerWidth = " + headerWidth
                    + "; cellWidth = " + cellWidth);
        }

        // XXX: Before Swing 1.1 Beta 2, use setMinWidth instead.
        column.setPreferredWidth(Math.max(headerWidth, cellWidth));
    }
}

From source file:components.TableRenderDemo.java

private void initColumnSizes(JTable table) {
    MyTableModel model = (MyTableModel) table.getModel();
    TableColumn column = null;
    Component comp = null;/*from  w w  w .j a v a 2s.  co m*/
    int headerWidth = 0;
    int cellWidth = 0;
    Object[] longValues = model.longValues;
    TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();

    for (int i = 0; i < 5; i++) {
        column = table.getColumnModel().getColumn(i);

        comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
        headerWidth = comp.getPreferredSize().width;

        comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table,
                longValues[i], false, false, 0, i);
        cellWidth = comp.getPreferredSize().width;

        if (DEBUG) {
            System.out.println("Initializing width of column " + i + ". " + "headerWidth = " + headerWidth
                    + "; cellWidth = " + cellWidth);
        }

        column.setPreferredWidth(Math.max(headerWidth, cellWidth));
    }
}

From source file:cn.labthink.ReadAccess060.java

public void ExportOneRecord(int k) {
    Integer testID = (Integer) jTable1.getValueAt(k, 0);
    if (testID == null) {
        jLabel_info.setText("<html><font color='red'>No Record Selected</font></html>");
        return;//  w  w w.  j av  a 2s  . co m
    }
    //        System.out.println(k);
    //vector
    Enumeration<TableColumn> en = jTable1.getTableHeader().getColumnModel().getColumns();
    Vector<String> TestInfoName = new Vector<>();
    Vector<String> TestInfoValue = new Vector<String>();
    while (en.hasMoreElements()) {
        TableColumn tc = en.nextElement();
        TestInfoName.add((String) tc.getHeaderValue());
    }
    for (int i = 0; i < TestInfoName.size(); i++) {
        TestInfoValue.add("" + jTable1.getValueAt(k, i));
    }
    outputexcelInfo(TestInfoName, TestInfoValue);
    try {
        //            initDB();
        for (int cellno = 0; cellno < 6; cellno++) {
            generateCellData(testID, cellno);
        }
    } catch (SQLException ee) {
        System.out.println(ee);
    } finally {
        try {
            sql.close();
        } catch (Exception e) {
        }
        try {
            rs.close();
        } catch (Exception e) {
        }
    }
    try {
        book.write();
        book.close();
        book = null;
    } catch (Exception ee) {
        ee.printStackTrace();
    }
}

From source file:cn.labthink.ReadAccess330.java

public void ExportOneRecord(int k) {
    Integer testID = (Integer) jTable1.getValueAt(k, 0);
    if (testID == null) {
        jLabel_info.setText("<html><font color='red'>No Record Selected</font></html>");
        return;/*w w w .jav  a  2s  . c o m*/
    }
    //        System.out.println(k);
    //vector
    Enumeration<TableColumn> en = jTable1.getTableHeader().getColumnModel().getColumns();
    Vector<String> TestInfoName = new Vector<String>();
    Vector<String> TestInfoValue = new Vector<String>();
    while (en.hasMoreElements()) {
        TableColumn tc = en.nextElement();
        TestInfoName.add((String) tc.getHeaderValue());
    }
    for (int i = 0; i < TestInfoName.size(); i++) {
        TestInfoValue.add("" + jTable1.getValueAt(k, i));
    }
    outputexcelInfo(TestInfoName, TestInfoValue);
    try {
        //            initDB();
        for (int cellno = 0; cellno < 6; cellno++) {
            generateCellData(testID, cellno);
        }
    } catch (SQLException ee) {
        System.out.println(ee);
    } finally {
        try {
            sql.close();
        } catch (Exception e) {
        }
        try {
            rs.close();
        } catch (Exception e) {
        }
    }
    try {
        book.write();
        book.close();
        book = null;
    } catch (Exception ee) {
        ee.printStackTrace();
    }
}