javax.swing.table.TableColumn#setWidth ( )源码实例Demo

下面列出了javax.swing.table.TableColumn#setWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: osp   文件: GridDataTable.java
/**
 * Constructor GridDataTable
 * @param griddata
 * @param component
 */
public GridDataTable(GridData griddata, int component) {
  super();
  refreshTimer.setRepeats(false);
  refreshTimer.setCoalesce(true);
  tableModel = new GridTableModel(griddata, component);
  setModel(tableModel);
  setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  String name = getColumnName(0);
  TableColumn column = getColumn(name);
  int width = 20;
  column.setMinWidth(width);
  column.setResizable(true);
  // column.setMaxWidth(width);
  column.setWidth(width);
  width = 60;
  for(int i = 1, n = getColumnCount(); i<n; i++) {
    name = getColumnName(i);
    column = getColumn(name);
    column.setMinWidth(width);
    column.setWidth(width);
    column.setResizable(true);
  }
  sizeColumnsToFit(0);
}
 
源代码2 项目: ghidra   文件: GTableWidget.java
/**
 * Sets the column preferred widths.  If you give less widths then there are columns, then
 * the widths will be applied in order, with the remaining columns going untouched.
 * <p>
 * Note: this method needs to be called after building your columns. So, call this after
 * making any calls to {@link #addColumn(AbstractDynamicTableColumn)}.
 * <p>
 * <b>WARNING!</b>  If you set the widths to a size that is smaller than the total display,
 * then the table model will apply the extra space equally across your columns, resulting
 * in sizes that you did not set.  So, the best way to use this method is to set the
 * actual preferred size for your small columns and then set a very large size (400 or so)
 * for your columns that can be any size.
 * <p>
 *
 * @param widths the widths to apply
 */
public void setColumnPreferredWidths(int... widths) {
	int columnCount = table.getColumnCount();
	int n = Math.min(widths.length, columnCount);
	TableColumnModel model = table.getColumnModel();
	for (int i = 0; i < n; i++) {
		TableColumn column = model.getColumn(i);
		int width = widths[i];
		if (width == 75) {
			// Horrible Code: we have special knowledge that a value of 75 is the default
			// column size, which we use in TableColumnModelState to signal that we can
			// override the size.  So, if the user sets that value, then change it to
			// override our algorithm.
			width = 76;
		}
		column.setWidth(width);
		column.setPreferredWidth(widths[i]);
	}
}
 
源代码3 项目: dagger-intellij-plugin   文件: ShowUsagesAction.java
private static int calcMaxWidth(JTable table) {
  int colsNum = table.getColumnModel().getColumnCount();

  int totalWidth = 0;
  for (int col = 0; col < colsNum - 1; col++) {
    TableColumn column = table.getColumnModel().getColumn(col);
    int preferred = column.getPreferredWidth();
    int width = Math.max(preferred, columnMaxWidth(table, col));
    totalWidth += width;
    column.setMinWidth(width);
    column.setMaxWidth(width);
    column.setWidth(width);
    column.setPreferredWidth(width);
  }

  totalWidth += columnMaxWidth(table, colsNum - 1);

  return totalWidth;
}
 
源代码4 项目: consulo   文件: OrderPanel.java
public void setCheckboxColumnName(final String name) {
  final int width;
  if (StringUtil.isEmpty(name)) {
    CHECKBOX_COLUMN_NAME = "";
    width = new JCheckBox().getPreferredSize().width;
  }
  else {
    CHECKBOX_COLUMN_NAME = name;
    final FontMetrics fontMetrics = myEntryTable.getFontMetrics(myEntryTable.getFont());
    width = fontMetrics.stringWidth(" " + name + " ") + 4;
  }

  final TableColumn checkboxColumn = myEntryTable.getColumnModel().getColumn(getCheckboxColumn());
  checkboxColumn.setWidth(width);
  checkboxColumn.setPreferredWidth(width);
  checkboxColumn.setMaxWidth(width);
  checkboxColumn.setMinWidth(width);
}
 
