/*
  *  Update the input and action maps with new ToggleAction
  */
 private void installToggleAction(
     boolean isToggleDynamic, boolean isToggleLarger, String key, String keyStroke) {
   Action action = new ToggleAction(isToggleDynamic, isToggleLarger);
   KeyStroke ks = KeyStroke.getKeyStroke(keyStroke);
   table.getInputMap().put(ks, key);
   table.getActionMap().put(key, action);
 }
  //
  //  Implement the TableModelListener
  //
  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();
    }
  }
 /*
  *  Update the input and action maps with a new ColumnAction
  */
 private void installColumnAction(
     boolean isSelectedColumn, boolean isAdjust, String key, String keyStroke) {
   Action action = new ColumnAction(isSelectedColumn, isAdjust);
   KeyStroke ks = KeyStroke.getKeyStroke(keyStroke);
   table.getInputMap().put(ks, key);
   table.getActionMap().put(key, action);
 }
  /*
   *  Restore the width of the specified column to its previous width
   */
  private void restoreColumn(int column) {
    TableColumn tableColumn = table.getColumnModel().getColumn(column);
    Integer width = columnSizes.get(tableColumn);

    if (width != null) {
      table.getTableHeader().setResizingColumn(tableColumn);
      tableColumn.setWidth(width.intValue());
    }
  }
  /*
   *  Get the preferred width for the specified cell
   */
  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;
  }
  /*
   *  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;
  }
  /*
   *  Indicate whether changes to the model should cause the width to be
   *  dynamically recalculated.
   */
  public void setDynamicAdjustment(boolean isDynamicAdjustment) {
    //  May need to add or remove the TableModelListener when changed

    if (this.isDynamicAdjustment != isDynamicAdjustment) {
      if (isDynamicAdjustment) {
        table.addPropertyChangeListener(this);
        table.getModel().addTableModelListener(this);
      } else {
        table.removePropertyChangeListener(this);
        table.getModel().removeTableModelListener(this);
      }
    }

    this.isDynamicAdjustment = isDynamicAdjustment;
  }
  /*
   *  Adjust the widths of all the columns in the table
   */
  public void adjustColumns() {
    TableColumnModel tcm = table.getColumnModel();

    for (int i = 0; i < tcm.getColumnCount(); i++) {
      adjustColumn(i);
    }
  }
  /*
   *  Calculate the width based on the widest cell renderer for the
   *  given column.
   */
  private int getColumnDataWidth(int column) {
    if (!isColumnDataIncluded) return 0;

    int preferredWidth = 0;
    int maxWidth = table.getColumnModel().getColumn(column).getMaxWidth();

    for (int row = 0; row < table.getRowCount(); row++) {
      preferredWidth = Math.max(preferredWidth, getCellDataWidth(row, column));

      //  We've exceeded the maximum width, no need to check other rows

      if (preferredWidth >= maxWidth) break;
    }

    return 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);
  }
  /*
   *  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);
  }
  public void init() {
    setTitle("Expression Property Metadata Editor");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    expressionsTableModel = new EditableMetaDataTableModel();
    expressionsTable = new JTable(expressionsTableModel);
    expressionsTable.setDefaultRenderer(String.class, new EditableMetaDataRenderer());

    final JPanel contentPane = new JPanel();
    contentPane.setLayout(new BorderLayout());
    contentPane.add(new JScrollPane(expressionsTable), BorderLayout.CENTER);
    setContentPane(contentPane);
    setSize(800, 600);
  }
 /** Updates the tableModel for the JTable. */
 private void updateModel() {
   jTable.setModel(tableModel);
   tableModel.fireTableDataChanged();
 }
 public TableController(JTable jTable, Feed feed, Frame frame) {
   this.jTable = jTable;
   this.feed = feed;
   this.frame = frame;
   tableModel = (DefaultTableModel) jTable.getModel();
 }
Example #15
0
 private JTable createTable(CantorTableResult result) {
   JTable cantorTable = new JTable(result.getRowsData(), result.getColumnNames());
   cantorTable.setFillsViewportHeight(true);
   return cantorTable;
 }