The code is as follows:
Display display = new Display();
Shell shell = new Shell(display);
public ViewFormExample() {
shell.setLayout(new FillLayout());
final ViewForm viewForm = new ViewForm(shell, SWT.BORDER);
Label label = new Label(viewForm, SWT.NULL);
label.setText("Top center");
viewForm.setTopCenter(label);
shell.setSize(400, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new ViewFormExample();
}
}
After creating the Action in the ActionGroup in the previous step, the next step is to add a toolbar to the interface. First, you need to adjust the layout using the ViewForm class. ViewForm is also a container inherited from Composite. The original table was built on the Shell. Now we need to insert a ViewForm container on the Shell and use it as a base to create the toolbar and table in it, as shown in Figure 14.9.
Modify the open() method in the original main program as follows, leaving other codes unchanged:
shell.setLayout(new FillLayout());ViewForm viewForm = new ViewForm(shell, SWT.NONE); //Layout base ViewFormviewForm.setLayout(new FillLayout());final TableViewer tv = new TableViewer(viewForm, SW… / /The parent container is changed from shell to viewForm//...the same code as the previous section (omitted)//Create a toolbar ToolBar toolBar = new ToolBar(viewForm, SWT.FLAT); //Create a ToolBar container ToolBarManager toolBarManager = new ToolBarManager(toolBar); //Create a toolBar manager actionGroup.fillActionToolBars(toolBarManager); //Inject Action into ToolBar through toolBarManager //Set the position of the table and toolbar in the layout viewForm.setContent(tv.getControl() ); // Main body: table viewForm.setTopLeft(toolBar); // Top edge: toolbar shell.open(); |
498)this.style.width=498;" border=0> |
Figure 14.9 Layout diagram |