Java Code Examples for javax.swing.JTable#AUTO_RESIZE_OFF

The following examples show how to use javax.swing.JTable#AUTO_RESIZE_OFF . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ETableColumn.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Forces the table to resize given column.
 */
private void resize(int newWidth, JTable table) {
    int oldWidth = getWidth();
    JTableHeader header = table.getTableHeader();
    if (header == null) {
        return;
    }
    header.setResizingColumn(this);
    final int oldMin = getMinWidth();
    final int oldMax = getMaxWidth();
    setMinWidth(newWidth);
    setMaxWidth(newWidth);
    setWidth(newWidth);
    // The trick is to restore the original values
    // after the table has be layouted. During layout this column
    // has fixed width (by setting min==max==preffered)
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            setMinWidth(oldMin);
            setMaxWidth(oldMax);
        }
    });
    Container container;
    if ((header.getParent() == null) ||
            ((container = header.getParent().getParent()) == null) ||
            !(container instanceof JScrollPane)) {
        header.setResizingColumn(null);
        return;
    }
    
    if (!container.getComponentOrientation().isLeftToRight() &&
            ! header.getComponentOrientation().isLeftToRight()) {
        if (table != null) {
            JViewport viewport = ((JScrollPane)container).getViewport();
            int viewportWidth = viewport.getWidth();
            int diff = newWidth - oldWidth;
            int newHeaderWidth = table.getWidth() + diff;
            
            /* Resize a table */
            Dimension tableSize = table.getSize();
            tableSize.width += diff;
            table.setSize(tableSize);
            
            /* If this table is in AUTO_RESIZE_OFF mode and
             * has a horizontal scrollbar, we need to update
             * a view's position.
             */
            if ((newHeaderWidth >= viewportWidth) &&
                    (table.getAutoResizeMode() == JTable.AUTO_RESIZE_OFF)) {
                Point p = viewport.getViewPosition();
                p.x = Math.max(0, Math.min(newHeaderWidth - viewportWidth, p.x + diff));
                viewport.setViewPosition(p);
            }
        }
    }
    header.setResizingColumn(null);
}
 
Example 2
Source File: HeaderlessColumnResizer.java    From orbit-image-analysis with GNU General Public License v3.0 4 votes vote down vote up
public void mouseDragged(MouseEvent e) {
  int mouseX = e.getX();

  TableColumn resizingColumn = table.getTableHeader().getResizingColumn();

  boolean headerLeftToRight =
    table.getTableHeader().getComponentOrientation().isLeftToRight();

  if (resizingColumn != null) {
    int oldWidth = resizingColumn.getWidth();
    int newWidth;
    if (headerLeftToRight) {
      newWidth = mouseX - mouseXOffset;
    } else {
      newWidth = mouseXOffset - mouseX;
    }
    resizingColumn.setWidth(newWidth);

    Container container;
    if ((table.getTableHeader().getParent() == null)
      || ((container = table.getTableHeader().getParent().getParent()) == null)
      || !(container instanceof JScrollPane)) {
      return;
    }

    if (!container.getComponentOrientation().isLeftToRight()
      && !headerLeftToRight) {
      if (table != null) {
        JViewport viewport = ((JScrollPane)container).getViewport();
        int viewportWidth = viewport.getWidth();
        int diff = newWidth - oldWidth;
        int newHeaderWidth = table.getWidth() + diff;

        /* Resize a table */
        Dimension tableSize = table.getSize();
        tableSize.width += diff;
        table.setSize(tableSize);

        /*
         * If this table is in AUTO_RESIZE_OFF mode and has a horizontal
         * scrollbar, we need to update a view's position.
         */
        if ((newHeaderWidth >= viewportWidth)
          && (table.getAutoResizeMode() == JTable.AUTO_RESIZE_OFF)) {
          Point p = viewport.getViewPosition();
          p.x =
            Math.max(0, Math.min(newHeaderWidth - viewportWidth, p.x + diff));
          viewport.setViewPosition(p);

          /* Update the original X offset value. */
          mouseXOffset += diff;
        }
      }
    }
  }
}
 
Example 3
Source File: HeaderlessColumnResizer.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
public void mouseDragged(MouseEvent e) {
  int mouseX = e.getX();

  TableColumn resizingColumn = table.getTableHeader().getResizingColumn();

  boolean headerLeftToRight =
    table.getTableHeader().getComponentOrientation().isLeftToRight();

  if (resizingColumn != null) {
    int oldWidth = resizingColumn.getWidth();
    int newWidth;
    if (headerLeftToRight) {
      newWidth = mouseX - mouseXOffset;
    } else {
      newWidth = mouseXOffset - mouseX;
    }
    resizingColumn.setWidth(newWidth);

    Container container;
    if ((table.getTableHeader().getParent() == null)
      || ((container = table.getTableHeader().getParent().getParent()) == null)
      || !(container instanceof JScrollPane)) {
      return;
    }

    if (!container.getComponentOrientation().isLeftToRight()
      && !headerLeftToRight) {
      if (table != null) {
        JViewport viewport = ((JScrollPane)container).getViewport();
        int viewportWidth = viewport.getWidth();
        int diff = newWidth - oldWidth;
        int newHeaderWidth = table.getWidth() + diff;

        /* Resize a table */
        Dimension tableSize = table.getSize();
        tableSize.width += diff;
        table.setSize(tableSize);

        /*
         * If this table is in AUTO_RESIZE_OFF mode and has a horizontal
         * scrollbar, we need to update a view's position.
         */
        if ((newHeaderWidth >= viewportWidth)
          && (table.getAutoResizeMode() == JTable.AUTO_RESIZE_OFF)) {
          Point p = viewport.getViewPosition();
          p.x =
            Math.max(0, Math.min(newHeaderWidth - viewportWidth, p.x + diff));
          viewport.setViewPosition(p);

          /* Update the original X offset value. */
          mouseXOffset += diff;
        }
      }
    }
  }
}