SWT里的表格编程(翻译)


Tables
SWT使用Table、TableColumn和TableItem这3个独立的类来构建table.
这3个类都不需要被继承.

*****Creating Tables

Table类只提供了一个构造方法:
public Table(Composite parent, int style)

下表显示了style的作用,在构造方法中允许用逻辑与操作符表示style.
Style                Description
SWT.SINGLE            一次只能选择一行,默认的参数.
SWT.MULTI            一次能选择多行,一般是使用Ctrl+鼠标实现选择多行.
SWT.CHECK            将放置一个checkbox在每行的最开头处.注意checkbox
                    的选择状态与表格行是否处于被选择的状态无关.                    
SWT.FULL_SELECTION    当表格行被选择时,高亮显示,而不是默认的只显示第一列.
SWT.HIDE_SELECTIN    当表格所在的窗口不是当前的窗口时,除去被选择行的高亮
                    显示.默认的是无论表格所在窗口是否为当前窗口都使被选
                    择行处于高亮显示.
                    
*****Adding Columns

TableColumn类表示表格里的一列,你创建一列使用一个parent table, a style
,和一个可选的index.如果你不设置index,那么这列就将被默认把index设置为0.

TableColumn的构造方法:
public TableColumn(Table parent, int style)
public TableColumn(Table parent, int style, int index)

所有设置表格列的内容的对齐参数有:
SWT.LEFT    左对齐
SWT.CENTER    居中
SWT.RIGHT    右对齐

只能设置其中一个参数,如果设置了多于一个参数,后果将不确定.可以在构造方法后改
变对齐:通过setAlignment()方法.默认对齐是左对齐并且影响在这列中所有的行.
表格列能够显示标题.每个标题只能显示一行,它将按照ASC2码显示字符.
parent table可以通过setHeadersVisible()方法控制标题是否显示.

*****Adding Rows

构造方法:
TableItem(Table parent, int style)
TableItem(Table parent, int style, int index)

使用第2个参数在表格中插入行,将已经存在的行往后移,如果传入的参数超过范围就抛
出IllegalArgumentException.比如,如果没有行存在与table中,这样写的话:
new TableItem(table, SWT.NONE, 1);
结果如下:
java.lang.IllegalArgumentException: Index out of bounds
没有style提供给TableItem,所以你必须经常传递SWT.NONE参数以忽略其它值.
你可以改变TableItem的前景颜色和背景颜色,以整行宽或者一个单元格宽都可以.单
元格能够显示文字,图片,或者两者都显示.如果你两者都设置了,图片将显示在文字的左
方.

*****示例程序:
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
 * Displays ASCII Codes
 */
public class AsciiTable {
  // The number of characters to show.
  private static final int MAX_CHARS = 128;
  // Names for each of the columns
  private static final String[] COLUMN_NAMES = { "Char", "Dec", "Hex", "Oct",
      "Bin", "Name"};

  // The names of the first 32 characters
  private static final String[] CHAR_NAMES = { "NUL", "SOH", "STX", "ETX", "EOT",
      "ENQ", "ACK", "BEL", "BS", "TAB", "LF", "VT", "FF", "CR", "SO", "SI",
      "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", "CAN", "EM", "SUB",
      "ESC", "FS", "GS", "RS", "US", "Space"};

  // The font to use for displaying characters
  private Font font;

  // The background colors to use for the rows
  private Color[] colors = new Color[MAX_CHARS];

  /**
   * Runs the application
   */
  public void run() {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("ASCII Codes");
    createContents(shell);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    // Call dispose to dispose any resources
    // we have created
    dispose();
    display.dispose();
  }

  /**
   * Disposes the resources created
   */
  private void dispose() {
    // We created this font; we must dispose it
    if (font != null) {
      font.dispose();
    }

    // We created the colors; we must dispose them
    for (int i = 0, n = colors.length; i < n; i++) {
      if (colors[i] != null) {
        colors[i].dispose();
      }
    }
  }

  /**
   * Creates the font
   */
  private void createFont() {
    // Create a font that will display the range
    // of characters. "Terminal" works well in
    // Windows
    font = new Font(Display.getCurrent(), "Terminal", 10, SWT.NORMAL);
  }

  /**
   * Creates the columns for the table
   *
   * @param table the table
   * @return TableColumn[]
   */
  private TableColumn[] createColumns(Table table) {
    TableColumn[] columns = new TableColumn[COLUMN_NAMES.length];
    for (int i = 0, n = columns.length; i < n; i++) {
      // Create the TableColumn with right alignment
      columns[i] = new TableColumn(table, SWT.RIGHT);

      // This text will appear in the column header
      columns[i].setText(COLUMN_NAMES[i]);
    }
    return columns;
  }

