/**
   * This method implements the TableModelListener
   *
   * @param e The event to listen for.
   */
  @Override
  public void tableChanged(TableModelEvent e) {
    if (!isColumnDataIncluded) {
      return;
    }

    //  A cell has been updated

    if (e.getType() == TableModelEvent.UPDATE) {
      int column = table.convertColumnIndexToView(e.getColumn());

      //  Only need to worry about an increase in width for this cell

      if (isOnlyAdjustLarger) {
        int row = e.getFirstRow();
        TableColumn tableColumn = table.getColumnModel().getColumn(column);

        if (tableColumn.getResizable()) {
          int width = getCellDataWidth(row, column);
          updateTableColumn(column, width);
        }
      } //	Could be an increase of decrease so check all rows
      else {
        adjustColumn(column);
      }
    } //  The update affected more than one column so adjust all columns
    else {
      adjustColumns();
    }
  }
  /*
   *  Adjust the width of the specified column in the table
   */
  public void adjustColumn(final int column) {
    TableColumn tableColumn = table.getColumnModel().getColumn(column);

    if (!tableColumn.getResizable()) return;

    int columnHeaderWidth = getColumnHeaderWidth(column);
    int columnDataWidth = getColumnDataWidth(column);
    int preferredWidth = Math.max(columnHeaderWidth, columnDataWidth);

    updateTableColumn(column, preferredWidth);
  }
  /*
   *  Update the TableColumn with the newly calculated width
   */
  private void updateTableColumn(int column, int width) {
    final TableColumn tableColumn = table.getColumnModel().getColumn(column);

    if (!tableColumn.getResizable()) return;

    width += spacing;

    //  Don't shrink the column width

    if (isOnlyAdjustLarger) {
      width = Math.max(width, tableColumn.getPreferredWidth());
    }

    columnSizes.put(tableColumn, new Integer(tableColumn.getWidth()));
    table.getTableHeader().setResizingColumn(tableColumn);
    tableColumn.setWidth(width);
  }
예제 #4
0
  public static void main(String args[]) {

    final Object rowData[][] = {
      {"1", "one", "I"},
      {"2", "two", "II"},
      {"3", "three", "III"}
    };
    final String columnNames[] = {"#", "English", "Roman"};

    final JTable table = new JTable(rowData, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);

    JFrame frame = new JFrame("Resizing Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(scrollPane, BorderLayout.CENTER);

    TableColumn tc = null;
    for (int i = 0; i < 3; i++) {

      tc = table.getColumnModel().getColumn(i);

      if (tc.getModelIndex() == 0) tc.setResizable(false);
      if (i == 2) {
        tc.setPreferredWidth(100); // sport column is bigger
      } else {
        tc.setPreferredWidth(50);
      }
      System.out.printf(
          "column [%s] header=[%s] modelIndex=[%d] resizable=[%b] minWidth=[%s] maxWidth=[%d] preferredWidth=[%d]\n",
          tc.getIdentifier(),
          tc.getHeaderValue(),
          tc.getModelIndex(),
          tc.getResizable(),
          tc.getMinWidth(),
          tc.getMaxWidth(),
          tc.getPreferredWidth());
    }

    frame.setSize(300, 150);
    frame.setVisible(true);
  }