Creating a ViewForm : ViewForm « SWT « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » SWT » ViewForm 
17. 87. 1. Creating a ViewForm
  1. ViewForm creates three controls in a row across the top of a Composite, with a content area below the controls.
  2. The first control contains the image in the upper left and the text Outline.
  3. The second control contains the toolbar buttons to the right of the text Outline, up to, but not including, the close button in the upper right.
  4. The third control is the close button in the upper right.
  5. Everything else is the content area.

You create a ViewForm by calling its only constructor:

ViewForm(Composite parent, int style)

The applicable styles for ViewForm are

  1. SWT.BORDER, which draws a visible border and a drop shadow around the ViewForm, and
  2. SWT.FLAT, which eliminates the drop shadow.
  3. SWT.FLAT must be combined with SWT.BORDER (using the bitwise OR operator) to have any effect.
Creating a ViewForm
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class ViewFormCreate {
  private static int count = 0;

  static Display display = new Display();

  private static void createViewFormHelper(final Composite parent, String text) {
    final ViewForm vf = new ViewForm(parent, SWT.BORDER);

    CLabel label = new CLabel(vf, SWT.NONE);
    label.setText(text);
    label.setImage(new Image(display, "yourFile.gif"));
    label.setAlignment(SWT.LEFT);
    vf.setTopLeft(label);

    final ToolBar tbMenu = new ToolBar(vf, SWT.FLAT);
    final ToolItem itemMenu = new ToolItem(tbMenu, SWT.PUSH);
    vf.setTopCenter(tbMenu);

    ToolBar tbClose = new ToolBar(vf, SWT.FLAT);
    ToolItem itemClose = new ToolItem(tbClose, SWT.PUSH);
    itemClose.setImage(new Image(display, "yourFile.gif"));
    itemClose.setText("X");
    itemClose.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        vf.dispose();
        parent.layout();
      }
    });
    vf.setTopRight(tbClose);

    final Text textArea = new Text(vf, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
    vf.setContent(textArea);

    final Menu menu = new Menu(tbMenu);
    MenuItem clear = new MenuItem(menu, SWT.NONE);
    clear.setText("Clear");
    clear.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        textArea.setText("");
      }
    });

    itemMenu.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        Rectangle rect = itemMenu.getBounds();
        menu.setLocation(tbMenu.toDisplay(rect.x, rect.y + rect.height));
        menu.setVisible(true);
      }
    });
  }

  public static void main(String[] args) {

    Shell shell = new Shell(display);
    shell.setText("Look");

    shell.setLayout(new GridLayout(1false));

    Button button = new Button(shell, SWT.PUSH);
    button.setText("New Document");

    final Composite composite = new Composite(shell, SWT.NONE);
    composite.setLayoutData(new GridData(GridData.FILL_BOTH));
    composite.setLayout(new FillLayout());

    button.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        createViewFormHelper(composite, "Document " (++count));
        composite.layout();
      }
    });

    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}
17. 87. ViewForm
17. 87. 1. Creating a ViewFormCreating a ViewForm
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.