Example #1
0
  private HeaderCellRenderer getHeaderRenderer(int idx) {
    TableColumn tc = data.getColumnModel().getColumn(idx);
    if (tc.getHeaderRenderer() != null) return (HeaderCellRenderer) tc.getHeaderRenderer();

    HeaderCellRenderer hcr = new HeaderCellRenderer();
    tc.setHeaderRenderer(hcr);

    return hcr;
  }
Example #2
0
  /**
   * Calculates the optimal width for the header of the given table. The calculation is based on the
   * preferred width of the header renderer.
   *
   * @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 calcHeaderWidth(JTable table, int col) {
    if (table == null) return -1;

    if (col < 0 || col > table.getColumnCount()) {
      System.out.println("invalid col " + col);
      return -1;
    }

    JTableHeader header = table.getTableHeader();
    TableCellRenderer defaultHeaderRenderer = null;
    if (header != null) defaultHeaderRenderer = header.getDefaultRenderer();
    TableColumnModel columns = table.getColumnModel();
    TableModel data = table.getModel();
    TableColumn column = columns.getColumn(col);
    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, col);
      width = c.getPreferredSize().width + 5;
    }

    return width;
  }
  private int getHeaderHeight() {
    if ((header == null) || (header.getTable() == null)) {
      return 0;
    }
    int height = 0;
    boolean accomodatedDefault = false;
    TableColumnModel columnModel = header.getColumnModel();
    for (int column = 0; column < columnModel.getColumnCount(); column++) {
      TableColumn aColumn = columnModel.getColumn(column);
      boolean isDefault = (aColumn.getHeaderRenderer() == null);

      if (!isDefault || !accomodatedDefault) {
        Component comp = getHeaderRenderer(column);
        int rendererHeight = comp.getPreferredSize().height;
        height = Math.max(height, rendererHeight);

        // Configuring the header renderer to calculate its preferred size
        // is expensive. Optimise this by assuming the default renderer
        // always has the same height as the first non-zero height that
        // it returns for a non-null/non-empty value.
        if (isDefault && rendererHeight > 0) {
          Object headerValue = aColumn.getHeaderValue();
          if (headerValue != null) {
            headerValue = headerValue.toString();

            if (headerValue != null && !headerValue.equals("")) {
              accomodatedDefault = true;
            }
          }
        }
      }
    }
    return height + 2;
  }
Example #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);
  }
Example #5
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);
  }
 public void
     setColumnWidths() { // See
                         // "http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#custom"
   int n = getModel().getColumnCount();
   for (int j = 0; j < n; ++j) {
     TableColumn column = getColumnModel().getColumn(j);
     TableCellRenderer headerRenderer = column.getHeaderRenderer();
     if (headerRenderer == null)
       headerRenderer = getTableHeader().getDefaultRenderer(); // the new 1.3 way
     Component columnComponent =
         headerRenderer.getTableCellRendererComponent(
             this, column.getHeaderValue(), false, false, -1, j);
     Component cellComponent =
         getDefaultRenderer(getColumnClass(j))
             .getTableCellRendererComponent(this, getModel().getValueAt(0, j), false, false, 0, j);
     int wantWidth =
         Math.max(
             columnComponent.getPreferredSize().width
                 + 10, // fudge factor ... seems to always be too small otherwise (on x86 Linux)
             cellComponent.getPreferredSize().width
                 + 10 // fudge factor ... seems to always be too small otherwise (on Mac OS X)
             );
     column.setPreferredWidth(wantWidth);
   }
 }
 private int getColumnHeaderWidth(JTable table, int colIndex) {
   TableColumn col = table.getColumnModel().getColumn(colIndex);
   TableCellRenderer renderer = col.getHeaderRenderer();
   if (renderer == null) renderer = table.getTableHeader().getDefaultRenderer();
   Component comp =
       renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
   return comp.getPreferredSize().width;
 }
Example #8
0
 public void setSortCol(int sortCol, boolean reverse) {
   TableColumnModel tcm = table.getColumnModel();
   for (int i = 0; i < table.getColumnCount(); i++) {
     TableColumn tc = tcm.getColumn(i);
     SortedHeaderRenderer shr = (SortedHeaderRenderer) tc.getHeaderRenderer();
     shr.setSortCol(sortCol, reverse);
   }
 }
 protected Component getHeaderRenderer(int col) {
   if ((header == null) || (header.getTable() == null)) {
     return null;
   }
   TableColumn tabCol = header.getColumnModel().getColumn(col);
   TableCellRenderer renderer = tabCol.getHeaderRenderer();
   if (renderer == null) {
     renderer = header.getDefaultRenderer();
   }
   return renderer.getTableCellRendererComponent(
       header.getTable(), tabCol.getHeaderValue(), false, false, -1, col);
 }
Example #10
0
  /** @param col */
  public void sort(int col) {
    if (col != -1) {
      final TableColumnModel tcm = getColumnModel();
      final TableColumn tc = tcm.getColumn(col);
      final SortRenderer renderer = (SortRenderer) tc.getHeaderRenderer();
      renderer.setPressedColumn(tc);
    }

    final List objs = saveSelection(this);
    getSelectionModel().clearSelection();
    ((RowTableModel) getModel()).sort(convertColumnIndexToModel(col));
    loadSelection(this, objs);
  }
  /*
   *  Calculated the width based on the column name
   */
  private int getColumnHeaderWidth(int column) {
    if (!isColumnHeaderIncluded) return 0;

    TableColumn tableColumn = table.getColumnModel().getColumn(column);
    Object value = tableColumn.getHeaderValue();
    TableCellRenderer renderer = tableColumn.getHeaderRenderer();

    if (renderer == null) {
      renderer = table.getTableHeader().getDefaultRenderer();
    }

    Component c = renderer.getTableCellRendererComponent(table, value, false, false, -1, column);
    return c.getPreferredSize().width;
  }
      private Component getHeaderRenderer(int columnIndex) {
        TableColumn aColumn = header.getColumnModel().getColumn(columnIndex);
        TableCellRenderer renderer = aColumn.getHeaderRenderer();

        if (renderer == null) {
          renderer = header.getDefaultRenderer();
        }

        if (headerSelected == columnIndex) {
          headerSelected = -1;
          return renderer.getTableCellRendererComponent(
              header.getTable(), aColumn.getHeaderValue(), true, false, -1, columnIndex);
        } else {
          return renderer.getTableCellRendererComponent(
              header.getTable(), aColumn.getHeaderValue(), false, false, -1, columnIndex);
        }
      }
