protected void updateLDS(int original_index, int new_index) {
    TableColumn tc = this.getColumn(new_index);
    int model_index = tc.getModelIndex();
    DataColumn dc = lds.getDataColumn(model_index);
    dc.setIncludePosition(new_index);
    if (Math.abs(original_index - new_index) == 1) {
      // two adjacent columns were swapped, so set include value for neighbor
      TableColumn tc2 = this.getColumn(original_index);
      int model_index2 = tc2.getModelIndex();
      DataColumn dc2 = lds.getDataColumn(model_index2);
      dc2.setIncludePosition(original_index);

      // swap names for dc and dc2, as well as table column header values and identifiers
      String name = dc.getName();
      int type = dc.getType();
      dc.setName(dc2.getName());
      dc.setType(dc2.getType());
      dc2.setName(name);
      dc2.setType(type);

      tc2.setHeaderValue(dc2.getName());
      tc2.setIdentifier(dc2.getName());

      tc.setHeaderValue(dc.getName());
      tc.setIdentifier(dc.getName());
    } else {
      syncIncludes();
    }
  }
Esempio n. 2
0
  /** Adjust the preferred size of the <code>Table</code>. */
  private void adjustPreferredSize() {
    // Refresh the model
    model.refresh();

    int preferredWidth = 20;
    TableColumn col = null;

    for (Enumeration<TableColumn> e = table.getColumnModel().getColumns(); e.hasMoreElements(); ) {
      col = e.nextElement();

      // Calculs & sets the preferred width of the cells.
      int colPreferredWidth = col.getPreferredWidth();
      for (int i = 0; i < table.getRowCount(); i++) {
        int cellWidth =
            table
                    .getTableHeader()
                    .getFontMetrics(table.getTableHeader().getFont())
                    .stringWidth(model.getValueAt(i, col.getModelIndex()).toString())
                + 5;
        if (cellWidth > colPreferredWidth) colPreferredWidth = cellWidth;
      }

      // Increments the table preferred width.
      col.setPreferredWidth(colPreferredWidth);

      // Increments the table preferred width.
      preferredWidth += colPreferredWidth;
    }
    // Set the preferred size.
    setPreferredSize(new Dimension(preferredWidth, (int) getPreferredSize().getHeight()));
  }
Esempio n. 3
0
  /** Save state to the PreferencesExt. */
  public void saveState(PreferencesExt store) {
    if (store == null) return;

    int ncols = table.getColumnCount();
    int[] size = new int[ncols];
    int[] modelIndex = new int[ncols];

    TableColumnModel tcm = table.getColumnModel();
    for (int i = 0; i < ncols; i++) {
      TableColumn tc = tcm.getColumn(i);
      size[i] = tc.getWidth();
      modelIndex[i] = tc.getModelIndex();
    }
    store.putBeanObject("ColumnWidths", size);
    store.putBeanObject("ColumnOrder", modelIndex);

    store.putInt("SortOnCol", model.getSortCol());
    store.putBoolean("SortReverse", model.getReverse());
    store.putBoolean("isThreadsOn", model.isThreadsOn());

    if (debug) {
      System.out.println(" store widths = ");
      for (int i = 0; i < size.length; i++) System.out.print(" " + size[i]);
      System.out.println();
    }
  }
Esempio n. 4
0
  private void initTableTextFields(JTable table) {

    TableColumnModel tcm = table.getColumnModel();
    Enumeration<TableColumn> cols = tcm.getColumns();
    while (cols.hasMoreElements()) {
      TableColumn tc = cols.nextElement();

      int limit = 5;
      switch (tc.getModelIndex()) {
        case 0:
          limit = 3;
          break;
        case 1:
          limit = 4;
          break;
        case 2:
          limit = 2;
      }

      //            JTextField jt = new JTextField();
      //            jt.setBorder(null);
      //            limitCharacters(table, limit, jt);
      tc.setCellEditor(new DefaultCellEditor(new UldTextField(table, limit)));
    }
  }
 public TableColumn getColumnByModelIndex(int modelIndex) {
   Enumeration<TableColumn> allColumns = this.getColumns();
   while (allColumns.hasMoreElements()) {
     TableColumn col = allColumns.nextElement();
     if (col.getModelIndex() == modelIndex) return col;
   }
   return null;
 }
Esempio n. 6
0
 public final void resetColumnWidths() {
   Enumeration<TableColumn> en = columnModel.getColumns(false);
   while (en.hasMoreElements()) {
     TableColumn tc = en.nextElement();
     int width = dataModel.getPreferredWidth(tc.getModelIndex());
     tc.setPreferredWidth(width);
   }
   dataTable.sizeColumnsToFit(-1);
 }
Esempio n. 7
0
 public int columnModelIndextoView(int mColIndex) {
   for (int c = 0; c < getColumnCount(); c++) {
     TableColumn col = getColumnModel().getColumn(c);
     if (col.getModelIndex() == mColIndex) {
       return c;
     }
   }
   return -1;
 }
 /**
  * Maps the index of the column in the table model at <code>modelColumnIndex</code> to the
  * TableColumn object. There may me multiple TableColumn objects showing the same model column,
  * though this is uncommon. This method will always return the first visible or else the first
  * invisible column with the specified index.
  *
  * @param modelColumnIndex index of column in table model
  * @return table column object or null if no such column in this column model
  */
 public TableColumn getColumnByModelIndex(int modelColumnIndex) {
   for (int columnIndex = 0; columnIndex < allTableColumns.size(); ++columnIndex) {
     TableColumn column = (TableColumn) allTableColumns.elementAt(columnIndex);
     if (column.getModelIndex() == modelColumnIndex) {
       return column;
     }
   }
   return null;
 }
 public void setCellRendered(JTableIbp table) {
   Enumeration<TableColumn> tcs = table.getColumnModel().getColumns();
   while (tcs.hasMoreElements()) {
     TableColumn tc = tcs.nextElement();
     if (table.getColumnClass(tc.getModelIndex()).toString().contains("java.lang.")) {
       tc.setCellRenderer(new CustomTableCellRenderer());
     }
   }
 }
