本文整理匯總了Java中javax.swing.table.TableColumn.setWidth方法的典型用法代碼示例。如果您正苦於以下問題:Java TableColumn.setWidth方法的具體用法?Java TableColumn.setWidth怎麽用?Java TableColumn.setWidth使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.swing.table.TableColumn
的用法示例。
在下文中一共展示了TableColumn.setWidth方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: adjustColumnWidths
import javax.swing.table.TableColumn; //導入方法依賴的package包/類
protected void adjustColumnWidths() {
TableColumnModel colModel = getColumnModel();
for( int i=0; i<colModel.getColumnCount(); i++ ) {
TableColumn tc = colModel.getColumn( i );
int colWidth = 0;
for( int row=0; row<getRowCount(); row++ ) {
colWidth = Math.max( renderer.getPreferredWidth( getValueAt( row, i ) ), colWidth );
}
colWidth = Math.max( colWidth, 30 );
colWidth += getIntercellSpacing().width;
tc.setWidth( colWidth );
tc.setMinWidth( colWidth );
tc.setMaxWidth( colWidth );
tc.setPreferredWidth( colWidth );
tc.setResizable( false );
}
}
示例2: initTableVisualProperties
import javax.swing.table.TableColumn; //導入方法依賴的package包/類
private void initTableVisualProperties(JTable table) {
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setIntercellSpacing(new java.awt.Dimension(0, 0));
// set the color of the table's JViewport
table.getParent().setBackground(table.getBackground());
//we'll get the parents width so we can use that to set the column sizes.
double pw = table.getParent().getParent().getPreferredSize().getWidth();
//#88174 - Need horizontal scrollbar for library names
//ugly but I didn't find a better way how to do it
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
TableColumn column = table.getColumnModel().getColumn(0);
column.setMinWidth(226);
column.setWidth( ((int)pw/2) - 1 );
column.setPreferredWidth( ((int)pw/2) - 1 );
column.setMinWidth(75);
column = table.getColumnModel().getColumn(1);
column.setMinWidth(226);
column.setWidth( ((int)pw/2) - 1 );
column.setPreferredWidth( ((int)pw/2) - 1 );
column.setMinWidth(75);
this.addComponentListener(new TableColumnSizeComponentAdapter(table));
}
示例3: showColumn
import javax.swing.table.TableColumn; //導入方法依賴的package包/類
void showColumn(TableColumn column, ProfilerTable table) {
column.setMaxWidth(Integer.MAX_VALUE);
Integer width = hiddenColumnWidths.remove(column.getModelIndex());
column.setWidth(width != null ? width.intValue() :
getDefaultColumnWidth(column.getModelIndex()));
column.setMinWidth(minColumnWidth);
int toResizeIndex = getFitWidthColumn();
if (column.getModelIndex() == toResizeIndex) {
Enumeration<TableColumn> columns = getColumns();
while (columns.hasMoreElements()) {
TableColumn col = columns.nextElement();
int index = col.getModelIndex();
if (col.getModelIndex() != toResizeIndex && isColumnVisible(col))
col.setWidth(getDefaultColumnWidth(index));
}
table.doLayout();
}
}
示例4: FitTableColumns
import javax.swing.table.TableColumn; //導入方法依賴的package包/類
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);
}
}
示例5: setColumnWidth
import javax.swing.table.TableColumn; //導入方法依賴的package包/類
final public int setColumnWidth(int intPcolumnIndex, int intPwidth) {
final int intLidentifier = this.getColumnIdentifier(intPcolumnIndex);
final JTableHeader objLjTableHeader = this.getTableHeader();
final TableColumn objLtableColumn = this.getColumnModel().getColumn(intPcolumnIndex);
objLjTableHeader.setResizingColumn(objLtableColumn);
final int intLwidth =
intPwidth == Constants.bytS_UNCLASS_NO_VALUE
? intLidentifier == Constants.bytS_UNCLASS_NO_VALUE
? Constants.bytS_UNCLASS_NO_VALUE
: this.intGidentifierColumnWidthA[intLidentifier]
: intPwidth;
if (intLwidth != Constants.bytS_UNCLASS_NO_VALUE) {
if (intLidentifier != Constants.bytS_UNCLASS_NO_VALUE) {
this.intGidentifierColumnWidthA[intLidentifier] = intLwidth;
}
objLtableColumn.setWidth(intLwidth);
}
return intLwidth;
}
示例6: adjustColumnWidths
import javax.swing.table.TableColumn; //導入方法依賴的package包/類
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 );
}
}
示例7: componentResized
import javax.swing.table.TableColumn; //導入方法依賴的package包/類
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 );
}
示例8: createTableColumn
import javax.swing.table.TableColumn; //導入方法依賴的package包/類
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);
}
}
};
}
示例9: setDefaultColumnWidth
import javax.swing.table.TableColumn; //導入方法依賴的package包/類
void setDefaultColumnWidth(int width) {
defaultColumnWidth = width;
Enumeration<TableColumn> columns = getColumns();
while (columns.hasMoreElements()) {
TableColumn column = columns.nextElement();
int index = column.getModelIndex();
if (defaultColumnWidths == null || defaultColumnWidths.get(index) == null)
if (index != fitWidthColumn) column.setWidth(width);
}
}
示例10: fitTableColumns
import javax.swing.table.TableColumn; //導入方法依賴的package包/類
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: esconderColuna
import javax.swing.table.TableColumn; //導入方法依賴的package包/類
private void esconderColuna(int indice) {
TableColumn coluna = getColumnModel().getColumn(indice);
coluna.setMinWidth(0);
coluna.setMaxWidth(0);
coluna.setWidth(0);
coluna.setPreferredWidth(0);
doLayout();
}
示例12: init
import javax.swing.table.TableColumn; //導入方法依賴的package包/類
private void init() {
setOpaque( false );
getSelectionModel().clearSelection();
getSelectionModel().setAnchorSelectionIndex(-1);
getSelectionModel().setLeadSelectionIndex(-1);
setAutoscrolls( false );
setShowHorizontalLines(false);
setShowVerticalLines( false);
setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
setTableHeader( null );
// Calc row height here so that TableModel can adjust number of columns.
calcRowHeight(getOffscreenGraphics());
//mouse click into the table performs the switching
addMouseListener( new MouseAdapter() {
@Override
public void mousePressed( MouseEvent e ) {
int row = rowAtPoint( e.getPoint() );
int col = columnAtPoint( e.getPoint() );
if( row >= 0 && col >= 0 ) {
if( select( row, col ) ) {
performSwitching();
}
}
}
});
//icon for top-level items with sub-items
rightArrowLabel.setIcon( new ArrowIcon() );
rightArrowLabel.setIconTextGap( 2 );
rightArrowLabel.setHorizontalTextPosition( JLabel.LEFT );
topItemPanel.setLayout( new BorderLayout(5, 0) );
topItemPanel.add( rightArrowLabel, BorderLayout.EAST );
topItemPanel.getAccessibleContext().setAccessibleDescription( NbBundle.getMessage(Table.class, "ACD_OTHER_EDITORS") );
topItemPanel.setBorder( BorderFactory.createEmptyBorder( 0, 0, 0, 2) );
//adjust column widths to accomodate the widest item in each column
for( int col=0; col<getColumnCount(); col++ ) {
if( getSwitcherModel().isTopItemColumn( col ) )
adjustColumnWidths( col );
}
//include the width of vertical scrollbar if there are too many rows
int maxRowCount = getSwitcherModel().getMaxRowCount();
if( maxRowCount > MAX_VISIBLE_ROWS && getRowCount() <= MAX_VISIBLE_ROWS ) {
JScrollPane scroll = new JScrollPane();
scroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
int scrollWidth = scroll.getVerticalScrollBar().getPreferredSize().width;
TableColumn tc = getColumnModel().getColumn( getColumnCount()-1 );
tc.setMaxWidth( tc.getMaxWidth() + scrollWidth );
tc.setPreferredWidth( tc.getPreferredWidth() + scrollWidth );
tc.setWidth( tc.getWidth() + scrollWidth );
}
}
示例13: doLayout
import javax.swing.table.TableColumn; //導入方法依賴的package包/類
public void doLayout() {
ProfilerColumnModel cModel = _getColumnModel();
JTableHeader header = getTableHeader();
TableColumn res = header == null ? null : header.getResizingColumn();
if (res != null) {
// Resizing column
int delta = getWidth() - cModel.getTotalColumnWidth();
TableColumn next = cModel.getNextVisibleColumn(res);
if (res == next) {
res.setWidth(res.getWidth() + delta);
} else {
next.setWidth(next.getWidth() + delta);
}
} else {
// Resizing table
int toResizeIndex = cModel.getFitWidthColumn();
if (toResizeIndex == -1) {
super.doLayout();
} else {
Enumeration<TableColumn> columns = cModel.getColumns();
TableColumn toResizeColumn = null;
int columnsWidth = 0;
while (columns.hasMoreElements()) {
TableColumn column = columns.nextElement();
if (column.getModelIndex() == toResizeIndex) {
if (!cModel.isColumnVisible(column)) {
super.doLayout();
return;
}
toResizeColumn = column;
} else {
columnsWidth += column.getWidth();
}
}
if (toResizeColumn != null) toResizeColumn.setWidth(getWidth() - columnsWidth);
// instead of super.doLayout()
layout();
}
}
}
示例14: pack
import javax.swing.table.TableColumn; //導入方法依賴的package包/類
public void pack(ExtendedJTable table) {
if (!table.isShowing()) {
// throw new IllegalStateException("Table must be showed in order to pack it.");
// return silently;
return;
}
if (table.getTableHeader() == null) {
return;
}
if (table.getColumnCount() == 0) {
return;
}
int width[] = new int[table.getColumnCount()];
int total = 0;
for (int col = 0; col < width.length; col++) {
width[col] = preferredWidth(table, col);
total += width[col];
}
int extra = table.getVisibleRect().width - total;
if (extra > 0) {
if (distributeExtraArea) {
int bonus = extra / table.getColumnCount();
for (int i = 0; i < width.length; i++) {
width[i] += bonus;
}
extra -= bonus * table.getColumnCount();
}
width[width.length - 1] += extra;
}
TableColumnModel columnModel = table.getColumnModel();
for (int col = width.length - 1; col >= 0; col--) {
TableColumn tableColumn = columnModel.getColumn(col);
table.getTableHeader().setResizingColumn(tableColumn);
tableColumn.setWidth(width[col]);
}
}
示例15: mouseClicked
import javax.swing.table.TableColumn; //導入方法依賴的package包/類
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
JTableHeader header = (JTableHeader) e.getSource();
TableColumn tableColumn = getResizingColumn(header, e.getPoint());
if (tableColumn == null) {
return;
}
JTable table = header.getTable();
if ((e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK) {
if (table instanceof ExtendedJTable) {
((ExtendedJTable) table).pack();
e.consume();
}
} else {
int col = header.getColumnModel().getColumnIndex(tableColumn.getIdentifier());
int width = (int) header.getDefaultRenderer()
.getTableCellRendererComponent(table, tableColumn.getIdentifier(), false, false, -1, col)
.getPreferredSize().getWidth();
int firstRow = 0;
int lastRow = table.getRowCount();
if (table instanceof ExtendedJTable) {
ExtendedJScrollPane scrollPane = ((ExtendedJTable) table).getExtendedScrollPane();
if (scrollPane != null) {
JViewport viewport = scrollPane.getViewport();
Rectangle viewRect = viewport.getViewRect();
if (viewport.getHeight() < table.getHeight()) {
firstRow = table.rowAtPoint(new Point(0, viewRect.y));
firstRow = Math.max(0, firstRow);
lastRow = table.rowAtPoint(new Point(0, viewRect.y + viewRect.height - 1));
lastRow = Math.min(lastRow, table.getRowCount());
}
}
}
for (int row = firstRow; row < lastRow; row++) {
int preferedWidth = (int) table.getCellRenderer(row, col)
.getTableCellRendererComponent(table, table.getValueAt(row, col), false, false, row, col)
.getPreferredSize().getWidth();
width = Math.max(width, preferedWidth);
}
header.setResizingColumn(tableColumn); // this line is very important
tableColumn.setWidth(width + table.getIntercellSpacing().width);
e.consume();
}
}
}