前文回顾:

插件学习篇

简单的建立插件工程以及模型文件分析

利用扩展点,开发透视图

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(); TableColumn column2 = new TableColumn(table,SWT.NULL);
column2.setText("Parent");
column2.pack();
column2.setWidth();
        TableItem item = new TableItem(table,SWT.NONE);
item.setText(new String[]{“”,“”});

  那么下面还是看一个搭配使用的例子

  首先应用的是一个Tab容器,在第一页放置了一个树形控件,和一个列表控件。点击树形控件的节点,会在列表中添加相关的内容。

  源码参考如下:

 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 = ;
compsoite1.setLayout(layout);
Group treeGroup = new Group(compsoite1,SWT.NONE);
treeGroup.setText("Tree");
GridData griddata = new GridData(GridData.FILL_BOTH);
griddata.heightHint = ;
treeGroup.setLayoutData(griddata);
treeGroup.setLayout(new GridLayout(,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()[].toString(),tree.getSelection()[].getText()});
}
});
}
Group tableGroup = new Group(compsoite1,SWT.NONE);
tableGroup.setText("Table");
GridData gd = new GridData(GridData.FILL_BOTH);
gd.heightHint = ;
tableGroup.setLayoutData(gd);
tableGroup.setLayout(new GridLayout(,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(); TableColumn column2 = new TableColumn(table,SWT.NULL);
column2.setText("Parent");
column2.pack();
column2.setWidth();
} TabItem tabItem2 = new TabItem(tabFolder,SWT.NONE);
tabItem2.setText("第二页");
}

  全部源码

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 = ;
compsoite1.setLayout(layout);
Group treeGroup = new Group(compsoite1,SWT.NONE);
treeGroup.setText("Tree");
GridData griddata = new GridData(GridData.FILL_BOTH);
griddata.heightHint = ;
treeGroup.setLayoutData(griddata);
treeGroup.setLayout(new GridLayout(,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()[].toString(),tree.getSelection()[].getText()});
}
});
}
Group tableGroup = new Group(compsoite1,SWT.NONE);
tableGroup.setText("Table");
GridData gd = new GridData(GridData.FILL_BOTH);
gd.heightHint = ;
tableGroup.setLayoutData(gd);
tableGroup.setLayout(new GridLayout(,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(); TableColumn column2 = new TableColumn(table,SWT.NULL);
column2.setText("Parent");
column2.pack();
column2.setWidth();
} 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(,,,);
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
}

【插件开发】—— 6 SWT 复杂控件使用以及布局的更多相关文章

  1. iOS 开发 ZFUI framework控件,使布局更简单

    来自:http://www.jianshu.com/p/bcf86b170d9c 前言 为什么会写这个?因为在iOS开发中,界面的布局一直没有Android布局有那么多的方法和优势,我个人开发都是纯代 ...

  2. Qt基本控件及三大布局

    Qt基本控件及三大布局 来源: http://blog.csdn.net/a2604539133/article/details/73920696 Qt基本模块 一.Qt的三大布局 QHBoxLayo ...

  3. c#学习笔记之使用 TableLayoutPanel 控件设置窗体布局

    使用 TableLayoutPanel 控件设置窗体布局 在 Visual Studio IDE 左侧,找到“工具箱”选项卡. 选择“工具箱”选项卡,随即将显示工具箱.(或者,在菜单栏上,依次选择“视 ...

  4. Windows程序控件升级==>>构建布局良好的Windows程序

    01.菜单栏(MenuStrip) 01.看看这就是menuStrip的魅力: 02.除了一些常用的属性(name.text..)外还有: 03.有人会问:上图的快捷键: 方法: 方式一:1.设置菜单 ...

  5. WinForm DataGridView控件、duck布局

    1.DataGridView控件 显示数据表 (1)后台数据绑定: List<xxx> list = new List<xxx>(); dataGridView1.DataSo ...

  6. iOS Masonry控件等比例布局

    一.先解释相关API 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 /**  *  distribute with fixed spacing  *  *  ...

  7. struts中的dojo控件sx:submit布局问题

    想在一个四列的表格中插入两个按钮,希望实现下面的布局效果: 其中保存按钮为<sx:submit />控件.按照下面的代码布局: <tr><td align="c ...

  8. DevExpress的图形按钮菜单栏控件WindowsUIButtonPanel的布局、使用和设置按钮的点击事件

    场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...

  9. C#用户控件实战01_CSS布局

    很多应用系统的主页布局,一般采用如下案例所示布局较多,如下图的CSS布局框架,上.中.下,接下来我们演示,在C#中实现如下的业务架构布局. 代码范例: 在<body></body&g ...

随机推荐

  1. Spring基础入门(二)

    一.AOP 1.AOP概念 aop:面向切面编程,扩展功能不修改源代码实现. AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码. 2.AOP原理 (1)第一种情况,有接口情况,使用动态代理创建 ...

  2. DotProject首页、文档和下载 - 项目管理工具 - 开源中国社区

    DotProject首页.文档和下载 - 项目管理工具 - 开源中国社区

  3. 蓦然回首,Java 已经 24 岁了!

    01.蓦然 真没想到,Java 竟然 24 岁了(算是 90 后)! 提起 Java,印象最深刻的当然就是: class Cmower {  public static void main(Strin ...

  4. 【网络】TCP的拥塞控制

    一.拥塞控制的一般原理 拥塞:对网络中某一资源的需求超过了该资源所能提供的可用部分 拥塞控制是防止过多的数据注入到网络,这样可以使网络中的路由器或链路不致过载,拥塞控制是一个全局性的过程. 流量控制往 ...

  5. iOS设计模式 - (1)概述

    近期可自由安排的时间比較多, iOS应用方面, 没什么好点子, 就先放下, 不写了.花点时间学学设计模式. 之后将会写一系列博文, 记录设计模式学习过程. 当然, 由于我自己是搞iOS的, 所以之后设 ...

  6. jni——如何转换有符号与无符号数

    java数据结构默认均为有符号数,而通过jni转换到c/c++层,却不一定是有符号数. 如若在java中存储的即为无符号数,则在jni中可将jbyte直接进行类型转换. 若进行操作,则可在计算时,先将 ...

  7. update语句执行卡死现象原因及解决方案

    https://blog.csdn.net/wpz0713/article/details/51499654 原因分析: 可能在PLSQL Developer执行update时没有commit,ora ...

  8. 搜索学术论文訪问google的能用的几个IP地址

    google搜索引擎打不开时的解决的方法,谷歌(google)的IP是多少? google IP镜像. 这里搜集了几个经过測试可用的IP,用来在不能域名訪问google的时候进行訪问 更新一个最新的. ...

  9. oracle官方文档_查看初始化參数(举例)

    原创作品,出自 "深蓝的blog" 博客.深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/46864217 记录 ...

  10. 算法导论学习之线性时间求第k小元素+堆思想求前k大元素

    对于曾经,假设要我求第k小元素.或者是求前k大元素,我可能会将元素先排序,然后就直接求出来了,可是如今有了更好的思路. 一.线性时间内求第k小元素 这个算法又是一个基于分治思想的算法. 其详细的分治思 ...