我在单元格中使用带有编辑器(文本、组合和检查)的 SWT 表,我想根据文本正确调整列的大小。我尝试了 TableColumn 的 Pack 方法,但它似乎不起作用。
这是我使用的示例
public static void main( String[] args )
{
Display display = new Display();
Shell shell = new Shell( display );
shell.setLayout( new FillLayout() );
Table table = new Table( shell, SWT.BORDER | SWT.MULTI );
table.setLinesVisible( true );
for ( int i = 0; i < 3; i++ )
{
TableColumn column = new TableColumn( table, SWT.NONE );
column.setWidth( 100 );
}
for ( int i = 0; i < 12; i++ )
{
new TableItem( table, SWT.NONE );
}
TableItem[] items = table.getItems();
for ( int i = 0; i < items.length; i++ )
{
TableEditor editor = new TableEditor( table );
CCombo combo = new CCombo( table, SWT.NONE );
combo.setText( "CCombo" );
combo.add( "item 1" );
combo.add( "A very lengthyyyyyyyyy item 2" );
editor.grabHorizontal = true;
editor.setEditor( combo, items[i], 0 );
editor = new TableEditor( table );
Text text = new Text( table, SWT.NONE );
text.setText( "A very lengthyyyyyyyy text" );
editor.grabHorizontal = true;
editor.setEditor( text, items[i], 1 );
editor = new TableEditor( table );
final Button button = new Button( table, SWT.CHECK );
button.pack();
editor.minimumWidth = button.getSize().x;
editor.horizontalAlignment = SWT.LEFT;
editor.setEditor( button, items[i], 2 );
}
//resize columns
for ( TableColumn columm : table.getColumns() )
columm.pack();
shell.pack();
shell.open();
while ( !shell.isDisposed() )
{
if ( !display.readAndDispatch() )
display.sleep();
}
display.dispose();
}
最佳答案
做这样的事情: 首先
table.setRedraw(false) ;
然后做你的工作 最后将其设置为 true ,如下所示:
table.setRedraw(true);
关于java - 使用编辑器调整 SWT 表的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22154924/