How to set lines visible in SWT table, including w

2019-02-15 22:01发布

I created a table with SWT and I included SWT widgets such as Label, Text in the table cells using TableEditor. But now my problem is, the outlines of the table cells are not visible now. I want to make visible all the borders of columns and rows. How to do it? Setting table.setLinesVisible(true) is not a success!

       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);

Following is the current output:

enter image description here

1条回答
我想做一个坏孩纸
2楼-- · 2019-02-15 22:32

You can wrap the editor widgets (label and text) into a Composite. The composite can be given a border and a layout that can specify a margin:

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 );

A different approach might be owner draw table items In particular see the EraseItem event that can be used to draw the background. However I am afraid that since you cannot influence the TableEditor's layout, the owner-drawn borders will be hidden by the editor widgets.

Note that this approach (neither your initial nor the composite-wrapped one) will not scale well for several hundret rows. For each row, 4 or 8 widgets will be created. Depending on the client computers performance it will slow down when creating several hundret widgets and eventually run out of handles.

查看更多
登录 后发表回答