在前面的文章中我们了解SWT的一些基本控件,现在我为大家介绍几种同样常用但稍微复杂点的控件
1.tree:
Styles:
SINGLE(不能多选), MULTI(可以多选), CHECK(每个treeitem有复选框), FULL_SELECTION(选中时,选中整行), VIRTUAL(树是个虚拟的树,在得到用户、客户要求或者请求的时候才建立树的Item,而这些Item要设置有多少个子Item。有助于提升性能), NO_SCROLL(没有滚动条,默认是自动的)
Events:
Selection(先择事件), DefaultSelection(默认选择事件), Collapse(收紧子节点), Expand(展开子节点), SetData(铺展时间,经常与tree.setItemCount(x);连用), MeasureItem( 计算Item应该画多大,就是计算好了画多大后,在加上Image的宽度,这里要注意的是改变event.width的宽度就改变的项的宽度了。), EraseItem(设置背景), PaintItem( 画上设置的图像),MouseDown(这个不说了- -!)
TreeColumn:
代表tree的一列
TreeItem:
代表tree的一行
cell:
确定某行某列的一个小单元
static Image getImage(Color c) {
Image image = new Image(Display.getCurrent(), 17, 17);
GC gc = new GC(image);
gc.setBackground(c);
gc.fillRectangle(3, 3, 10, 10);
gc.drawRectangle(3, 3,9,9);
gc.dispose();
return image;
}
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
final Image image=getImage(display.getSystemColor(SWT.COLOR_BLUE));
TreeColumnLayout layout=new TreeColumnLayout();
shell.setLayout (layout);
final Tree tree = new Tree(shell, SWT.VIRTUAL | SWT.BORDER|SWT.FULL_SELECTION);
TreeColumn nameColumn=new TreeColumn(tree,SWT.NONE);
nameColumn.setText("Name");
layout.setColumnData(nameColumn,new ColumnWeightData(50, 150));
TreeColumn valueColumn=new TreeColumn(tree,SWT.NONE);
valueColumn.setText("Value");
layout.setColumnData(valueColumn,new ColumnWeightData(50, 150));
final TreeEditor editor= new TreeEditor(tree);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
// 生成节点的总是
tree.addMouseListener(new MouseListener() {
@Override
public void mouseUp(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseDown(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseDoubleClick(MouseEvent e) {
// TODO Auto-generated method stub
Control c = editor.getEditor();
if (c != null) {
c.dispose();
}
Point point = new Point(e.x, e.y);
final TreeItem treeItem = tree.getItem(point);
if(treeItem!=null){
int column = -1;
for (int i = 0; i < tree.getColumnCount(); i++) {
Rectangle rec = treeItem.getBounds(i);
if (rec.contains(point))
column = i;
}
final Text txt=new Text(tree,SWT.NONE);
txt.setText(treeItem.getText(column));
txt.forceFocus();
editor.setEditor(txt, treeItem,column);
System.out.println(txt.getBounds());
txt.addListener(SWT.FocusOut, new Listener() {
@Override
public void handleEvent(Event event) {
// TODO Auto-generated method stub
treeItem.setText(txt.getText());
txt.dispose();
}
});
}
}
});
final int[] iArr = new int[1];
tree.addListener(SWT.MeasureItem,new Listener(){
@Override
public void handleEvent(Event event) {
// TODO Auto-generated method stub
TreeItem item=(TreeItem) event.item;
//for(int i=0;i
效果图:
2.table:
table就是一个没有父子的关系的tree
Styles:
和tree一致
Events:
除了 Collapse, Expand没有其他都有
TableColumn:
代表table的一列
TableItem:
代表table的一行
确定某行某列的一个小单元
public static void main(String[] args) {
new TestTable();
}
private TestTable() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setText("tableС¹¤³Ì");
createTable(shell);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/**
* ´´½¨±í¸ñ
*
* @param shell
*/
private void createTable(final Shell shell) {
table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION);
editor = new TableEditor(table);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
table.setHeaderVisible(true);
table.setLinesVisible(true);
final TableColumn col1 = new TableColumn(table, SWT.NONE);
col1.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
// TODO Auto-generated method stub
final Text txt=new Text(table,SWT.NONE);
txt.setText(col1.getText());
txt.forceFocus();
txt.setBounds(new Rectangle(10, 10, col1.getWidth(), table.getHeaderHeight()));
System.out.println(txt.getBounds());
txt.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
}
});
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
});
col1.setMoveable(true);
col1.setText("ÐÕÃû");
col1.setWidth(100);
TableColumn col2 = new TableColumn(table, SWT.LEFT);
col2.setText("ÄêÁä");
col2.setWidth(100);
TableColumn col5 = new TableColumn(table, SWT.LEFT);
col5.setText("ÐÔ±ð");
col5.setWidth(100);
TableColumn col3 = new TableColumn(table, SWT.LEFT);
col3.setText("µØÖ·");
col3.setWidth(100);
TableColumn col4 = new TableColumn(table, SWT.LEFT);
col4.setText("µç»°");
col4.setWidth(100);
/**
* Ìí¼Ó±í¸ñÊý¾Ý
*/
new TableItem(table, SWT.LEFT).setText(new String[] { "ÕÅÈý", "22", "ÄÐ",
"ÖØÇìÊÐ", "13022332356" });
new TableItem(table, SWT.LEFT).setText(new String[] { "СÇÅ", "22", "Å®",
"ÉϺ£ÊÐ", "13029872222" });
new TableItem(table, SWT.LEFT).setText(new String[] { "С¿É", "22", "ÄÐ",
"±±¾©ÊÐ", "13006800123" });
new TableItem(table, SWT.LEFT).setText(new String[] { "ÍõÎå", "31", "ÄÐ",
"Î人ÊÐ", "13098982201" });
new TableItem(table, SWT.LEFT).setText(new String[] { "ÌìÆô", "29", "ÄÐ",
"ÄÏÄþÊÐ", "13024374001" });
new TableItem(table, SWT.LEFT).setText(new String[] { "¸ßÀÖ", "25", "Å®",
"Ìì½òÊÐ", "13088888888" });
// ɾ³ý²Ëµ¥
Menu menu1 = new Menu(shell, SWT.POP_UP);
table.setMenu(menu1);
MenuItem menuitem1 = new MenuItem(menu1, SWT.PUSH);
menuitem1.setText("ɾ³ý");
menuitem1.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
MessageBox mbox = new MessageBox(shell, SWT.DIALOG_TRIM|SWT.ICON_INFORMATION);
mbox.setText("ɾ³ý³É¹¦");
mbox.setMessage("ɾ³ýÁË" + table.getSelectionCount() + "Ìõ¼Ç¼");
table.remove(table.getSelectionIndices());
mbox.open();
}
});
// ÐÞ¸Ätable
{
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseDoubleClick(MouseEvent e) {
Control c = editor.getEditor();
if (c != null) {
c.dispose();
}
// µÃµ½Ñ¡ÖеÄÐÐ
Point point = new Point(e.x, e.y);
// MessageDialog.openInformation(shell, null,
// point.x+","+point.y);
final TableItem tableitem = table.getItem(point);
// µÃµ½Ñ¡ÖеÄÁÐ
int column = -1;
for (int i = 0; i < table.getColumnCount(); i++) {
Rectangle rec = tableitem.getBounds(i);
if (rec.contains(point))
column = i;
}
final int col1 = column;
//ÐÞ¸ÄÄêÁ䣬½øÐÐ΢µ÷
if(col1==1){
final Spinner spiner=new Spinner(table,SWT.NONE);
spiner.setMaximum(120);
spiner.setMinimum(1);
spiner.setSelection(new Integer(tableitem.getText(1)));
editor.setEditor(spiner, tableitem, col1);
spiner.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
tableitem.setText(col1,String.valueOf(spiner.getSelection()));
}
});
}else if (col1 == 2) {
//ÐÞ¸ÄÐÔ±ðÓÃÏÂÀÁбí
final Combo comb = new Combo(table, SWT.READ_ONLY);
comb.setItems(new String[] { "ÄÐ", "Å®", "δ֪" });
comb.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
tableitem.setText(col1, comb.getText());
comb.dispose();
super.widgetSelected(e);
}
});
editor.setEditor(comb, tableitem, column);
}else{
//ÆäËûµÄÐ޸Ķ¼ÊÇÓÃÎı¾¿ò
final Text txt=new Text(table,SWT.NONE);
txt.setText(tableitem.getText(col1));
txt.forceFocus();
editor.setEditor(txt, tableitem, col1);
System.out.println(txt.getBounds());
txt.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
tableitem.setText(col1, txt.getText());
}
});
}
}
});
}
}
效果图:
(由于早前刚换windows10系统, 代码是我以前系统的学习例子,也许是没相关字体,所以中文都变成了乱码,对不起大家)
3.TabFolder和CTabFolder:
TabFolder(选项卡)是一个进行功能划分的控件,CTabFolder则是他的高级版
TabFolder Styles:TOP(将选项卡放在顶部) BOTTOM(将选项卡放在底部)
CTabFolder Styles:TOP(将选项卡放在顶部) BOTTOM(将选项卡放在底部) FLAT(为选项卡提供一个扁平的外观) BORDER(在控件周围现实边框) CLOSE(允许选项卡关闭)
TabFolder Events:selection,其他都是control 默认能触发的event
CTabFolder Events:selection,CTabFolder2Listener,其他也都是Control 默认能出发的event
TabItem :TabFolder的一个选项。
CTabItem :CTabFolder的一个选项。
下面我们着重看下CTabFolder2Listener下面有哪些方法:
(1)close 关掉一个CTabItem时触发,(假如在关掉当前这个item时,剩下还有item,CTabFolder默认会选中当前要关闭的item的后面一个,所以此回调基本都会出发selectionchange)。
(2)minimize:用了最小化按钮后触发
(3)maximize:用了最大化按钮后触发
(4)restore :用了恢复按钮后触发
(5)showList:在item太多,不够显示的时候,item的最右边会显示咯下拉箭头,showList就是在点了这个按钮后触发。
CTabFolder简单列子
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final TabFolder tabFolder = new TabFolder(shell, SWT.BORDER|SWT.BOTTOM);
for (int i = 0; i < 6; i++) {
TabItem item = new TabItem(tabFolder, SWT.NONE);
item.setText("TabItem " + i);
Button button = new Button(tabFolder, SWT.PUSH);
button.setText("Page " + i);
item.setControl(button);
}
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
效果图:
CTabFolder简单例子:
import java.util.ArrayList;
import org.eclipse.jface.dialogs.PopupDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabFolder2Listener;
import org.eclipse.swt.custom.CTabFolderEvent;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;
public class TestCTabFolder {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final CTabFolder tabFolder = new CTabFolder(shell, SWT.BORDER|SWT.BOTTOM|SWT.FLAT);
for (int i = 0; i < 20; i++) {
CTabItem item = new CTabItem(tabFolder, SWT.Close);
item.setText("CTabItem " + i);
Button button = new Button(tabFolder, SWT.PUSH);
button.setText("Page " + i);
item.setControl(button);
}
tabFolder.addCTabFolder2Listener(new CTabFolder2Listener() {
@Override
public void showList(CTabFolderEvent event) {
// TODO Auto-generated method stub
int x = event.x;
int y = event.y +event.height;
Point location = tabFolder.getDisplay().map(tabFolder, null, x, y);
TestPopupDialog dialog=new TestPopupDialog(tabFolder.getShell(), tabFolder, location.x, location.y);
dialog.open();
event.doit=false;
}
@Override
public void restore(CTabFolderEvent event) {
// TODO Auto-generated method stub
}
@Override
public void minimize(CTabFolderEvent event) {
// TODO Auto-generated method stub
}
@Override
public void maximize(CTabFolderEvent event) {
// TODO Auto-generated method stub
}
@Override
public void close(CTabFolderEvent event) {
// TODO Auto-generated method stub
}
});
shell.setSize(400, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
static public class TestPopupDialog extends PopupDialog {
Text text;
CTabFolder tabFolder;
Font boldF;
int x;
int y;
TableViewer tableViewer;
ActionContentProvider actionContentProvider;
public TestPopupDialog(Shell parent,CTabFolder tabFolder,int x,int y){
super(parent, SWT.NO_TRIM, true, false, false, false, false, null,
null);
this.tabFolder=tabFolder;
this.x=x;
this.y=y;
}
@Override
protected Control createDialogArea(Composite parent) {
// TODO Auto-generated method stub
final Composite composite = (Composite) super.createDialogArea(parent);
text=new Text(composite,SWT.NONE);
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true,
false).applyTo(text);
Label comseparator = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true,
false).applyTo(comseparator);
tableViewer=new TableViewer(composite,SWT.SINGLE|SWT.FULL_SELECTION);
int itemH=tableViewer.getTable().getItemHeight();
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).hint(-1,itemH*15).grab(true,
false).applyTo(tableViewer.getTable());
// TableColumn col=new TableColumn(tableViewer.getTable(), SWT.NONE);
// col.setText("djh");
FontData fd= Display.getCurrent().getSystemFont().getFontData()[0];
fd.setStyle(SWT.BOLD);;
boldF=new Font(null,fd);
actionContentProvider=new ActionContentProvider("");
tableViewer.setContentProvider(actionContentProvider);
tableViewer.setLabelProvider(new ActionLabelProvider());
tableViewer.setInput(tabFolder);
tableViewer.setSelection(new StructuredSelection(tabFolder.getSelection()));
tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
// TODO Auto-generated method stub
ISelection selection=event.getSelection();
if(selection!=null&&!selection.isEmpty()){
StructuredSelection ss=(StructuredSelection) selection;
tabFolder.setMRUVisible(false);
tabFolder.setSelection((CTabItem) ss.getFirstElement());
Listener[] listeners=tabFolder.getListeners(SWT.Selection);
for(Listener l:listeners){
Event e=new Event();
e.item=(Widget) ss.getFirstElement();
e.type=SWT.Selection;
e.widget=tabFolder;
l.handleEvent(e);
//e.widget=tabFolder;
}
close();
}
}
});
text.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
// TODO Auto-generated method stub
actionContentProvider.setText(text.getText());
tableViewer.refresh();
}
});
return composite;
}
class ActionContentProvider implements IStructuredContentProvider{
String text;
ActionContentProvider(String text){
this.text=text;
}
public void setText(String text) {
this.text = text;
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
// TODO Auto-generated method stub
}
@Override
public Object[] getElements(Object inputElement) {
// TODO Auto-generated method stub
if(inputElement instanceof CTabFolder){
CTabFolder tabFolder=(CTabFolder) inputElement;
ArrayList items=new ArrayList();
if(text.equals("")){
return tabFolder.getItems();
}
for(CTabItem item:tabFolder.getItems()){
if(item.getText().startsWith(text)){
items.add(item);
}
}
return items.toArray(new CTabItem[]{});
}
return new Object[]{};
}
}
class ActionLabelProvider extends StyledCellLabelProvider{
@Override
public void update(ViewerCell cell) {
// TODO Auto-generated method stub
CTabItem tabItem=(CTabItem) cell.getElement();
//CTabFolder tabFolder=tabItem.getParent();
if(cell.getColumnIndex()==0){
if(tabItem.isShowing()){
}else{
cell.setFont(boldF);
}
cell.setText(tabItem.getText());
}
super.update(cell);
}
}
@Override
protected void adjustBounds() {
// TODO Auto-generated method stub
// Point size=getShell().getSize();
// if(size.y>600){
// getShell().setSize(size.x, 600);
// }
getShell().setLocation(x, y);
}
@Override
public boolean close() {
// TODO Auto-generated method stub
//return false;
boolean flag= super.close();
if(flag){
boldF.dispose();
}
return flag;
}
}
}
效果图:
谢谢大家