如何在SWT表中设置可见行,包括单元格中的小部件?

时间:2014-10-09 04:20:21

标签: java widget swt border

我使用SWT创建了一个表,并使用TableEditor在表格单元格中包含了SWT小部件,例如Label,Text。但现在我的问题是,表格单元格的轮廓现在不可见。我想让所有列和行的边界可见。怎么做?设置table.setLinesVisible(true)并不成功!

       Table table = new Table(detailArea, SWT.BORDER | SWT.MULTI);
       table.setLinesVisible(true);
       for (int i = 0; i < 4; i++) {
          TableColumn column = new TableColumn(table, SWT.NONE);
          column.setWidth(100);
       }

        TableItem x1= new TableItem(table, SWT.NONE);
        TableEditor editorw = new TableEditor(table);

        Label lblName =new Label(table, SWT.NONE);
        lblName.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        lblName.setText("Language");           
        editorw.grabHorizontal = true;
        editorw.setEditor(lblName, x1, 0);

        editorw = new TableEditor(table);
        Text txtLang = new Text(table, SWT.BORDER);
        editorw.grabHorizontal = true;
        editorw.setEditor(txtLang, x1, 1);                  

        editorw = new TableEditor(table);
        Label lblReference = new Label(table,
                SWT.NONE);
        lblReference.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        lblReference.setText("Value");
        editorw.grabHorizontal = true;
        editorw.setEditor(lblReference, x1, 2);

        editorw = new TableEditor(table);
        Text txtValue= new Text(table, SWT.BORDER);
        editorw.grabHorizontal = true;
        editorw.setEditor(txtValue, x1, 3);

        /////table row 2
        TableItem x2= new TableItem(table, SWT.NONE);
        editorw = new TableEditor(table);

        Label lblName2 =new Label(table, SWT.NONE);
        lblName2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        lblName2.setText("Language");           
        editorw.grabHorizontal = true;
        editorw.setEditor(lblName2, x2, 0);

        editorw = new TableEditor(table);
        Text txtLang2 = new Text(table, SWT.BORDER);
        editorw.grabHorizontal = true;
        editorw.setEditor(txtLang2, x2, 1);

        editorw = new TableEditor(table);
        Label lblReference2 = new Label(table,
                  SWT.NONE);
        lblReference2.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        lblReference2.setText("Value");
        editorw.grabHorizontal = true;
        editorw.setEditor(lblReference2, x2, 2);

        editorw = new TableEditor(table);
        Text txtValue2= new Text(table, SWT.BORDER);
        editorw.grabHorizontal = true;
        editorw.setEditor(txtValue2, x2, 3);


        table.setLinesVisible(true);

以下是当前输出:

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以将编辑器小部件(标签和文本)包装到Composite中。可以为复合提供边框和可以指定边距的布局:

Composite composite = new Composite( table, SWT.BORDER );
GridLayout layout = new GridLayout();
composite.setLayout( layout );
Label label = new Label( composite, SWT.NONE );
GridData gridData = new GridData( SWT.FILL, SWT.FILL, true, true );
gridData.verticalIndent = 2;
gridData.horizontalIndent = 2;
label.setLayoutData( gridData );
editor.setEditor( composite, item, 0 );

另一种方法可能是owner draw table items特别是请参阅可用于绘制背景的EraseItem事件。但是我担心,由于你不能影响TableEditor的布局,所有者绘制的边框将被编辑器小部件隐藏。

请注意,这种方法(无论是初始方法还是复合包装方法)都不能很好地扩展几个hundret行。对于每一行,将创建4或8个小部件。根据客户端计算机的性能,在创建多个hundret小部件并最终耗尽句柄时,它会变慢。