如何将网格分成两部分

时间:2013-11-27 18:50:10

标签: java swt

我用表查看器构建应用程序。

我想将屏幕拆分为两部分:table,message。

消息应该是带滚动的SWT控制器(可能有很多消息)

它看起来应该是这样的

       --------------------------------
       -         toolbar of the table -
       --------------------------------
       -                              -
       -        Table                 -
       -                              -
       --------------------------------
       -        message               -
       --------------------------------

用于消息的最佳SWT控件是什么? (我需要控制消息+滚动)

我如何拆分屏幕?我需要使用SashForm吗?

    GridLayout dsGridLayout = new GridLayout();
    dsGridLayout .numColumns = 1;
    // set layout
    parent.setLayout(dsGridLayout);
    // toolBar 
    GridData tBGridData = new GridData();
    tB.horizontalAlignment = SWT.FILL;
    tB.grabExcessHorizontalSpace = true;
    // details control grid data
    GridData dGridData = new GridData();
    dGridData.horizontalAlignment = SWT.FILL;
    dGridData.grabExcessHorizontalSpace = true;
    dGridData.verticalAlignment = SWT.FILL;
    dGridData.grabExcessVerticalSpace = true;
    // Details toolBar
    ToolBar toolBar = createToolBarPart(parent); // Create toolBar for the table
    toolBar.setLayoutData(tBarGridData);
    // Separator line
    createSeperatorLabel(parent);
    // Details control
    Table table = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER ) // Create the table viewer
    table.setLayoutData(dGridData);

2 个答案:

答案 0 :(得分:2)

您可以将此代码作为起点:

private static Shell    shell;

public static void main(final String[] args)
{
    Display display = new Display();
    shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(1, false));

    createToolbar();
    createTablePart();
    createMessagesPart();

    shell.pack();
    shell.open();
    shell.setSize(500, 350);
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
    display.dispose();
}

private static void createToolbar()
{
    ToolBar toolbar = new ToolBar(shell, SWT.FLAT | SWT.BORDER | SWT.HORIZONTAL | SWT.RIGHT);
    toolbar.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

    String[] labels = new String[] { "A", "B", "C" };
    for (int i = 0; i < 3; i++)
    {
        ToolItem item = new ToolItem(toolbar, SWT.PUSH);
        item.setText(labels[i]);
    }
    toolbar.pack();
}

private static void createTablePart()
{
    Table table = new Table(shell, SWT.BORDER);
    table.setHeaderVisible(true);
    table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    for(int col = 0; col < 3; col++)
    {
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText("Col " + col);
    }

    for(int row = 0; row < 10; row++)
    {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(0, row + " " + 0);
        item.setText(1, row + " " + 1);
        item.setText(2, row + " " + 2);
    }

    for(TableColumn col : table.getColumns())
        col.pack();
}

private static void createMessagesPart()
{
    Text text = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);
    GridData data = new GridData(SWT.FILL, SWT.END, true, false);
    data.heightHint = 50;
    text.setLayoutData(data);

    text.setText("First Message\nSecond Message\nThirdMessage\nFourth Message");
}

看起来像这样:

enter image description here


或者,您可以使用SashForm来调整底部的大小:

私有静态Shell shell;

public static void main(final String[] args)
{
    Display display = new Display();
    shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(1, false));

    createToolbar();

    SashForm form = new SashForm(shell, SWT.VERTICAL);
    form.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    createTablePart(form);
    createMessagesPart(form);
    form.setWeights(new int[] { 3, 1 });

    shell.pack();
    shell.open();
    shell.setSize(500, 350);
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
    display.dispose();
}

private static void createToolbar()
{
    ToolBar toolbar = new ToolBar(shell, SWT.FLAT | SWT.BORDER | SWT.HORIZONTAL | SWT.RIGHT);
    toolbar.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

    String[] labels = new String[] { "A", "B", "C" };
    for (int i = 0; i < 3; i++)
    {
        ToolItem item = new ToolItem(toolbar, SWT.PUSH);
        item.setText(labels[i]);
    }
    toolbar.pack();
}

private static void createTablePart(SashForm parent)
{
    Table table = new Table(parent, SWT.BORDER);
    table.setHeaderVisible(true);
    table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    for (int col = 0; col < 3; col++)
    {
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText("Col " + col);
    }

    for (int row = 0; row < 10; row++)
    {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(0, row + " " + 0);
        item.setText(1, row + " " + 1);
        item.setText(2, row + " " + 2);
    }

    for (TableColumn col : table.getColumns())
        col.pack();
}

private static void createMessagesPart(SashForm parent)
{
    Text text = new Text(parent, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);
    GridData data = new GridData(SWT.FILL, SWT.END, true, false);
    data.heightHint = 50;
    text.setLayoutData(data);

    text.setText("First Message\nSecond Message\nThirdMessage\nFourth Message");
}

答案 1 :(得分:1)

您可以在视图中创建eclipse视图并添加表查看器。视图工具栏可以用作表格工具栏,也可以使视图不可关闭。对于消息,我建议使用StyledText控件,以便您可以将样式(字体大小,字体颜色,字体样式等)应用于消息。