SWT_之Table篇
package edu.ch4;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class TableShell {
Display d;
Shell s;
public TableShell() {
d = new Display();
s = new Shell(d);
s.setSize(350, 200);
s.setText("Table Shell");
GridLayout g1 = new GridLayout();
g1.numColumns = 4;
s.setLayout(g1);
// create a table
final Table t = new Table(s, SWT.BORDER | SWT.FULL_SELECTION
| SWT.CHECK | SWT.MULTI);
// create table header
final GridData gd = new GridData(GridData.FILL_BOTH);
gd.horizontalSpan = 4;
t.setLayoutData(gd);
TableColumn tc1 = new TableColumn(t, SWT.LEFT);
TableColumn tc2 = new TableColumn(t, SWT.CENTER);
TableColumn tc3 = new TableColumn(t, SWT.CENTER);
tc1.setText("First Name");
tc2.setText("Last Name");
tc3.setText("Address");
tc1.setWidth(75);
tc2.setWidth(70);
tc3.setWidth(80);
t.setHeaderVisible(true);
t.setLinesVisible(true);
// create table cell elements
final TableItem item1 = new TableItem(t, SWT.NONE);
item1.setText(new String[] { "Tim", "Hatton", "Kentucky" });
TableItem item2 = new TableItem(t, SWT.NONE);
item2.setText(new String[] { "Caitlyn", "Warner", "Ohio" });
TableItem item3 = new TableItem(t, SWT.NONE);
item3.setText(new String[] { "Reese", "Miller", "Ohio" });
final Text find = new Text(s, SWT.SINGLE | SWT.BORDER);
final Text replace = new Text(s, SWT.SINGLE | SWT.BORDER);
final Button b = new Button(s, SWT.PUSH | SWT.BORDER);
b.setText("replace");
b.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
TableItem[] tia = t.getItems();
for (int i = 0; i < tia.length; i++) {
// 搜索并替换第三列的数值
if (tia[i].getChecked() && tia[i].getText(2).equals(find.getText()))
System.out.println(tia[i].getText(2)); //取表格一行中的某列的值
System.out.println(tia[i].getChecked()); //被选中的
tia[i].setText(2, replace.getText());
}
}
});
s.open();
while (!s.isDisposed())
if (!d.readAndDispatch())
d.sleep();
d.dispose();
}
public static void main(String args[]) {
new TableShell();
}
}
SWT Table(Table是无法编辑的,若要创建出可编辑的table,需要自行使用Composite与GirdLayout来开发)
1.创建简单的Table
创建Table就是创建出Table类的对象,传入parent容器与样式属性。对该对象加入TableColumn这个类的对象来表示想要出现在table中的字段。
Table t=new Table(s,SWT.BORDER);
创建table字段主要有三个步骤:
1)对每个字段创建出TableColumn的对象。
2)使用SetText()对每个字段设置表头文字。
3)使用SetWidth()来设定每个字段的宽度。
TableColumn tc1=new TableColumn(t,SWT.CENTER);
TableColumn tc2=new TableColumn(t,SWT.CENTER);
TableColumn tc3=new TableColumn(t,SWT.CENTER);
tc1.setText("First Name");
tc2.setText("Last Name");
tc3.setText("Address");
tc1.setWidth(75);
tc2.setWidth(70);
tc3.setWidth(80);
t.setHeaderVisible(true);
TableColumn支持三种样式:SWT.CENTER、SWT.RIGHT、SWT.LEFT,这些样式决定了文字在每个字段中显示方式。
创建完Table的表头后,需要通过TableItem类来加入数据到Table中。
TableItem item1=new TableItem(t,SWT.NONE);
item1.setText(new String[]{"Tim","Hatton","Kentucky"});
TableItem item2=new TableItem(t,SWT.NONE);
item2.setText(new String[]{"Caitlyn","Warner","Ohio"});
TableItem item3=new TableItem(t,SWT.NONE);
item3.setText(new String[]{"Reese","Miller","Ohio"});
每个TableItem对象代表了table中的一行且行中的每一栏都有一个值。数据是通过String数组加入TableItem中的。
2.字段表头与网格线的显示
字段表头的显示: t.setHeaderVisible(true);
网格线的显示: t.setLinesVisible(true);
3.强调选取行
在行中的某一栏被选取时,通过将跨字段的整行强调来给用户以整行被选取的视觉指示,可以使用SWT.FULL_SELECTION这个样式即可。
4.允许选取多行
Table默认情况下只被允许选取一行。可以设定SWT.MULTI样式来允许选取多行。在选取时必须通过按住ctrl键来进行多行选取。
5.程序化得选取项目
若table的项目是可以被选取的,那就一定有方法来判断哪个项目被选取的,或者可以在没有用户介入的情况下让某个项目被选取。
Table通过getSelection()这个方法返回当前被选取的TableItem(一行数据)对象的数组。
TableItem []ti=t.getSelection();
若Table是多重选取的,可以通过getSelectionIndices()返回int值的数组来以零基准索引指出哪些TableItem被选取。
int[] selected=t.getSelectionIndices();
若Table为单选的,可以通过getSelectionIndex()返回当前被选取项目的索引。
int selected=t.getSelectionIndex();
要想将Table中的整行以新值取代旧值,可以再setText()中传入String对象的数组。数组中的每个元素会被用来替代调用setText()的TableItem中的单一cell:
String [] newText={"Nikki","Miller","Asia"};
item2.setText(newText);
6.Check样式
可以通过SWT.CHECK样式在table项目前加上一个checkbox,可以通过setChecked()方法来让项目成为checked,通过getChecked()方法判断项目的状态。
item3.setChecked(true);
boolean checked=item3.getChecked();
7.改变背景颜色
通过使用setBackground()来指定个别TableItem的背景颜色:item2.setBackground(new Color(d,127,178,127));
8.创建可以搜素和替换的Table
虽然Table这个类内置了通过键入cell中的文字的第一个字符来寻找项目的功能,但这还不足以提供完整的Table搜索能力。还可以通过getText()和setText()对TableItem中的文字进行交互,完成寻找与替换功能。
SWT_之Table篇的更多相关文章
- vue+elementUI table篇
1.table内容展示 <el-table stripe :key='tableKey' header-cell-class-name="bindonce" :data=&q ...
- BootstrapBlazor 组件库使用体验---Table篇
原文地址:https://www.cnblogs.com/ysmc/p/13323242.html Blazor 是一个使用 .NET 生成交互式客户端 Web UI 的框架: 使用 C# 代替 Ja ...
- R语言中文社区历史文章整理(类型篇)
R语言中文社区历史文章整理(类型篇) R包: R语言交互式绘制杭州市地图:leafletCN包简介 clickpaste包介绍 igraph包快速上手 jiebaR,从入门到喜欢 Catterpl ...
- TGL站长关于常见问题的回复
问题地址: http://www.thegrouplet.com/thread-112923-1-1.html 问题: 网站配有太多的模板是否影响网站加载速度 月光答复: wp不需要删除其他的模板,不 ...
- JS组件系列——表格组件神器:bootstrap table(三:终结篇,最后的干货福利)
前言:前面介绍了两篇关于bootstrap table的基础用法,这章我们继续来看看它比较常用的一些功能,来个终结篇吧,毛爷爷告诉我们做事要有始有终~~bootstrap table这东西要想所有功能 ...
- iOS进阶篇索引,标记和自定义的table
一.带索引目录的表视图 ①效果图 图1 带索引的列表 ② 数据源 本想获取通讯录中得名字,但为了用模拟器调试方便,就写死了数据,所以也只写了部分字母,总之有那么点意思就成 @interface Vie ...
- Salesforce 大数据量处理篇(一)Skinny Table
本篇参考:https://developer.salesforce.com/docs/atlas.en-us.salesforce_large_data_volumes_bp.meta/salesfo ...
- (二)Superset 1.3图表篇——Time-series Table
(二)Superset 1.3图表篇--Time-series Table 本系列文章基于Superset 1.3.0版本.1.3.0版本目前支持分布,趋势,地理等等类型共59张图表.本次1.3版本的 ...
- 用python解析word文件(段落篇(paragraph) 表格篇(table) 样式篇(style))
首先需要安装相应的支持库: 直接在命令行执行pip install python-docx 示例代码如下: import docxfrom docx import Document #导入库 path ...
随机推荐
- composer随笔
composer dump-autoload 生成autoload.php文件
- December 16th 2016 Week 51st Friday
My life is a straight line, turning only for you. 我的人生是一条直线,转弯只是为了你. My life is a straight line that ...
- ZT Android4.2关于bluetooth在HAL层的分析(1)
我的电子杂烩饭 http://blog.sina.com.cn/wuchuchu2012 [订阅][手机订阅] 首页 博文目录 图片 关于我 正文 字体大小:大 中 小 Android4.2关于blu ...
- ZT Linux系统环境下的Socket编程详细解析
Linux系统环境下的Socket编程详细解析 来自: http://blog.163.com/jiangh_1982/blog/static/121950520082881457775/ 什么是So ...
- javascript对象和函数的几种常见写法
/** * Created by chet on 15/12/17. */ var En= function (button,func) { //dosth,不能return alert(button ...
- IPUtils 工具类
package com.hxqc.basic.dependency.util; import org.apache.commons.lang.StringUtils; import javax.ser ...
- WebUploader 图片上传控件使用范例
官网 http://fex.baidu.com/webuploader/getting-started.html 其实官网写的挺详细的,看官网也可以了. 引入资源 使用Web Uploader文件上 ...
- vim在插入模式粘贴代码缩进问题解决方法
转载自:https://blog.csdn.net/commshare/article/details/6215088 在vim粘贴代码会出现缩进问题,原因在于vim在代码粘贴时会自动缩进 解决方法: ...
- web-ctf随机数安全
rand() 函数在产生随机数的时候没有调用 srand(),则产生的随机数是有规律可询的. 产生的随机数可以用下面这个公式预测 : state[i] = state[i-3] + state[i-3 ...
- 【Git】本地与GitHub同步
按步骤一步一步来,成功啦~ 以管理员身份运行Git-bash 要求输入用户名,密码 成功推入github~~加油加油 补充: 将仓库中的改动同步到本地 在git-bash中进入项目目录下,使用git ...