ViewForm 是 SWT Custom 套件中特殊的容器,專為「頂部工具列/標籤 + 內容區」的經典介面模式設計。它繼承自 Composite,內建四個錨點區域:TopLeft、TopCenter、TopRight、Content。本文展示基本用法與結合 JFace TableViewer、ToolBarManager 的實戰整合模式。
| 方法 | 說明 |
|---|---|
| setTopLeft(Control) | 左上角區域(通常放工具列、標籤) |
| setTopCenter(Control) | 中上區域(標題、搜尋框) |
| setTopRight(Control) | 右上區域(動作按鈕、狀態) |
| setContent(Control) | 主內容區(表格、樹、編輯器) |
| getTopLeft()/getTopCenter()/getTopRight()/getContent() | 獲取對應控件 |
public class ViewFormExample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
ViewForm viewForm = new ViewForm(shell, SWT.BORDER);
Label topLabel = new Label(viewForm, SWT.NONE);
topLabel.setText("Top Center Title");
viewForm.setTopCenter(topLabel);
shell.setSize(400, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}
場景:表格上方顯示工具列,工具列由 JFace ActionGroup 管理動作。
// 原主方法中的 open() 修改如下:
public void open() {
// ... 原有初始化代碼 ...
// 1. Shell 使用 FillLayout 作為基座
shell.setLayout(new FillLayout());
// 2. 建立 ViewForm 作為佈局容器
ViewForm viewForm = new ViewForm(shell, SWT.NONE);
viewForm.setLayout(new FillLayout()); // Content 區填滿
// 3. 建立 TableViewer,父容器設為 viewForm
TableViewer tv = new TableViewer(viewForm, SWT.BORDER | SWT.FULL_SELECTION);
// ... 配置 Column、LabelProvider、ContentProvider、Input ...
// 4. 建立工具列,父容器同為 viewForm
ToolBar toolBar = new ToolBar(viewForm, SWT.FLAT);
ToolBarManager toolBarManager = new ToolBarManager(toolBar);
// 5. 將 ActionGroup 的動作注入工具列
actionGroup.fillActionToolBars(toolBarManager);
// 6. 關鍵:設置 ViewForm 佈局位置
viewForm.setContent(tv.getControl()); // 主體:表格
viewForm.setTopLeft(toolBar); // 頂端左側:工具列
// 7. 開啟視窗
shell.open();
// ... 事件循環 ...
}
+-------------------------------------------------------+ | ViewForm (Shell) | | +-------------------+-------------------+-------------+ | | | ToolBar (TopLeft) | TopCenter (empty) | TopRight | | | | [New] [Edit] [Del]| | (empty) | | | +-------------------+-------------------+-------------+ | | | | | | | TableViewer | | | | (Content) | | | | | | | +---------------------------------------------------+ | +-------------------------------------------------------+
// 左側:工具列
viewForm.setTopLeft(toolBar);
// 中間:搜尋框
Text search = new Text(viewForm, SWT.SEARCH | SWT.BORDER);
search.setMessage("Search...");
viewForm.setTopCenter(search);
// 右側:篩選下拉
Combo filter = new Combo(viewForm, SWT.READ_ONLY);
filter.setItems("All", "Active", "Archived");
filter.select(0);
viewForm.setTopRight(filter);
// 切換到不同 Viewer
viewForm.setContent(newTableViewer.getControl());
viewForm.layout(true); // 重新佈局
ScrolledComposite sc = new ScrolledComposite(viewForm, SWT.H_SCROLL | SWT.V_SCROLL);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
Composite content = new Composite(sc, SWT.NONE);
content.setLayout(new GridLayout());
// ... 添加大量控件 ...
sc.setContent(content);
sc.setMinSize(content.computeSize(SWT.DEFAULT, SWT.DEFAULT));
viewForm.setContent(sc);
| 維度 | ViewForm | GridLayout 手動 | FormLayout 手動 |
|---|---|---|---|
| 代碼量 | 極少(4 行設定) | 中等(需 GridData span/grab) | 較多(需 FormAttachment 鏈) |
| 語義清晰度 | 高(TopLeft/Content 直觀) | 低(需閱讀 GridData) | 中(附著關係隱性) |
| 彈性 | 固定四區域 | 高(任意網格) | 最高(像素級控制) |
| 適用場景 | 標準「工具列+內容」介面 | 複雜表單、多區域 | 精確對話框、重疊介面 |
viewForm.setTopLeft(toolBar) 在 shell.open() 前呼叫,且 toolBar 父容器是 viewForm。FillLayout),或父容器給足空間。computeSize。ViewForm 是「工具列 + 內容區」標準介面的最佳實踐容器:
setXxx() 完成專業介面結構。ToolBarManager + ActionGroup 直接注入。當你的介面符合「頂部操作區 + 底部內容區」模式時,優先選擇 ViewForm。