Shared Menu in SWT : Menu « SWT JFace Eclipse « Java

Home
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.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » SWT JFace Eclipse » Menu 




Shared Menu in SWT
Shared Menu in SWT

/******************************************************************************
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * All right reserved. 
 
 * Created on Jan 5, 2004 2:50:44 PM by JACK
 * $Id$
 
 * visit: http://www.asprise.com/swt
 *****************************************************************************/

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
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;

public class SharedMenu {
  Display display = new Display();
  Shell shell = new Shell(display);

  public SharedMenu() {

    menu();

    shell.pack();
    shell.open();
    
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }

    display.dispose();
  }
  
  private void menu() {
    shell.setLayout(new GridLayout(2true));
    
    final Composite composite1 = new Composite(shell, SWT.BORDER);
    composite1.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
    
    final Composite composite2 = new Composite(shell, SWT.BORDER);
    composite2.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
    
    Menu menu = new Menu(composite1);
    MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
    menuItem.setText("Popup menu");
    
    menuItem.addDisposeListener(new DisposeListener() {
      public void widgetDisposed(DisposeEvent e) {
        System.out.println("Menu item is disposed.");
      }
    });

    menuItem.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        System.out.println("Dispsoing ...");
        //composite2.setMenu(null);
        composite2.dispose();
      }
    });
    
    composite1.setMenu(menu);
    composite2.setMenu(menu);
  }

/*  
  private void complex() {
    shell.setLayout(new GridLayout(2, true));
    
    Label label = new Label(shell, SWT.NULL);
    label.setText("Label");
    
    Composite composite = new Composite(shell, SWT.NULL);
    composite.setLayout(new GridLayout());
    
    Button button1 = new Button(composite, SWT.PUSH);
    button1.setText("Button 1");
    
    Button button2 = new Button(composite, SWT.PUSH);
    button2.setText("Button 2");
    
  }
  
  private void addButton() {
    shell.setLayout(new GridLayout());
    
    Label label = new Label(shell, SWT.NULL);
    
    final Color color = new Color(display, 255, 0, 0);
    
    label.setForeground(color);
    label.setForeground(display.getSystemColor(SWT.COLOR_RED));
    
    label.setText("Testing label");
    
    label.addDisposeListener(new DisposeListener() {
      public void widgetDisposed(DisposeEvent e) {
        color.dispose();
      }
    });
  }

  private void draw() {
    GC gc = new GC(shell);
    gc.setLineWidth(3);
    
    //Color color = new Color(display, 255, 0, 0);
    
    //gc.setForeground(color);
    gc.drawRectangle(10, 10, 200, 100);
    
    gc.dispose();
  }
*/

  public static void main(String[] args) {
    new SharedMenu();
  }
  
}



           
       














Related examples in the same category
1.SWT Menu and menu eventSWT Menu and menu event
2.Menu Examples Menu Examples
3.Demonstrates menusDemonstrates menus
4.Menu Shell
5.Menu Shell 2Menu Shell 2
6.Menu Shell 3Menu Shell 3
7.Menu Shell 4Menu Shell 4
8.Menu Shell 6Menu Shell 6
9.Menu Shell 5Menu Shell 5
10.SWT Menu ExampleSWT Menu Example
11.Show a popup menu (wait for it to close)Show a popup menu (wait for it to close)
12.Fill a menu dynamically (when menu shown)Fill a menu dynamically (when menu shown)
13.Enable menu items dynamically (when menu shown)Enable menu items dynamically (when menu shown)
14.Create a menu with radio itemsCreate a menu with radio items
15.Create a popup menu (set in multiple controls)Create a popup menu (set in multiple controls)
16.Create a bar and pull down menu (accelerators, mnemonics)Create a bar and pull down menu (accelerators, mnemonics)
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.