📄 table1.java
字号:
/**
* @作者:陈刚
* @Email:glchengang@yeah.net
* @Blog:http://blog.csdn.net/glchengang
*/
package swt;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class Table1 {
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(327, 253);
shell.setText("SWT Application");
//------------------新插入的界面核心代码------------------------
shell.setLayout(new FillLayout());
/*
* 创建一个Table对象,在式样里设置它:可多选、全列选择。 并用两条语句设置它显示表头和表格线。
*/
final Table table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION);
table.setHeaderVisible(true);
table.setLinesVisible(true);
/*
* 为Table增加两列并设置列宽都是80
*/
TableColumn col1 = new TableColumn(table, SWT.NONE);
col1.setText("ID");
col1.setWidth(80);
TableColumn col2 = new TableColumn(table, SWT.NONE);
col2.setText("姓名");
col2.setWidth(80);
/*
* 创建一个Composite容器,并放入两个按钮。
*/
Composite composite = new Composite(shell, SWT.NONE);
composite.setLayout(new RowLayout());
Button addButton = new Button(composite, SWT.NONE);
addButton.setText("新增");
addButton.addSelectionListener(new SelectionAdapter() {
int i = 1;
public void widgetSelected(SelectionEvent e) {
TableItem item = new TableItem(table, 0); //创建Item
item.setText(new String[] { "" + i, "陈刚" + i });//给Item设值
i++;
}
});
Button removeButton = new Button(composite, SWT.NONE);
removeButton.setText("删除");
removeButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
//得到装有所有被选择记录的序号的数组
int[] selInices = table.getSelectionIndices();
//Table根据数组中的序号将Item删除。
table.remove(selInices);
}
});
/**
* 监听Table的鼠标双击事件
*/
table.addMouseListener(new MouseListener() {
public void mouseDoubleClick(MouseEvent e) {
int selIndex = table.getSelectionIndex();
TableItem item = table.getItem(selIndex);
String str = "列1 = " + item.getText(0) + "\n列2 = " + item.getText(1);
MessageDialog.openInformation(null, null, str);
}
public void mouseDown(MouseEvent e) {
}
public void mouseUp(MouseEvent e) {
}
});
//------------------END---------------------------------------------
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -