TableViewer居中布局

时间:2012-08-27 18:11:46

标签: swt jface

现在我已在TableViewer成功添加了TitleAreaDialog 我想弄清楚我遇到的一些布局问题 我可以在对话框窗口中控制tableViewer的布局和位置 现在桌子出现在右侧 我希望它以我的父复合材料为中心 我可以在createDialogArea方法中将TableViewer添加到父布局吗? 我将在Dialog中添加更多复合材料,并希望能够控制它们的去向和外观。 我的表格在表格的末尾显示了一个半空的列,有没有办法删除它?

Dialog w/TableViewer

类似的东西:

GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = true;
gridData.horizontalAlignment = GridData.CENTER;
TableViewer d = createTableViewer(area);
d.setLayoutData(gridData);

这是我的createDialogArea代码。

protected Control createDialogArea(Composite parent) {
   final Composite area = new Composite(parent, SWT.NULL);
   final GridLayout gridLayout = new GridLayout();
   gridLayout.marginWidth = 15;
   gridLayout.marginHeight = 10;
   area.setLayout(gridLayout);

   TableViewer d = createTableViewer(area);

   return area;
}

这是我的tableviewer代码

 private TableViewer createTableViewer(Composite parent) {
    viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
    createColumns(parent, viewer);
    final Table table = viewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    viewer.setContentProvider(new ArrayContentProvider());
    viewer.setInput(AplotSelectedDataTable.getInstance().getArrayData());

    // Layout the viewer
    GridData gridData = new GridData(SWT.CENTER);
    gridData.verticalAlignment = GridData.FILL;
    gridData.horizontalSpan = 2;
    gridData.grabExcessHorizontalSpace = true;
    gridData.grabExcessVerticalSpace = true;
    gridData.horizontalAlignment = GridData.FILL;
    viewer.getControl().setLayoutData(gridData);
  return viewer;
 }

1 个答案:

答案 0 :(得分:1)

以下是一个可以帮助您解决布局问题的小例子:

public class TestClass extends Dialog {

    private TableViewer viewer;

    protected TestClass(Shell parentShell) {
        super(parentShell);
    }

    protected Control createDialogArea(Composite parent) {
        final Composite area = new Composite(parent, SWT.NONE);
        final GridLayout gridLayout = new GridLayout(2, true);
        gridLayout.marginWidth = 15;
        gridLayout.marginHeight = 10;
        area.setLayout(gridLayout);

        area.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        createButtons(area);
        createTableViewer(area);

        return area;
    }

    private void createButtons(Composite parent)
    {
        Button button1 = new Button(parent, SWT.PUSH);
        button1.setText("Button1");
        button1.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true));

        Button button2 = new Button(parent, SWT.PUSH);
        button2.setText("Button2");
        button2.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true));
    }

    private void createTableViewer(Composite parent) {
        viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
                | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
        createColumns(parent);
        final Table table = viewer.getTable();
        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        viewer.setContentProvider(new ArrayContentProvider());

        // Layout the viewer
        GridData gridData = new GridData(SWT.CENTER, SWT.FILL, true, true);
        gridData.horizontalSpan = 2;
        table.setLayoutData(gridData);
    }

    private void createColumns(Composite parent)
    {
        TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
        final TableColumn column = viewerColumn.getColumn();
        column.setText("Title");
        column.setWidth(100);
        column.setResizable(true);
        column.setMoveable(false);
    }

    public static void main(String[] args) {
        Display display = Display.getDefault();
        final Shell shell = new Shell(display);

        TestClass test = new TestClass(shell);

        test.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

它基本上会创建一个小Dialog,顶部有2个按钮,下方有居中 TableViewer。这应该可以让您了解如何解决问题。