  /**
   * Creates the window's contents (the table)
   *
   * @param composite the parent composite
   */
  private void createContents(Composite composite) {
    composite.setLayout(new FillLayout());

    // The system font will not display the lower 32
    // characters, so create one that will
    createFont();

    // Create a table with visible headers
    // and lines, and set the font that we
    // created
    Table table = new Table(composite, SWT.SINGLE | SWT.FULL_SELECTION);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    table.setRedraw(false);
    table.setFont(font);

    // Create the columns
    TableColumn[] columns = createColumns(table);

    for (int i = 0; i < MAX_CHARS; i++) {
      // Create a background color for this row
      colors[i] = new Color(table.getDisplay(), 255 - i, 127 + i, i);

      // Create the row in the table by creating
      // a TableItem and setting text for each
      // column
      int c = 0;
      TableItem item = new TableItem(table, SWT.NONE);
      item.setText(c++, String.valueOf((char) i));
      item.setText(c++, String.valueOf(i));
      item.setText(c++, Integer.toHexString(i).toUpperCase());
      item.setText(c++, Integer.toOctalString(i));
      item.setText(c++, Integer.toBinaryString(i));
      item.setText(c++, i < CHAR_NAMES.length ? CHAR_NAMES[i] : "");
      item.setBackground(colors[i]);
    }

    // Now that we've set the text into the columns,
    // we call pack() on each one to size it to the
    // contents
    for (int i = 0, n = columns.length; i < n; i++) {
      columns[i].pack();
    }

    // Set redraw back to true so that the table
    // will paint appropriately
    table.setRedraw(true);
  }

  /**
   * The application entry point
   *
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    new AsciiTable().run();
  }
}
*****Sorting Tables
用户希望能通过点击表格标题使每行按照列排序,以升序或者降序交替.SWT表格不会自动
做这些事,但是可以通过编程实现,要实现排序,你可以按照下面的步骤:
1.为列标题栏增加一个listener,这样可以侦察到标题栏被鼠标点击.
2.取得现在要排序的信息.(按哪一列排序,按哪个方向排序--升序或降序)
3.对数据进行排序并且重新显示.

*****小结
如果你是一个Swing develloper,你一定会取笑SWT表格这种排序方法:取得数据,排序,
然后重新填入.用SWT编程意味着在widget level上,使用data(model),view,并且意
味着view和controller不可分割.象Swing的表格那样将data和view分离,并且用MVC
模式使用controller,使表格排序更加直接.
但是记住虽然SWT是在widget的level上,但是JFace提供了一个在SWT之上的MVC层次编
程,从而使在JFace中编程变得象Swing一样简单.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 Java 的 SWT 中,要使表格Table)中的数据可以复制,您需要为表格添加复制功能的相关代码。以下是一个示例代码,演示如何实现表格数据的复制功能: ```java import org.eclipse.swt.SWT; import org.eclipse.swt.dnd.*; import org.eclipse.swt.layout.FillLayout; 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 TableCopyExample { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); // 创建表格 Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION); table.setHeaderVisible(true); // 添加表格TableColumn column1 = new TableColumn(table, SWT.NONE); column1.setText("Column 1"); TableColumn column2 = new TableColumn(table, SWT.NONE); column2.setText("Column 2"); // 添加表格项 for (int i = 0; i < 10; i++) { TableItem item = new TableItem(table, SWT.NONE); item.setText(new String[] { "Data " + (i + 1) + ", 1", "Data " + (i + 1) + ", 2" }); } // 设置表格的复制剪贴板操作 table.addListener(SWT.KeyDown, event -> { if (event.stateMask == SWT.CTRL && event.keyCode == 'c') { StringBuilder sb = new StringBuilder(); TableItem[] selection = table.getSelection(); for (TableItem item : selection) { for (int i = 0; i < table.getColumnCount(); i++) { sb.append(item.getText(i)).append("\t"); } sb.append("\n"); } // 将复制的数据放入剪贴板 if (sb.length() > 0) { TextTransfer textTransfer = TextTransfer.getInstance(); Clipboard clipboard = new Clipboard(display); clipboard.setContents(new Object[] { sb.toString() }, new Transfer[] { textTransfer }); clipboard.dispose(); } } }); // 调整表格列宽 column1.pack(); column2.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } } ``` 这个示例代码创建了一个带有两列的表格,并添加了一些数据。通过按下 `Ctrl+C` 键,可以将选中的表格数据复制到剪贴板中,您可以在其他应用程序中粘贴这些数据。 请注意,这个示例只提供了基本的复制功能,如果您需要更复杂的功能,比如支持表格中的格式、富文本等,请参考 SWT 的更高级特性和相应的库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值