Esempio n. 10
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);
  }
Esempio n. 11
0
 private void setupRenderers(JTable table) {
   // setup a table cell renderer for each column:
   for (Enumeration<TableColumn> enumeration = table.getColumnModel().getColumns();
       enumeration.hasMoreElements(); ) {
     final TableColumn column = enumeration.nextElement();
     final int iModelIndex = column.getModelIndex();
     final CollectableEntityField clctef =
         this.timelimittaskview
             .getTimelimitTaskTableModel()
             .getCollectableEntityField(iModelIndex);
     final CollectableComponent clctcomp =
         CollectableComponentFactory.getInstance().newCollectableComponent(clctef, null, false);
     column.setCellRenderer(clctcomp.getTableCellRenderer(false));
   }
 }
Esempio n. 12
0
  /** this array translates the column index to the model index */
  public int[] getModelIndex() {
    int[] modelIndex = new int[model.getColumnCount()];

    try {
      TableColumnModel tcm = table.getColumnModel();
      for (int i = 0; i < model.getColumnCount(); i++) {
        TableColumn tc = tcm.getColumn(i);
        modelIndex[i] = tc.getModelIndex();
      }
    } catch (java.lang.ArrayIndexOutOfBoundsException e) {
      // can happen when model size increases
    }

    return modelIndex;
  }
Esempio n. 13
0
  public void sort(int index, boolean ascend) {
    TableColumnModel colModel = getColumnModel();
    TableColumn tCol = null;
    try {
      tCol = colModel.getColumn(index);
    } catch (ArrayIndexOutOfBoundsException ex) {
      return;
    }
    int modelIndex = tCol.getModelIndex();

    SortTableModel model = (SortTableModel) getModel();
    if (model.isSortable(modelIndex)) {

      sortedColumnAscending = ascend;
      sortedColumnIndex = index;

      int cols = getColumnCount();

      Vector sel = null;
      int r = getSelectedRow();
      if (r != -1) {
        sel = new Vector();
        for (int i = 0; i < cols; i++) sel.add(getValueAt(r, i));
      }

      clearSelection();

      model.sortColumn(modelIndex, sortedColumnAscending);

      if (sel == null) return;

      for (int i = 0; i < getRowCount(); i++) {
        Vector h = new Vector();
        for (int c = 0; c < cols; c++) h.add(getValueAt(i, c));

        if (h.equals(sel)) {
          setRowSelectionInterval(i, i);
          Rectangle rect = getCellRect(i, 0, true);
          scrollRectToVisible(rect);
          break;
        }
      }
    }
  }
    public void mouseClicked(MouseEvent e) {
      TableColumnModel colModel = table.getColumnModel();
      int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
      int modelIndex = colModel.getColumn(columnModelIndex).getModelIndex();

      if (modelIndex < 0) return;
      if (sortCol == modelIndex) isSortAsc = !isSortAsc;
      else sortCol = modelIndex;

      for (int i = 0; i < colNames.length; i++) {
        TableColumn column = colModel.getColumn(i);
        column.setHeaderValue(getColumnName(column.getModelIndex()));
      }
      table.getTableHeader().repaint();

      Collections.sort(rowData, new MyComparator(isSortAsc));
      table.tableChanged(new TableModelEvent(MyTableModel.this));
      table.repaint();
    }
  /** @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent) */
  public void mouseReleased(MouseEvent e) {

    if (columnBeingResized == null) return;

    ColumnSettingParameter<CommonColumnType> csPar =
        parameters.getParameter(PeakListTableParameters.commonColumns);

    ColumnSettingParameter<DataFileColumnType> dfPar =
        parameters.getParameter(PeakListTableParameters.dataFileColumns);

    final int modelIndex = columnBeingResized.getModelIndex();
    final int newWidth = columnBeingResized.getPreferredWidth();

    final int numOfCommonColumns = CommonColumnType.values().length;
    final int numOfDataFileColumns = DataFileColumnType.values().length;

    if (modelIndex < numOfCommonColumns) {
      csPar.setColumnWidth(modelIndex, newWidth);
    } else {
      int dataFileColumnIndex = (modelIndex - numOfCommonColumns) % numOfDataFileColumns;
      dfPar.setColumnWidth(dataFileColumnIndex, newWidth);

      // set same width to other data file columns of this type
      for (int dataFileIndex = peakList.getNumberOfRawDataFiles() - 1;
          dataFileIndex >= 0;
          dataFileIndex--) {
        int columnIndex =
            numOfCommonColumns + (dataFileIndex * numOfDataFileColumns) + dataFileColumnIndex;

        TableColumn col = this.getColumnByModelIndex(columnIndex);

        int currentWidth = col.getPreferredWidth();

        if (currentWidth != newWidth) {
          col.setPreferredWidth(newWidth);
        }
      }
    }
  }
 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;
 }