2013-11-27 72 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