Esempio n. 1
0
  public void packColumn(JTable table, int vColIndex, int margin) {
    DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
    TableColumn col = colModel.getColumn(vColIndex);
    int width = 0;

    // Get width of column header
    TableCellRenderer renderer = col.getHeaderRenderer();
    if (renderer == null) {
      renderer = table.getTableHeader().getDefaultRenderer();
    }
    Component comp =
        renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
    width = comp.getPreferredSize().width;

    // Get maximum width of column data
    for (int r = 0; r < table.getRowCount(); r++) {
      renderer = table.getCellRenderer(r, vColIndex);
      comp =
          renderer.getTableCellRendererComponent(
              table, table.getValueAt(r, vColIndex), false, false, r, vColIndex);
      width = Math.max(width, comp.getPreferredSize().width);
    }

    // Add margin
    width += 2 * margin;

    // Set the width
    col.setPreferredWidth(width);
  }
 private static void drawSelection(JTable table, int column, Graphics g, final int width) {
   int y = 0;
   final int[] rows = table.getSelectedRows();
   final int height = table.getRowHeight();
   for (int row : rows) {
     final TableCellRenderer renderer = table.getCellRenderer(row, column);
     final Component component =
         renderer.getTableCellRendererComponent(
             table, table.getValueAt(row, column), false, false, row, column);
     g.translate(0, y);
     component.setBounds(0, 0, width, height);
     boolean wasOpaque = false;
     if (component instanceof JComponent) {
       final JComponent j = (JComponent) component;
       if (j.isOpaque()) wasOpaque = true;
       j.setOpaque(false);
     }
     component.paint(g);
     if (wasOpaque) {
       ((JComponent) component).setOpaque(true);
     }
     y += height;
     g.translate(0, -y);
   }
 }
    @Override
    public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
      setFont(table.getFont());
      setText((value == null) ? "" : " " + value.toString());

      if ((row == -1) && (column == 0)) { // No. column header
        int width_header = getPreferredSize().width + 2;
        int width_numbers = 25;

        if (data.getRowCount() > 0) {
          TableCellRenderer r = table.getCellRenderer(data.getRowCount() - 1, 0);
          Component c =
              r.getTableCellRendererComponent(
                  table,
                  ((ConstructionTableData) data).getValueAt(data.getRowCount() - 1, 0),
                  false,
                  false,
                  data.getRowCount() - 1,
                  0);
          // width = Math.max(width, c.getPreferredSize().width +2);
          width_numbers = Math.max(width_numbers, c.getPreferredSize().width + 2);
        }

        // tableColumns[0].setMaxWidth(width);
        tableColumns[0].setMinWidth(width_numbers);
        tableColumns[0].setMaxWidth(Math.max(width_numbers, width_header));
        // tableColumns[0].setPreferredWidth(width);
      }

      return this;
    }
Esempio n. 4
0
  private void packColumn(JTable table, int vColIndex, int margin) {
    //		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
    TableColumn col = colModel.getColumn(vColIndex);
    int width = 0;

    // obtiene la anchura de la cabecera de la columna
    TableCellRenderer renderer = col.getHeaderRenderer();
    if (renderer == null) {
      renderer = table.getTableHeader().getDefaultRenderer();
    }
    Component comp =
        renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
    //		width = comp.getPreferredSize().width;
    width = 5;

    // Obtine la anchura maxima de la coluna de
    for (int r = 0; r < table.getRowCount(); r++) {
      renderer = table.getCellRenderer(r, vColIndex);
      comp =
          renderer.getTableCellRendererComponent(
              table, table.getValueAt(r, vColIndex), false, false, r, vColIndex);
      width = Math.max(width, 0);
    }

    width += 2 * margin;

    // Configura el ancho
    col.setPreferredWidth(width);
  }
  private static int preferredWidth(JTable table, int col) {
    TableColumn tableColumn = table.getColumnModel().getColumn(col);
    int width =
        (int)
            table
                .getTableHeader()
                .getDefaultRenderer()
                .getTableCellRendererComponent(
                    table, tableColumn.getIdentifier(), false, false, -1, col)
                .getPreferredSize()
                .getWidth();

    if (table.getRowCount() != 0) {
      int from = 0, to = 0;
      Rectangle rect = table.getVisibleRect();
      from = table.rowAtPoint(rect.getLocation());
      to = table.rowAtPoint(new Point((int) rect.getMaxX(), (int) rect.getMaxY())) + 1;

      for (int row = from; row < to; row++) {
        int preferedWidth =
            (int)
                table
                    .getCellRenderer(row, col)
                    .getTableCellRendererComponent(
                        table, table.getValueAt(row, col), false, false, row, col)
                    .getPreferredSize()
                    .getWidth();
        width = Math.max(width, preferedWidth);
      }
    }
    return width + table.getIntercellSpacing().width;
  }