@Override
protected void updateEditorLayout() {
    int currentColumnCount = this.getTableModel().getColumnCount();
    int[] columnWidths = getFileEditorState().getColumnWidths();
    int prevColumnCount = columnWidths.length;
    if (prevColumnCount != currentColumnCount) {
        columnWidths = ArrayUtil.realloc(columnWidths, currentColumnCount);
        if (prevColumnCount < currentColumnCount) {
            Arrays.fill(columnWidths, prevColumnCount, currentColumnCount, CsvEditorSettings.getInstance().getTableDefaultColumnWidth());
        }
        getFileEditorState().setColumnWidths(columnWidths);
    }

    float zoomFactor = getZoomFactor();
    for (int i = 0; i < currentColumnCount; ++i) {
        TableColumn column = this.tblEditor.getColumnModel().getColumn(i);
        column.setPreferredWidth(Math.round(columnWidths[i] * zoomFactor));
        column.setWidth(Math.round(columnWidths[i] * zoomFactor));
    }

    this.updateRowHeights(null);
    panelInfo.setVisible(getFileEditorState().showInfoPanel());
}
 
源代码6 项目: magarena   文件: TableColumnAdjuster.java
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, tableColumn.getWidth());

    table.getTableHeader().setResizingColumn(tableColumn);
    tableColumn.setWidth(width);
}
 
源代码7 项目: SimpleERP   文件: QueryDailyIncome.java
public static void FitTableColumns(JTable myTable){
	  JTableHeader header = myTable.getTableHeader();
	     int rowCount = myTable.getRowCount();
	     Enumeration columns = myTable.getColumnModel().getColumns();
	     while(columns.hasMoreElements()){
	         TableColumn column = (TableColumn)columns.nextElement();
	         int col = header.getColumnModel().getColumnIndex(column.getIdentifier());
	         int width = (int)myTable.getTableHeader().getDefaultRenderer()
	                 .getTableCellRendererComponent(myTable, column.getIdentifier()
	                         , false, false, -1, col).getPreferredSize().getWidth();
	         for(int row = 0; row<rowCount; row++){
	             int preferedWidth = (int)myTable.getCellRenderer(row, col).getTableCellRendererComponent(myTable,
	               myTable.getValueAt(row, col), false, false, row, col).getPreferredSize().getWidth();
	             width = Math.max(width, preferedWidth);
	         }
	         header.setResizingColumn(column); // ���к���Ҫ
	         column.setWidth(width+myTable.getIntercellSpacing().width);
	     }
}
 
源代码8 项目: osp   文件: DataToolStatsTable.java
/**
 *  Refresh the cell widths in the table.
 */
public void refreshCellWidths() {
  // set width of columns
  if(getColumnCount()!=dataTable.getColumnCount()) {
    return;
  }
  for(int i = 0; i<getColumnCount(); i++) {
    TableColumn propColumn = getColumnModel().getColumn(i);
    TableColumn dataColumn = dataTable.getColumnModel().getColumn(i);
    propColumn.setMaxWidth(dataColumn.getWidth());
    propColumn.setMinWidth(dataColumn.getWidth());
    propColumn.setWidth(dataColumn.getWidth());
  }
}
 
源代码9 项目: netbeans   文件: CustomizerSources.java
public void componentResized(ComponentEvent evt){
    double pw = table.getParent().getParent().getSize().getWidth();
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    TableColumn column = table.getColumnModel().getColumn(0);
    column.setWidth( ((int)pw/2) - 1 );
    column.setPreferredWidth( ((int)pw/2) - 1 );
    column = table.getColumnModel().getColumn(1);
    column.setWidth( ((int)pw/2) - 1 );
    column.setPreferredWidth( ((int)pw/2) - 1 );
}
 
源代码10 项目: Method_Trace_Tool   文件: Utils.java
public static void fitTableColumns(JTable myTable) {

        JTableHeader header = myTable.getTableHeader();
        int rowCount = myTable.getRowCount();
        Enumeration columns = myTable.getColumnModel().getColumns();
        while (columns.hasMoreElements()) {
            TableColumn column = (TableColumn) columns.nextElement();
            int col = header.getColumnModel().getColumnIndex(
                    column.getIdentifier());
            int width = (int) myTable
                    .getTableHeader()
                    .getDefaultRenderer()
                    .getTableCellRendererComponent(myTable,
                            column.getIdentifier(), false, false, -1, col)
                    .getPreferredSize().getWidth();
            for (int row = 0; row < rowCount; row++) {
                int preferedWidth = (int) myTable
                        .getCellRenderer(row, col)
                        .getTableCellRendererComponent(myTable,
                                myTable.getValueAt(row, col), false, false,
                                row, col).getPreferredSize().getWidth();
                width = Math.max(width, preferedWidth);
            }
            header.setResizingColumn(column); // 此行很重要
            column.setWidth(width + myTable.getIntercellSpacing().width + 4);// 使表格看起来不是那么拥挤,起到间隔作用
        }
    }
 
