SWT 类似Excel 的表格

13年前

//在网上看到的。我可没说是我写的。就是给大家分享一下。相互交流么~
package com.ui;


import org.eclipse.swt.SWT;

/**
 * Eclipse 3.1
 * @author jrkui
 */
public class MatrixTable {

 protected Shell shell;

 final Display display = Display.getDefault();

 protected Table table;

 private Text[][] text;

 private String tableName;

 private Rectangle rectangle;

 private int numColumn;

 private int numButton;

 private int imageNum;

 private int style;

 private String[] stringColumn;

// private String[] stringButton;

 /**
  * 初始化! Item的高默认为16个像素大小! 参数sColumn、sButton分别用来指定水平Column与竖直Column的名字!
  * style的值默认为SWT.CENTER!
  * Table的名字默认为“MartixTable”!
  * 位置默认为x=200,y=200
  * 大小默认为400*400
  */
 public MatrixTable(String[] sColumn) {
  imageNum = 50;
  style = SWT.CENTER;
  tableName = "MatrixTable!";
  rectangle = new Rectangle(200,200,400,400);
  this.numColumn = sColumn.length;
  //this.numButton = sButton.length;
  stringColumn = new String[sColumn.length];
  //stringButton = new String[sButton.length];
  int i;
  // String x;
  for (i = 0; i < sColumn.length; i++) {
// stringColumn[i].copyValueOf(sColumn[i].toCharArray(),0,sColumn.length);
// stringColumn[i] = sColumn[i];
stringColumn[i] = new String(sColumn[i]);
  }
 // for (i = 0; i < sButton.length; i++)
// stringButton[i].copyValueOf(sButton[i].toCharArray(),0,sButton.length);
//stringButton[i] = new String(sButton[i]);
  // stringButton[i] = sButton[i];
 }

 /**
  * 设置item的高度!
  */
 public void setItemH(int inum) {
  imageNum = inum;
 }

 /**
  * 返回Table的位置、大小信息
  *
  * @return Rectangle
  */
 public Rectangle getBound() {
  return rectangle;
 }

 /**
  * 设置Table的位置、大小信息 其中x、y设置位置,h、w设置大小
  */
 public void setBound(int x, int y, int h, int w) {
  rectangle = new Rectangle(x, y, h, w);
 }

 /**
  * 获得table的名字
  */
 public String getTableName() {
  return tableName;
 }

 /**
  * 设置table的名字
  */
 public void setTableName(String tableName) {
  this.tableName = tableName;
 }

 /*
  *
  */
 public void open() {
  createContents();
  shell.open();
  shell.layout();
  while (!shell.isDisposed()) {
if (!display.readAndDispatch())
 display.sleep();
  }
 }

 protected void createContents() {
  shell = new Shell();
 
  shell.setText(getTableName());
  shell.setBounds(getBound());

  shell.setLayout(new FillLayout());

  table = new Table(shell, SWT.SINGLE | SWT.FULL_SELECTION
 | SWT.HIDE_SELECTION);
  table.setHeaderVisible(true);
  table.setLinesVisible(true);

  setColumn();
  setButton();
 }

 /**
  * 设置水平Column
  */
 private void setColumn() {
  for (int i = 0; i < stringColumn.length; i++) {
TableColumn column = new TableColumn(table, SWT.CENTER);
column.setText(stringColumn[i]);
column.pack();
  }
 }

 /**
  * 设置竖直Column与可编辑的Text
  */
 private void setButton() {
  TableEditor editor[] = new TableEditor[numColumn];

  Button button[] = new Button[numColumn];

  text = new Text[numButton][numColumn];
  for (int i = 0; i < numButton; i++)
for (int j = 0; j < numColumn; j++) {
 text[i][j] = new Text(table, style);
}

  for (int i = 0; i < numButton; i++) {

TableItem item = new TableItem(table, SWT.NONE);
Image image = new Image(display, 1, imageNum);
item.setImage(image);
//button[i] = new Button(table, SWT.PUSH);
//button[i].setText(stringButton[i]);
editor[i] = new TableEditor(table);
editor[i].grabHorizontal = true;
editor[i].minimumHeight = button[i].getSize().y;
editor[i].minimumWidth = button[i].getSize().x;
editor[i].setEditor(button[i], item, 0);

for (int j = 0; j < numColumn; j++)
 editor[i].setEditor(text[i][j], item, j);
  }
 }

 /**
  * 获得Text的引用
  */
 public Text[][] getText() {
  return text;
 }
 
 public int getStyle() {
  return style;
 }

 public void setStyle(int style) {
  if (style == SWT.CENTER || style == SWT.RIGHT || style == SWT.LEFT)
this.style = style;
  else{
MessageBox b = new MessageBox(new Shell());
b.setText("错误!");
b.setMessage("style的值为SWT.CENTER或SWT.RIGHT或style == SWT.LEFT!");
b.open();
  }

 }

 public static void main(String a[]) {
  String tColumn[] = { "1", "2", "3", "4","5","6","7","8","9","10" };

  String tButton[] = { "1", "2", "3", "4","5","6","7","8","9","10" };

  try {
MatrixTable m = new MatrixTable(tColumn);
m.setTableName("像Excel的Table");
m.setBound(50, 100, 1000, 550);
m.setStyle(SWT.RIGHT);
m.open();
  } catch (Exception e) {
e.printStackTrace();
  }
 }

 public int getNumButton() {
  return numButton;
 }

 public int getNumColumn() {
  return numColumn;
 }

}