Esempio n. 6
0
 /**
  * auto fit table columns width
  *
  * @version 1.0
  * @author dinglinhui
  */
 public void FitTableColumns(JTable myTable) {
   JTableHeader header = myTable.getTableHeader();
   int rowCount = myTable.getRowCount();
   Enumeration columns = myTable.getColumnModel().getColumns();
   while (columns.hasMoreElements()) {
     TableColumn column = (TableColumn) columns.nextElement();
     int col = header.getColumnModel().getColumnIndex(column.getIdentifier());
     int width =
         (int)
             myTable
                 .getTableHeader()
                 .getDefaultRenderer()
                 .getTableCellRendererComponent(
                     myTable, column.getIdentifier(), false, false, -1, col)
                 .getPreferredSize()
                 .getWidth();
     for (int row = 0; row < rowCount; row++) {
       int preferedWidth =
           (int)
               myTable
                   .getCellRenderer(row, col)
                   .getTableCellRendererComponent(
                       myTable, myTable.getValueAt(row, col), false, false, row, col)
                   .getPreferredSize()
                   .getWidth();
       width = Math.max(width, preferedWidth);
     }
     header.setResizingColumn(column); // 此行很重要
     column.setWidth(width + myTable.getIntercellSpacing().width);
   }
 }
Esempio n. 7
0
  // 表格列根据内容调整大小
  public static void adjustTableColumnWidths(JTable table) {
    JTableHeader header = table.getTableHeader(); // 表头
    int rowCount = table.getRowCount(); // 表格的行数
    TableColumnModel cm = table.getColumnModel(); // 表格的列模型

    for (int i = 0; i < cm.getColumnCount(); i++) { // 循环处理每一列
      TableColumn column = cm.getColumn(i); // 第i个列对象
      int width =
          (int)
              header
                  .getDefaultRenderer()
                  .getTableCellRendererComponent(table, column.getIdentifier(), false, false, -1, i)
                  .getPreferredSize()
                  .getWidth(); // 用表头的绘制器计算第i列表头的宽度
      for (int row = 0; row < rowCount; row++) { // 循环处理第i列的每一行,用单元格绘制器计算第i列第row行的单元格度
        int preferedWidth =
            (int)
                table
                    .getCellRenderer(row, i)
                    .getTableCellRendererComponent(
                        table, table.getValueAt(row, i), false, false, row, i)
                    .getPreferredSize()
                    .getWidth();
        width = Math.max(width, preferedWidth); // 取最大的宽度
      }
      column.setPreferredWidth(width + table.getIntercellSpacing().width); // 设置第i列的首选宽度
    }

    table.doLayout(); // 按照刚才设置的宽度重新布局各个列
  }
 @RunsInCurrentThread
 private Component cellRendererIn(final JTable table, final int row, final int column) {
   Object value = table.getValueAt(row, column);
   TableCellRenderer cellRenderer = table.getCellRenderer(row, column);
   boolean cellSelected = table.isCellSelected(row, column);
   return cellRenderer.getTableCellRendererComponent(
       table, value, cellSelected, false, row, column);
 }
  /**
   * This method gets the width of a cell, specified by a column and row number.
   *
   * @param row The column number.
   * @param column The row number.
   * @return The width of the cell data.
   */
  private int getCellDataWidth(int row, int column) {
    //  Inovke the renderer for the cell to calculate the preferred width

    TableCellRenderer cellRenderer = table.getCellRenderer(row, column);
    Component c = table.prepareRenderer(cellRenderer, row, column);
    int width = c.getPreferredSize().width + table.getIntercellSpacing().width;

    return width;
  }
Esempio n. 10
0
 private JLabel renderTableCell(int rowIndex, int columnIndex) {
   TableCellRenderer cellRenderer = table.getCellRenderer(rowIndex, columnIndex);
   Object cellValue = table.getValueAt(rowIndex, columnIndex);
   ListSelectionModel selectionModel = table.getSelectionModel();
   boolean selected =
       rowIndex >= selectionModel.getMinSelectionIndex()
           && rowIndex <= selectionModel.getMaxSelectionIndex();
   return (JLabel)
       cellRenderer.getTableCellRendererComponent(
           table, cellValue, selected, false, rowIndex, columnIndex);
 }
  private int getColumnCellWidth(JTable table, int colIndex) {
    int width = 0;
    for (int r = 0; r < table.getRowCount(); r++) {
      TableCellRenderer renderer = table.getCellRenderer(r, colIndex);
      Component comp =
          renderer.getTableCellRendererComponent(
              table, table.getValueAt(r, colIndex), false, false, r, colIndex);
      width = Math.max(width, comp.getPreferredSize().width);
    }

    return width;
  }
Esempio n. 12
0
 private void resizeColumnWidth() {
   final TableColumnModel columnModel = jTable1.getColumnModel();
   for (int column = 0; column < jTable1.getColumnCount(); column++) {
     int width = 50; // Min width
     for (int row = 0; row < jTable1.getRowCount(); row++) {
       TableCellRenderer renderer = jTable1.getCellRenderer(row, column);
       Component comp = jTable1.prepareRenderer(renderer, row, column);
       width = Math.max(comp.getPreferredSize().width, width);
     }
     columnModel.getColumn(column).setPreferredWidth(width);
   }
   CenterText();
 }
Esempio n. 13
0
 /*package*/ void fitTableColumns(JTable table) {
   for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
     TableColumn column = table.getColumnModel().getColumn(i);
     int max = MINIMUM_TABLE_COLUMN_WIDTH;
     for (int j = 0; j < table.getRowCount(); j++) {
       TableCellRenderer r = table.getCellRenderer(j, i);
       Component c =
           r.getTableCellRendererComponent(table, table.getValueAt(j, i), false, false, j, i);
       int width = c.getPreferredSize().width;
       if (width > max) {
         max = width;
       }
     }
     column.setPreferredWidth(max);
   }
 }
Esempio n. 14
0
  /**
   * Calculates the optimal width for the column of the given table. The calculation is based on the
   * preferred width of the header and cell renderer. <br>
   * Taken from the newsgoup de.comp.lang.java with some modifications.<br>
   * Taken from FOPPS/EnhancedTable - http://fopps.sourceforge.net/<br>
   *
   * @param table the table to calculate the column width
   * @param col the column to calculate the widths
   * @return the width, -1 if error
   */
  public static int calcColumnWidth(JTable table, int col) {
    int width = calcHeaderWidth(table, col);
    if (width == -1) return width;

    TableColumnModel columns = table.getColumnModel();
    TableModel data = table.getModel();
    int rowCount = data.getRowCount();
    TableColumn column = columns.getColumn(col);
    try {
      for (int row = rowCount - 1; row >= 0; --row) {
        Component c = table.prepareRenderer(table.getCellRenderer(row, col), row, col);
        width = Math.max(width, c.getPreferredSize().width + 10);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    return width;
  }
Esempio n. 15
0
  private static void adjustColumnWidth(JTable tbl, int col, int cellInset) {
    int maxwidth = 0;

    for (int row = 0; row < tbl.getRowCount(); row++) {
      TableCellRenderer tcr = tbl.getCellRenderer(row, col);
      Object val = tbl.getValueAt(row, col);
      Component comp = tcr.getTableCellRendererComponent(tbl, val, false, false, row, col);
      maxwidth = Math.max(comp.getPreferredSize().width + cellInset, maxwidth);
    }
    TableCellRenderer tcr = tbl.getTableHeader().getDefaultRenderer();
    Object val = tbl.getColumnModel().getColumn(col).getHeaderValue();
    Component comp = tcr.getTableCellRendererComponent(tbl, val, false, false, -1, col);
    maxwidth =
        Math.max(
            comp.getPreferredSize().width + Main.pref.getInteger("table.header-inset", 0),
            maxwidth);

    int spacing = tbl.getIntercellSpacing().width;
    tbl.getColumnModel().getColumn(col).setPreferredWidth(maxwidth + spacing);
  }
Esempio n. 16
0
 public static void resizeColumnToFit(
     PRManFrame frame, TableView view, TableColumn tc, JTable table) {
   int cIdx = table.getColumnModel().getColumnIndex(tc.getIdentifier());
   int width = tc.getWidth();
   for (int iCnt = 0; iCnt < table.getRowCount(); iCnt++) {
     if (view == null
         || !frame.isDescriptionResourceRow(view.tableModel.getModelRowForIndex(iCnt))) {
       Component comp =
           table
               .getCellRenderer(iCnt, cIdx)
               .getTableCellRendererComponent(
                   table, table.getValueAt(iCnt, cIdx), true, false, iCnt, cIdx);
       comp.invalidate();
       comp.validate();
       int cpw = comp.getPreferredSize().width;
       width = Math.max(width, cpw);
     }
   }
   tc.setPreferredWidth(width);
 }
 public void packColumn(JTable table, int vColIndex, int margin) {
   TableModel model = table.getModel();
   DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
   TableColumn col = colModel.getColumn(vColIndex);
   int width = 0; // Obtém a largura do cabeçalho da coluna
   TableCellRenderer renderer = col.getHeaderRenderer();
   if (renderer == null) {
     renderer = table.getTableHeader().getDefaultRenderer();
   }
   Component comp =
       renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
   width = comp.getPreferredSize().width; // Obtém a largura maxima da coluna de dados
   for (int r = 0; r < table.getRowCount(); r++) {
     renderer = table.getCellRenderer(r, vColIndex);
     comp =
         renderer.getTableCellRendererComponent(
             table, table.getValueAt(r, vColIndex), false, false, r, vColIndex);
     width = Math.max(width, comp.getPreferredSize().width);
   }
   width += 2 * margin; // Configura a largura
   col.setPreferredWidth(width);
 }
Esempio n. 18
0
  /**
   * This method picks good column sizes. If all column heads are wider than the column's cells'
   * contents, then you can just use column.sizeWidthToFit().
   */
  private void initColumnSizes(JTable table) {
    TableModel model = table.getModel();
    if (model.getRowCount() > 0) {
      TableColumn column = null;
      Component comp = null;
      int cellWidth = 0;
      TableColumnModel columnModel = table.getColumnModel();

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

        for (int j = 0; j < table.getRowCount(); j++) {
          TableCellRenderer cellRenderer = table.getCellRenderer(j, i);
          comp =
              cellRenderer.getTableCellRendererComponent(
                  table, model.getValueAt(j, i), false, false, j, i);
          cellWidth = comp.getPreferredSize().width;
          column.setPreferredWidth(Math.max(column.getPreferredWidth(), cellWidth));
        }
      }
    }
  }
Esempio n. 19
0
  public void setFont(Font font) {
    super.setFont(font);

    if (dataTable != null && dataTable.getRowCount() > 0 && dataTable.getColumnCount() > 0) {

      // set the font for each component
      dataTable.setFont(font);
      if (dataTable.getTableHeader() != null) dataTable.getTableHeader().setFont(font);
      rowHeader.setFont(font);

      // get row height needed to draw an "X" character
      int h =
          dataTable
              .getCellRenderer(0, 0)
              .getTableCellRendererComponent(dataTable, "X", false, false, 0, 0)
              .getPreferredSize()
              .height;

      // use this height to set the table and row header heights
      dataTable.setRowHeight(h);
      rowHeader.setFixedCellHeight(h);

      // set the column width
      int size = font.getSize();
      if (size < 12) size = 12; // minimum size
      double multiplier = (size) / 12.0;
      preferredColumnWidth = (int) (SpreadsheetSettings.TABLE_CELL_WIDTH * multiplier);

      // columnHeader.setPreferredSize(new Dimension(preferredColumnWidth,
      // (int)(MyTable.TABLE_CELL_HEIGHT * multiplier)));
      // this.validate();
      // dataTable.repaint();
    }

    if (dataTable != null)
      dataTable.setPreferredScrollableViewportSize(dataTable.getPreferredSize());
  }
Esempio n. 20
0
package com.l2fprod.common.swing.table;