源代码11 项目: consulo   文件: TableColumnAnimator.java
@Override
public void actionPerformed(ActionEvent e) {
  if (myColumns.isEmpty()) {
    stop();
    if (myDone != null) {
      SwingUtilities.invokeLater(myDone);
    }
    return;
  }

  final TableColumn c = myColumns.get(0).first;
  if (!added) {
    myTable.addColumn(c);
    c.setMaxWidth(0);
    c.setPreferredWidth(0);
    c.setWidth(0);
    added = true;
  }

  final int prefWidth = myColumns.get(0).second.intValue();
  int width = c.getWidth();
  width = Math.min(width + myStep, prefWidth);
  c.setMaxWidth(width);
  c.setPreferredWidth(width);
  c.setWidth(width);

  if (width == prefWidth) {
    added = false;
    myColumns.remove(0);
    //c.setMaxWidth(oldMaxWidth);
  }
}
 
源代码12 项目: netbeans   文件: Table.java
private void adjustColumnWidths( int topColumn ) {
    TableColumnModel colModel = getColumnModel();
    int colWidth = 0;
    int subColWidth = -1;
    for( int row=0; row<getRowCount(); row++ ) {
        Item item = ( Item ) getValueAt( row, topColumn );
        Component ren = prepareRenderer( this.getCellRenderer( row, topColumn ), row, topColumn, item, true );
        int prefWidth = ren.getPreferredSize().width;
        colWidth = Math.max( colWidth, prefWidth );
        
        if( null != item && item.hasSubItems() && topColumn+1 < getColumnCount()
                && !getSwitcherModel().isTopItemColumn( topColumn+1 ) ) {
            Item[] subItems = item.getActivatableSubItems();
            for( int i=0; i<subItems.length; i++ ) {
                ren = prepareRenderer( this.getCellRenderer( 0, topColumn+1 ), 0, topColumn+1, subItems[i], true );
                prefWidth = ren.getPreferredSize().width;
                subColWidth = Math.max( subColWidth, prefWidth );
            }
        }
    }
    colWidth = Math.min( colWidth, MAX_TOP_COLUMN_WIDTH );
    TableColumn tc = colModel.getColumn( topColumn );
    tc.setPreferredWidth( colWidth );
    tc.setWidth( colWidth );
    tc.setMaxWidth( colWidth );

    if( subColWidth > 0 ) {
        subColWidth = Math.min( subColWidth, MAX_SUB_COLUMN_WIDTH );
        tc = colModel.getColumn( topColumn+1 );
        tc.setPreferredWidth( subColWidth );
        tc.setWidth( subColWidth );
        tc.setMaxWidth( subColWidth );
    }
}
 
源代码13 项目: rapidminer-studio   文件: ExtendedJTable.java
public void unpack() {
	JTableHeader header = getTableHeader();
	if (header != null) {
		for (int c = 0; c < getColumnCount(); c++) {
			TableColumn tableColumn = header.getColumnModel().getColumn(c);
			header.setResizingColumn(tableColumn); // this line is very important

			int width = DEFAULT_COLUMN_WIDTH;
			if (getWidth() / width > getColumnCount()) {
				width = getWidth() / getColumnCount();
			}
			tableColumn.setWidth(width);
		}
	}
}
 
源代码14 项目: netbeans   文件: ProfilerColumnModel.java
TableColumn createTableColumn(int columnIndex) {
    return new TableColumn(columnIndex) {
        public void setWidth(int width) {
            if (getMaxWidth() == 0 && getWidth() == 0) {
                TableColumn c = getPreviousVisibleColumn(this);
                if (refWidth == -1) refWidth = c.getWidth();
                c.setWidth(refWidth + width);
            } else {
                super.setWidth(width);
            }
        }                
    };
}
 
源代码15 项目: azure-devops-intellij   文件: FormattedTable.java
/**
 * Format columns so that they are fit to the data they are holding. If a deafultLongColumn is specified that column
 * will take up any extra space in the table
 */
