`

swt 工具栏中添加下拉式的按钮[转]

SWT 
阅读更多

 

package rcp;

import org.eclipse.swt.SWT;

 

public class ToolItemDropDownMenu {

 

  public static void main(String[] args) {

    Display display = new Display();

    Shell shell = new Shell(display);

    ToolBar toolBar = new ToolBar(shell, SWT.BORDER | SWT.VERTICAL);

 

    ToolItem item = new ToolItem(toolBar, SWT.DROP_DOWN);

    item.setText("One");

 

    DropdownSelectionListener listenerOne = new DropdownSelectionListener(item);

    listenerOne.add("Option One for One");

    listenerOne.add("Option Two for One");

    listenerOne.add("Option Three for One");

    item.addSelectionListener(listenerOne);

 

    toolBar.pack();

 

    shell.pack();

    shell.open();

    while (!shell.isDisposed()) {

      if (!display.readAndDispatch())

        display.sleep();

    }

    display.dispose();

  }

}

 

转自:http://lizhensan.iteye.com/blog/1119362

class DropdownSelectionListener extends SelectionAdapter {

  private ToolItem dropdown;

 

  private Menu menu;

 

  public DropdownSelectionListener(ToolItem dropdown) {

    this.dropdown = dropdown;

    menu = new Menu(dropdown.getParent().getShell());

  }

 

  public void add(String item) {

    MenuItem menuItem = new MenuItem(menu, SWT.NONE);

    menuItem.setText(item);

    menuItem.addSelectionListener(new SelectionAdapter() {

      public void widgetSelected(SelectionEvent event) {

        MenuItem selected = (MenuItem) event.widget;

        dropdown.setText(selected.getText());

      }

    });

  }

 

  public void widgetSelected(SelectionEvent event) {

    if (event.detail == SWT.ARROW) {

      ToolItem item = (ToolItem) event.widget;

      Rectangle rect = item.getBounds();

      Point pt = item.getParent().toDisplay(new Point(rect.x, rect.y));

      menu.setLocation(pt.x, pt.y + rect.height);

      menu.setVisible(true);

    } else {

      System.out.println(dropdown.getText() + " Pressed");

    }

  }

}

分享到:
评论

相关推荐

    SWT常用组件

    - `add(String string)`:在下拉框中添加一项。 - `add(String string, int index)`:在指定索引处插入一项。 - `deselectAll()`:取消所有选项的选择。 - `removeAll()`:删除所有选项。 - `setItems(String[] ...

    SWT开发参考文档

    - **Toolbar组件**:创建工具栏。 - **Tray组件**:创建托盘图标。 - **Tree组件**:用于构建树形结构的数据展示。 - **Table组件**:用于表格数据展示。 - **TabFolder组件**:创建选项卡式界面。 - **MessageBox...

    Eclipse SWT 开发参考

    - **Toolbar组件**:用于创建工具栏。 - **Tray组件**:用于创建系统托盘图标。 - **Tree组件**:用于创建树形结构的列表。 - **Table组件**:用于创建表格形式的数据展示。 - **TabFolder组件**:用于创建带标签页...

    Eclipse_Swt_Jface_核心应用_部分19

    9.4.1 带有下拉选项的工具栏 163 9.4.2 常用的方法 163 9.5 系统托盘(Tray和TrayItem) 164 9.6 滑动组件 167 9.6.1 滑块(Slider) 167 9.6.2 刻度条(Scale) 168 9.6.3 微调按钮(Spinner) 168 ...

    Eclipse-4-RCP教程1

    19. **向表中添加上下文菜单** 上下文菜单(右键菜单)提供了针对特定对象的操作,可以根据选择的对象动态显示相关选项。 20. **添加工具栏** 工具栏提供了快捷访问常用操作的途径,学会创建和管理工具栏能增强...

    WindowsBuilder安装

    WindowsBuilder是一款功能强大的可视化GUI编程工具,它为Java开发者提供了简单直观的方式来进行Swing和SWT界面的设计与开发。通过拖拽式操作,开发者可以轻松创建复杂的用户界面,并能够直接在Eclipse集成开发环境中...

    Visual C++ 编程资源大全(源码 窗体)

    (61KB)<END><br>101,toolbar2.zip 这个程序演示了如何利用工具栏的新特性,在工具栏中的某个按钮加入下拉式的列表。(28KB)<END><br>102,avi_bar.zip 可以在状态栏里加入一个AVI动画。(34KB)<END><br>103,...

Global site tag (gtag.js) - Google Analytics