java - 使用编辑器调整 SWT 表的大小

标签 java swt

我在单元格中使用带有编辑器(文本、组合和检查)的 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/

相关文章:

java - 如何在运行时获取 SWT 小部件/控件的名称

java - SWT 从 Composite 中移除边距

java - Jersy/Jetty 服务器未找到 MessageBodyWriter 错误

java - 获取大文件的大小

java - 在外部类中创建新实例时出现 StackOverflow 错误

java - E4 如果 View 已打开,则聚焦透视

java - 如何在每个单元格的基础上右对齐 SWT 表格单元格中的文本

java - 如果内部类扩展外部类,内部类的不同成员行为?

java - 将 ImageIcon 转换为 BufferedImage

java - 使用 FillLayout 隐藏父级中的复合控件