private void formatColumns() {
    final Enumeration<TableColumn> columnList = getColumnModel().getColumns();
    while (columnList.hasMoreElements()) {
        final TableColumn column = columnList.nextElement();

        // if column is the defaultLongColumn allow it to fill in the extra table space
        if (defaultLongColumn != null && defaultLongColumn.toLowerCase().equals(column.getHeaderValue().toString().toLowerCase())) {
            column.setPreferredWidth(Short.MAX_VALUE);
        } else {

            // set min column width to the column header
            final TableCellRenderer headerRenderer = column.getHeaderRenderer() != null ? column.getHeaderRenderer() : getTableHeader().getDefaultRenderer();
            final Component headerComponent = headerRenderer.getTableCellRendererComponent(this, column.getHeaderValue(), false, false, -1, column.getModelIndex());
            int maxWidth = headerComponent.getPreferredSize().width + JBUI.scale(20); // added some padding so not hugging the text

            // don't check the width of every row to save time if there are many items
            final int maxRowsToCheck = Math.min(MAX_ROWS_CHECKED, getRowCount());
            for (int row = 0; row < maxRowsToCheck; row++) {
                final TableCellRenderer renderer = getCellRenderer(row, column.getModelIndex());
                final Component comp = prepareRenderer(renderer, row, column.getModelIndex());
                maxWidth = Math.max(comp.getPreferredSize().width, maxWidth);
            }

            column.setMinWidth(Math.min(maxWidth + JBUI.scale(10), DEFAULT_COLUMN_WIDTH));
            column.setWidth(column.getMinWidth());
        }
    }
}
 
源代码16 项目: weblaf   文件: SwingUtils.java
/**
 * Packs table column at the specified index to its preferred width.
 *
 * @param table  table to process
 * @param col    column index
 * @param margin column side margin
 */
public static void packColumnWidth ( @NotNull final JTable table, final int col, final int margin )
{
    final DefaultTableColumnModel columnModel = ( DefaultTableColumnModel ) table.getColumnModel ();
    final TableColumn column = columnModel.getColumn ( col );
    int width;

    // Header renderer
    TableCellRenderer renderer = column.getHeaderRenderer ();
    if ( renderer == null )
    {
        renderer = table.getTableHeader ().getDefaultRenderer ();
    }

    // Header width
    Component rendererComponent = renderer.getTableCellRendererComponent ( table, column.getHeaderValue (), false, false, 0, 0 );
    width = rendererComponent.getPreferredSize ().width;

    // Cells width
    for ( int r = 0; r < table.getRowCount (); r++ )
    {
        renderer = table.getCellRenderer ( r, col );
        rendererComponent = renderer.getTableCellRendererComponent ( table, table.getValueAt ( r, col ), false, false, r, col );
        width = Math.max ( width, rendererComponent.getPreferredSize ().width );
    }

    // Margin
    width += 2 * margin;

    // Final values
    column.setPreferredWidth ( width );
    column.setWidth ( width );
}
 
源代码17 项目: magarena   文件: TableColumnAdjuster.java
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);
    }
}
 
源代码18 项目: osp   文件: DataToolPropsTable.java
/**
 *  Refresh the cell widths in the table.
 */
public void refreshCellWidths() {
  // set width of columns
  if(getColumnCount()!=dataTable.getColumnCount()) {
    return;
  }
  for(int i = 0; i<getColumnCount(); i++) {
    TableColumn propColumn = getColumnModel().getColumn(i);
    TableColumn dataColumn = dataTable.getColumnModel().getColumn(i);
    propColumn.setMaxWidth(dataColumn.getWidth());
    propColumn.setMinWidth(dataColumn.getWidth());
    propColumn.setWidth(dataColumn.getWidth());
  }
}
 
源代码19 项目: ghidra   文件: TableColumnModelState.java
/**
 * Configure the columns in this model with their preferred size.
 */
private void setDefaultPreferredColumnSizes() {

	//
	// 					Unusual Code Alert!
	// The table model wants to resize the columns such that they all get an equal share
	// of any available width upon initialization.  This defeats the preferred size of
	// a column if it is specified (which it is usually not).  To override this badness,
	// we will set all preferred sizes AND then for all columns without a preferred size,
	// specify a large value, which causes Java's layout algorithm to have less remaining
	// width to divided amongst all the table columns.  Essentially, we need to make the
	// total width of all columns larger than the table size.  We do this by giving large
	// default width values.
	//
	// FYI, Java's badness happens inside of JTable.doLayout().
	//
	// To easily specify a preferred size for a column, do so in your DynamicTableColumn's
	// getColumnPreferredWidth() method.   If your model is not dynamic, then you have
	// to specify the preferred size manually after you construct your table by grabbing
	// its ColumnModel.
	//

	TableModel model = table.getUnwrappedTableModel();
	if (!(model instanceof AbstractGTableModel<?>)) {
		return;
	}

	AbstractGTableModel<?> gModel = (AbstractGTableModel<?>) model;
	List<TableColumn> columnList = columnModel.getAllColumns();
	for (TableColumn col : columnList) {
		int defaultPreferred = col.getPreferredWidth();
		if (defaultPreferred > 0 && defaultPreferred != 75) {
			// honor any saved preferred size (ignoring the magic default value found
			// inside of TableColumn)
			col.setWidth(defaultPreferred);
			continue;
		}

		int preferred = gModel.getPreferredColumnWidth(col.getModelIndex());
		if (preferred < 15) {
			preferred = LARGE_DEFAULT_COL_WIDTH;
		}
		int size = preferred;
		col.setWidth(size);
		col.setPreferredWidth(size);
	}
}
 
@Override
protected JPanel createVariablesPanel() {
    JPanel variablesBorderPanel = new JPanel();
    BoxLayout layout = new BoxLayout(variablesBorderPanel, BoxLayout.PAGE_AXIS);
    variablesBorderPanel.setLayout(layout);

    AbstractButton addVariableButton =
            ToolButtonFactory.createButton(UIUtils.loadImageIcon(Bundle.Icon_Add()), false);
    addVariableButton.setText(Bundle.CTL_Button_Add_Variable_Text());
    addVariableButton.setMaximumSize(new Dimension(150, controlHeight));
    addVariableButton.setAlignmentX(Component.LEFT_ALIGNMENT);

    AbstractButton addDependentVariableButton =
            ToolButtonFactory.createButton(UIUtils.loadImageIcon(Bundle.Icon_Add()), false);
    addDependentVariableButton.setText(Bundle.CTL_Button_Add_PDVariable_Text());
    addDependentVariableButton.setMaximumSize(new Dimension(250, controlHeight));
    addDependentVariableButton.setAlignmentX(Component.LEFT_ALIGNMENT);

    JPanel buttonsPannel = new JPanel(new SpringLayout());
    buttonsPannel.add(addVariableButton);
    buttonsPannel.add(addDependentVariableButton);
    SpringUtilities.makeCompactGrid(buttonsPannel, 1, 2, 0, 0, 0, 0);
    buttonsPannel.setAlignmentX(Component.LEFT_ALIGNMENT);
    variablesBorderPanel.add(buttonsPannel);

    varTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
    varTable.setRowHeight(controlHeight);
    int widths[] = {controlHeight, 3 * controlHeight, 10 * controlHeight};
    for (int i = 0; i < widths.length; i++) {
        TableColumn column = varTable.getColumnModel().getColumn(i);
        column.setPreferredWidth(widths[i]);
        column.setWidth(widths[i]);

    }
    JScrollPane scrollPane = new JScrollPane(varTable);
    scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
    variablesBorderPanel.add(scrollPane);
    variablesBorderPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
    Dimension variablesPanelDimension =
            new Dimension((formWidth - 3 * DEFAULT_PADDING) / 2 - 2 * DEFAULT_PADDING, 130);
    variablesBorderPanel.setMinimumSize(variablesPanelDimension);
    variablesBorderPanel.setMaximumSize(variablesPanelDimension);
    variablesBorderPanel.setPreferredSize(variablesPanelDimension);

    addVariableButton.addActionListener(e -> {
        newOperatorDescriptor.getVariables().add(new SystemVariable("key", ""));
        varTable.revalidate();
    });

    addDependentVariableButton.addActionListener(e -> {
        newOperatorDescriptor.getVariables().add(new SystemDependentVariable("key", ""));
        varTable.revalidate();
    });

    return variablesBorderPanel;
}