如何为SWT Table添加列(Column)菜单
来源:互联网 发布:网络教育本科升研究生 编辑:程序博客网 时间:2024/05/16 15:34
为一个Table添加菜单后,当右击该Table的某一行,即可弹出定义好的菜单,这个不难做到。今天碰到一需求:需要针对Table的某一列(Column)的单元格添加右键菜单,也即:只有在右键单击某一列的单元格时,才在被选中的单元格上显示出该右键菜单。为实现这一需求,我们需要使用org.eclipse.swt.custom包中的TableCursor类,示例代码如下:
- public static void main(String[] args) {
- Display display = new Display();
- Shell shell = new Shell(display);
- shell.setLayout(new GridLayout());
- // create a a table with 3 columns and fill with data
- final Table table = new Table(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE);
- table.setLayoutData(new GridData(GridData.FILL_BOTH));
- int columnSize = 3;
- TableColumn column;
- for (int i = 0; i < columnSize; i++) {
- column = new TableColumn(table, SWT.NONE);
- column.setWidth(100);
- column.setText("Column" + i);
- column.pack();
- }
- table.setHeaderVisible(true);
- for (int i = 0; i < 10; i++) {
- TableItem item = new TableItem(table, SWT.NONE);
- item.setText(new String[] { "cell" + i + "0", "cell" + i + "1", "cell" + i + "2" });
- }
- final TableCursor cursor = new TableCursor(table, SWT.NONE);
- cursor.setBackground(table.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION));
- cursor.setForeground(table.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
- cursor.setLayout(new FillLayout());
- final Menu menu = new Menu(table);
- MenuItem item = new MenuItem(menu, SWT.PUSH);
- item.setText("Hello Menu");
- cursor.addSelectionListener(new SelectionAdapter() {
- public void widgetSelected(SelectionEvent e) {
- int column = cursor.getColumn();
- if (column == 1) {
- table.setMenu(menu);
- } else {
- table.setMenu(null);
- }
- }
- });
- shell.open();
- while (!shell.isDisposed()) {
- if (!display.readAndDispatch())
- display.sleep();
- }
- display.dispose();
- }
运行效果如下,只有选中第二列的单元格时,才会有右键菜单弹出:
- 如何为SWT Table添加列(Column)菜单
- SWT如何给Table添加右键菜单
- SWT如何给Table添加右键菜单
- SWT学习笔记_1在如何table中动态添加
- 如何为对话框添加菜单
- swt table中添加入力框
- SWT TABLE 列宽度只能设为定长吗?能不能设为占整个table百分比的形式?
- user.table.column, table.column 或列说明无效
- user.table.column, table.column 或列说明无效
- DevExpress XtraGrid 添加列Column
- 动态添加table列
- 如何为treewidgetitem添加右键菜单
- table.column 或列说明无效
- table.column 或列说明无效
- el-table-column动态加载列
- 在db2数据库中,如何使用命令改变列(alter table, alter column)长度
- 【技巧】SQL中如何修改表格(table)中列(column)的默认值(default);
- SWT学习笔记_2在如何在下啦菜单中添加值
- 写给程序员
- Spring整合Hibernate的步骤
- Strassen矩阵连乘
- 程序委托
- 使用动态规划写出的矩阵连乘
- 如何为SWT Table添加列(Column)菜单
- 让程序支持脚本
- 用脚本语言扩展程序功能
- log4cxx配合boost::format
- (12)TranslateAccelerator函数
- 有点累了
- 看了五分之一的《疯狂的程序员》有感。
- Interface Design & UAR
- 使用客户端MercurialEclipse1.1.867进行源码管理的问题