2013-11-27 69 views
0

我使用表格查看器构建应用程序。如何将网格拆分为两部分

我想将屏幕分为两部分:表格,消息。

的消息应与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); 
+0

为什么不在消息部分使用'GridData.widthHint'并使表部分占据剩余空间? – Baz

+0

你能告诉我一个这样的例子吗? – user1365697

+0

什么应该是SWT控制器的消息? – user1365697

回答

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"); 
} 
+0

你能举一个例子说明如何用sashForm做到这一点吗? – user1365697

+0

@ user1365697更新了我的答案。 – Baz

1

您可以创建Eclipse视图并添加view.View工具栏表里面观众可以作为表格工具栏,也可以使视图不可关闭。对于消息,我建议使用StyledText控件,以便可以将样式(字体大小,字体颜色,字体样式等)应用于消息。

+0

你能告诉我一个这样的例子吗? – user1365697