Demonstrates ViewForm : View Form « SWT JFace Eclipse « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
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 » SWT JFace Eclipse » View FormScreenshots 
Demonstrates ViewForm
Demonstrates ViewForm


//Send questions, comments, bug reports, etc. to the authors:

//Rob Warner (rwarner@interspatial.com)
//Robert Harris (rbrt_harris@yahoo.com)

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
 * This class demonstrates ViewForm
 */
public class Look {
  // Images used in the ViewForm
  private Image lookImage;
  private Image menuImage;

  // Counter for titles of ViewForms
  private int count = 0;

  /**
   * Runs the application
   */
  public void run() {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Look");

    // Load the images
    lookImage = new Image(display, this.getClass().getResourceAsStream(
        "java2s.gif"));
    menuImage = new Image(display, this.getClass().getResourceAsStream(
        "java2s.gif"));

    createContents(shell);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }

    // We created the images, so we must dispose
    if (lookImage != nulllookImage.dispose();
    if (menuImage != nullmenuImage.dispose();
    display.dispose();
  }

  /**
   * Creates the main window's contents
   
   @param parent the main window
   */
  public void createContents(Composite parent) {
    parent.setLayout(new GridLayout(1false));

    // Pressing the "New Document" button will create a new ViewForm
    Button button = new Button(parent, SWT.PUSH);
    button.setText("New Document");

    // Create the composite that holds the ViewForms
    final Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayoutData(new GridData(GridData.FILL_BOTH));
    composite.setLayout(new FillLayout());

    // Add the event handler to create the ViewForms
    button.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        createViewFormHelper(composite, "Document " (++count));
        composite.layout();
      }
    });
  }

  /**
   * Helper function for creating the ViewForms
   
   @param parent the parent Composite
   @param text the title text
   */
  private void createViewFormHelper(final Composite parent, String text) {
    // Create the ViewForm
    final ViewForm vf = new ViewForm(parent, SWT.BORDER);

    // Create the CLabel for the top left, which will have an image and text
    CLabel label = new CLabel(vf, SWT.NONE);
    label.setText(text);
    label.setImage(lookImage);
    label.setAlignment(SWT.LEFT);
    vf.setTopLeft(label);

    // Create the downward-pointing arrow to display the menu
    // and set it as the top center
    final ToolBar tbMenu = new ToolBar(vf, SWT.FLAT);
    final ToolItem itemMenu = new ToolItem(tbMenu, SWT.PUSH);
    itemMenu.setImage(menuImage);
    vf.setTopCenter(tbMenu);

    // Create the close button and set it as the top right
    ToolBar tbClose = new ToolBar(vf, SWT.FLAT);
    ToolItem itemClose = new ToolItem(tbClose, SWT.PUSH);
    itemClose.setText("X");
    itemClose.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        vf.dispose();
        parent.layout();
      }
    });
    vf.setTopRight(tbClose);

    // Create the content--a multi-line text box
    final Text textArea = new Text(vf, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
    vf.setContent(textArea);

    /* Create the menu to display when the down arrow is pressed */
    
    
    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("");
      }
    });

    // Add the handler to display the menu
    itemMenu.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        // Place the menu right below the toolbar button
        Rectangle rect = itemMenu.getBounds();
        menu.setLocation(tbMenu.toDisplay(rect.x, rect.y + rect.height));
        menu.setVisible(true);
      }
    });
  }

  /**
   * The application entry point
   
   @param args the command line arguments
   */
  public static void main(String[] args) {
    new Look().run();
  }
}

           
       
Related examples in the same category
1. ViewForm ExampleViewForm Example
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.