Example #13
0
  public void autoResizeColWidth() {

    int margin = 5;

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

      // Get width of column header
      TableCellRenderer renderer = col.getHeaderRenderer();

      if (renderer == null) {
        renderer = tabel.getTableHeader().getDefaultRenderer();
      }

      Component comp =
          renderer.getTableCellRendererComponent(tabel, col.getHeaderValue(), false, false, 0, 0);

      width = comp.getPreferredSize().width;

      // Get maximum width of column data
      for (int r = 0; r < tabel.getRowCount(); r++) {
        renderer = tabel.getCellRenderer(r, vColIndex);
        comp =
            renderer.getTableCellRendererComponent(
                tabel, tabel.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);
    }

    ((DefaultTableCellRenderer) tabel.getTableHeader().getDefaultRenderer())
        .setHorizontalAlignment(SwingConstants.LEFT);

    // table.setAutoCreateRowSorter(true);
    tabel.getTableHeader().setReorderingAllowed(false);
  }
Example #14
0
  protected void setResultsData() {
    getTable().setModel(getTableModel());
    int maxScoreColWidth = 0;

    for (int x = 0; x < getTableModel().getColumnCount(); x++) {
      if (x != 1) {
        getTable()
            .getColumnModel()
            .getColumn(x)
            .setCellRenderer(new ResultsTableCellRenderer(false));
      }
      TableColumn column = getTable().getColumnModel().getColumn(x);
      Component comp;

      column.setHeaderRenderer(new ResultsTableCellRenderer(true));
      comp =
          column
              .getHeaderRenderer()
              .getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);
      int width = comp.getPreferredSize().width;

      for (int y = 0; y < getTableModel().getRowCount(); y++) {
        comp =
            getTable()
                .getDefaultRenderer(getTableModel().getColumnClass(x))
                .getTableCellRendererComponent(
                    getTable(), getTableModel().getValueAt(y, x), false, false, 0, x);
        if (comp.getPreferredSize().width > width) {
          width = comp.getPreferredSize().width;
        }
      }
      TableColumn col = getTable().getColumnModel().getColumn(x);

      col.setPreferredWidth(width);
      col.setMinWidth(width);
      col.setWidth(width);

      if (x >= 3 && width > maxScoreColWidth) {
        maxScoreColWidth = 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);
 }
 public int calcColumnWidths(JTableIbp table) {
   int panelsWidth = 0;
   JTableHeader header = table.getTableHeader();
   TableCellRenderer defaultHeaderRenderer = null;
   if (header != null) defaultHeaderRenderer = header.getDefaultRenderer();
   TableColumnModel columns = table.getColumnModel();
   // tableModel = table.getModel();
   int margin = columns.getColumnMargin(); // only JDK1.3
   int rowCount = table.getModel().getRowCount();
   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, table.getModel().getValueAt(row, columnIndex), false, false, row, i);
       width = Math.max(width, c.getPreferredSize().width);
     }
     if (width >= 0) column.setPreferredWidth(width + margin); // <1.3: without
     // margin
     else ; // ???
     panelsWidth += column.getPreferredWidth();
   }
   panelsWidth = panelsWidth > (screenWidth - 30) ? screenWidth : panelsWidth;
   frameWidth = panelsWidth > (frameWidth - 30) ? (panelsWidth - 30) : frameWidth;
   return panelsWidth;
 }
Example #17
0
    private void resizeColumns() {
      int columnCount = getColumnCount();
      int rowCount = getRowCount();
      TableColumnModel columnModel = getColumnModel();
      for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
        TableColumn column = columnModel.getColumn(columnIndex);
        int preferredWidth = column.getMinWidth();
        int maxWidth = column.getMaxWidth();

        // Header value with
        TableCellRenderer cellRenderer = column.getHeaderRenderer();
        if (cellRenderer == null) {
          cellRenderer = getTableHeader().getDefaultRenderer();
        }
        Object headerValue = column.getHeaderValue();
        Component comp =
            cellRenderer.getTableCellRendererComponent(
                this, headerValue, false, false, 0, columnIndex);
        int width = comp.getPreferredSize().width + getIntercellSpacing().width + 15;
        preferredWidth = Math.max(preferredWidth, width);

        // Row values' width
        for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
          cellRenderer = getCellRenderer(rowIndex, columnIndex);
          comp = prepareRenderer(cellRenderer, rowIndex, columnIndex);
          width = comp.getPreferredSize().width + getIntercellSpacing().width + 15;
          preferredWidth = Math.max(preferredWidth, width);
          if (preferredWidth >= maxWidth) {
            preferredWidth = maxWidth;
            break;
          }
        }

        column.setPreferredWidth(preferredWidth);
      }
    }
Example #18
0
package com.l2fprod.common.swing.table;