【插件开发】—— 6 SWT 复杂控件使用以及布局
时间:2022-04-22
本文章向大家介绍【插件开发】—— 6 SWT 复杂控件使用以及布局,主要内容包括Tab页、树形结构、表格、那么下面还是看一个搭配使用的例子、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。
前文回顾: 1 插件学习篇 2 简单的建立插件工程以及模型文件分析 3 利用扩展点,开发透视图 4 SWT编程须知 5 SWT简单控件的使用与布局搭配
前几篇讲到了简单控件的使用,复杂控件使用原则上与简单控件差不多,不过数据的使用还有一些布局还有些额外的技巧。
成果展示:
这里介绍下Tab页,列表,以及树的使用。
Tab页
这个tab页仍然采用SWT控件的一贯作风,子页都以挂载的方式连接到Tab容器上,但是需要使用一个组个对象才能在里面放置内容,并不支持直接进行布局。
TabFolder tabFolder = new TabFolder(shell,SWT.BORDER);
TabItem tabItem1 = new TabItem(tabFolder,SWT.NONE);
tabItem1.setText("第一页");
Composite compsoite1 = new Composite(tabFolder,SWT.NONE);
tabItem1.setControl(compsoite1);
这样再在Composite容器内放置其他的控件。
树形结构
而列表以及树的使用基本上差不多,树稍微复杂一点,有一个父亲孩子的概念,多使用几次就了解其中的关系技巧了。
tree = new Tree(treeGroup,SWT.SINGLE);
tree.setLayoutData(new GridData(GridData.FILL_BOTH));
TreeItem stu1 = new TreeItem(tree,SWT.NONE);
stu1.setText("xingoo");
{
TreeItem info1 = new TreeItem(stu1,SWT.NONE);
info1.setText("age:25");
TreeItem info2 = new TreeItem(stu1,SWT.NONE);
info2.setText("tel:12345");
}
TreeItem stu2 = new TreeItem(tree,SWT.NONE);
stu2.setText("halo");
{
TreeItem info3 = new TreeItem(stu2,SWT.NONE);
info3.setText("age:25");
TreeItem info4 = new TreeItem(stu2,SWT.NONE);
info4.setText("tel:67890");
}
表格
比较常用的一般就是列表,一般导向页,对话框也都是使用Table来制作。
table = new Table(tableGroup,SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
table.setHeaderVisible(true);//设置表头可见
table.setLinesVisible(true);//设置线条可见
table.setLayoutData(new GridData(GridData.FILL_BOTH));
TableColumn column1 = new TableColumn(table,SWT.NULL);
column1.setText("Tree Item");
column1.pack();
column1.setWidth(150);
TableColumn column2 = new TableColumn(table,SWT.NULL);
column2.setText("Parent");
column2.pack();
column2.setWidth(150);
TableItem item = new TableItem(table,SWT.NONE);
item.setText(new String[]{“123”,“445”});
那么下面还是看一个搭配使用的例子
首先应用的是一个Tab容器,在第一页放置了一个树形控件,和一个列表控件。点击树形控件的节点,会在列表中添加相关的内容。
源码参考如下:
1 public void todo(Shell shell) {
2 TabFolder tabFolder = new TabFolder(shell,SWT.BORDER);
3
4 TabItem tabItem1 = new TabItem(tabFolder,SWT.NONE);
5 tabItem1.setText("第一页");
6
7 Composite compsoite1 = new Composite(tabFolder,SWT.NONE);
8 tabItem1.setControl(compsoite1);
9
10 GridLayout layout = new GridLayout();
11 layout.numColumns = 1;
12 compsoite1.setLayout(layout);
13 Group treeGroup = new Group(compsoite1,SWT.NONE);
14 treeGroup.setText("Tree");
15 GridData griddata = new GridData(GridData.FILL_BOTH);
16 griddata.heightHint = 50;
17 treeGroup.setLayoutData(griddata);
18 treeGroup.setLayout(new GridLayout(1,false));
19 {
20 tree = new Tree(treeGroup,SWT.SINGLE);
21 tree.setLayoutData(new GridData(GridData.FILL_BOTH));
22
23 TreeItem stu1 = new TreeItem(tree,SWT.NONE);
24 stu1.setText("xingoo");
25 {
26 TreeItem info1 = new TreeItem(stu1,SWT.NONE);
27 info1.setText("age:25");
28
29 TreeItem info2 = new TreeItem(stu1,SWT.NONE);
30 info2.setText("tel:12345");
31 }
32 TreeItem stu2 = new TreeItem(tree,SWT.NONE);
33 stu2.setText("halo");
34 {
35 TreeItem info3 = new TreeItem(stu2,SWT.NONE);
36 info3.setText("age:25");
37
38 TreeItem info4 = new TreeItem(stu2,SWT.NONE);
39 info4.setText("tel:67890");
40 }
41
42 tree.addSelectionListener(new SelectionAdapter() {
43 public void widgetSelected(SelectionEvent evt){
44 TableItem item = new TableItem(table,SWT.NONE);
45 item.setText(new String[]{tree.getSelection()[0].toString(),tree.getSelection()[0].getText()});
46 }
47 });
48 }
49 Group tableGroup = new Group(compsoite1,SWT.NONE);
50 tableGroup.setText("Table");
51 GridData gd = new GridData(GridData.FILL_BOTH);
52 gd.heightHint = 20;
53 tableGroup.setLayoutData(gd);
54 tableGroup.setLayout(new GridLayout(1,false));
55 { //创建一个单选的,有边界的,一行全选的表格
56 table = new Table(tableGroup,SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
57 table.setHeaderVisible(true);//设置表头可见
58 table.setLinesVisible(true);//设置线条可见
59 table.setLayoutData(new GridData(GridData.FILL_BOTH));
60
61 TableColumn column1 = new TableColumn(table,SWT.NULL);
62 column1.setText("Tree Item");
63 column1.pack();
64 column1.setWidth(150);
65
66 TableColumn column2 = new TableColumn(table,SWT.NULL);
67 column2.setText("Parent");
68 column2.pack();
69 column2.setWidth(150);
70 }
71
72
73 TabItem tabItem2 = new TabItem(tabFolder,SWT.NONE);
74 tabItem2.setText("第二页");
75 }
全部源码
package com.xingoo.plugin.swttest.test;
import javax.swing.text.StyleConstants.ColorConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import com.xingoo.plugin.swttest.Abstract.AbstractExample;
public class Test1 extends AbstractExample{
private Table table;
private Tree tree;
public static void main(String[] args) {
new Test1().run();
}
public void todo(Shell shell) {
TabFolder tabFolder = new TabFolder(shell,SWT.BORDER);
TabItem tabItem1 = new TabItem(tabFolder,SWT.NONE);
tabItem1.setText("第一页");
Composite compsoite1 = new Composite(tabFolder,SWT.NONE);
tabItem1.setControl(compsoite1);
GridLayout layout = new GridLayout();
layout.numColumns = 1;
compsoite1.setLayout(layout);
Group treeGroup = new Group(compsoite1,SWT.NONE);
treeGroup.setText("Tree");
GridData griddata = new GridData(GridData.FILL_BOTH);
griddata.heightHint = 50;
treeGroup.setLayoutData(griddata);
treeGroup.setLayout(new GridLayout(1,false));
{
tree = new Tree(treeGroup,SWT.SINGLE);
tree.setLayoutData(new GridData(GridData.FILL_BOTH));
TreeItem stu1 = new TreeItem(tree,SWT.NONE);
stu1.setText("xingoo");
{
TreeItem info1 = new TreeItem(stu1,SWT.NONE);
info1.setText("age:25");
TreeItem info2 = new TreeItem(stu1,SWT.NONE);
info2.setText("tel:12345");
}
TreeItem stu2 = new TreeItem(tree,SWT.NONE);
stu2.setText("halo");
{
TreeItem info3 = new TreeItem(stu2,SWT.NONE);
info3.setText("age:25");
TreeItem info4 = new TreeItem(stu2,SWT.NONE);
info4.setText("tel:67890");
}
tree.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent evt){
TableItem item = new TableItem(table,SWT.NONE);
item.setText(new String[]{tree.getSelection()[0].toString(),tree.getSelection()[0].getText()});
}
});
}
Group tableGroup = new Group(compsoite1,SWT.NONE);
tableGroup.setText("Table");
GridData gd = new GridData(GridData.FILL_BOTH);
gd.heightHint = 20;
tableGroup.setLayoutData(gd);
tableGroup.setLayout(new GridLayout(1,false));
{ //创建一个单选的,有边界的,一行全选的表格
table = new Table(tableGroup,SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
table.setHeaderVisible(true);//设置表头可见
table.setLinesVisible(true);//设置线条可见
table.setLayoutData(new GridData(GridData.FILL_BOTH));
TableColumn column1 = new TableColumn(table,SWT.NULL);
column1.setText("Tree Item");
column1.pack();
column1.setWidth(150);
TableColumn column2 = new TableColumn(table,SWT.NULL);
column2.setText("Parent");
column2.pack();
column2.setWidth(150);
}
TabItem tabItem2 = new TabItem(tabFolder,SWT.NONE);
tabItem2.setText("第二页");
}
}
引用的抽象类
package com.xingoo.plugin.swttest.Abstract;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public abstract class AbstractExample{
public void run(){
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("shell example");
shell.setBounds(200,200,400,400);
shell.setLayout(new FillLayout());
todo(shell);
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
//dispose the resource
display.beep();
display.dispose();
}
public abstract void todo(Shell shell);//extension something here
}
- Shiro中的授权问题
- Spring Cloud中服务的发现与消费
- 使用Spring Cloud搭建高可用服务注册中心
- 从Netflix的Hystrix框架理解服务熔断和服务降级
- 使用Spring Cloud搭建服务注册中心
- 技术分享 | kafka的使用场景以及生态系统
- WebSocket刨根问底(二)
- WebSocket刨根问底(三)之群聊
- SDNLAB群分享(四):利用ODL下发流表创建VxLAN网络
- 一个简单的案例带你入门Dubbo分布式框架
- Ajax上传图片以及上传之前先预览
- Spring Cloud中Hystrix的服务降级与异常处理
- Open vSwitch源码解析之基于VxLAN实现NSH解析功能
- Spring Cloud自定义Hystrix请求命令
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Linux系统下Nginx支持ipv6配置的办法
- Smarty模板变量与调节器实例详解
- Linux中搭建完整的samba服务器全攻略(centos版)
- Thinkphp5 自定义上传文件名的实现方法
- PHP读取文件,解决中文乱码UTF-8的方法分析
- Linux低电量自动关机的实现办法
- php设计模式之工厂方法模式分析【星际争霸游戏案例】
- PHP+Apache实现二级域名之间共享cookie的方法
- linux虚拟机配置静态IP地址的完整步骤
- laravel-admin利用ModelTree实现对分类信息的管理
- Linux系统下快速配置HugePages的完整步骤
- Laravel5.1 框架关联模型之后操作实例分析
- PHP实现的多进程控制demo示例
- 你可能不知道的一些linux文件权限管理办法
- Laravel框架路由与MVC实例详解