swt table控件的使用(shell)

package swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.TableItem;


public class She extends Shell {
 private Table table;
 private TableItem tm;
 private TableItem tm_1;
 private TableItem tm_2;
 private Button button;

 /**
  * Launch the application.
  * @param args
  */
 public static void main(String args[]) {
  try {
   Display display = Display.getDefault();
   She shell = new She(display);
   shell.open();
   shell.layout();
   while (!shell.isDisposed()) {
    if (!display.readAndDispatch()) {
     display.sleep();
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 /**
  * Create the shell.
  * @param display
  */
 public She(Display display) {
  super(display, SWT.SHELL_TRIM);
  table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
  table.setBounds(10, 39, 414, 213);
  table.setHeaderVisible(true);
  table.setLinesVisible(true);
  
  TableColumn t = new TableColumn(table, SWT.NONE);
  t.setWidth(100);
  t.setText("姓名");
  
  TableColumn t_1 = new TableColumn(table, SWT.NONE);
  t_1.setWidth(100);
  t_1.setText("年龄 ");
  
  TableColumn t_2 = new TableColumn(table, SWT.NONE);
  t_2.setWidth(100);
  t_2.setText("性别 ");
  
  TableColumn t_3 = new TableColumn(table, SWT.NONE);
  t_3.setWidth(100);
  t_3.setText("身高");
  
  button = new Button(this, SWT.NONE);
  button.addSelectionListener(new SelectionAdapter() {
   @Override
   public void widgetSelected(SelectionEvent e) {
    
    tm = new TableItem(table, SWT.NONE);
    tm.setText(new String[]{"张三","22","男","175cm"});
    
    tm_1 = new TableItem(table, SWT.NONE);
    tm_1.setText(new String[]{"李四","21","女","172cm"});
    
    tm_2 = new TableItem(table, SWT.NONE);
    tm_2.setText(new String[]{"王五","25","男","170cm"});
    
   }
   
  });
  button.setBounds(146, 10, 80, 27);
  button.setText("/u663E/u793A ");
  createContents();
 }

 /**
  * Create contents of the shell.
  */
 protected void createContents() {
  
  
 }

 @Override
 protected void checkSubclass() {
  // Disable the check that prevents subclassing of SWT components
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个 SWT Table 带有 Combo 件的增加和删除行的完整代码示例: ```java import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.*; public class TableWithComboExample { private static String[] COLORS = {"Red", "Green", "Blue"}; private static String[] SIZES = {"Small", "Medium", "Large"}; public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Table with Combo Example"); shell.setLayout(new FillLayout()); // 创建表格Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION); table.setHeaderVisible(true); table.setLinesVisible(true); // 创建表头 TableColumn colorColumn = new TableColumn(table, SWT.NONE); colorColumn.setText("Color"); TableColumn sizeColumn = new TableColumn(table, SWT.NONE); sizeColumn.setText("Size"); // 添加 Combo 编辑器到列中 final TableEditor colorEditor = new TableEditor(table); final Combo colorCombo = new Combo(table, SWT.READ_ONLY); colorCombo.setItems(COLORS); colorEditor.grabHorizontal = true; colorEditor.setEditor(colorCombo, null, 0); final TableEditor sizeEditor = new TableEditor(table); final Combo sizeCombo = new Combo(table, SWT.READ_ONLY); sizeCombo.setItems(SIZES); sizeEditor.grabHorizontal = true; sizeEditor.setEditor(sizeCombo, null, 1); // 添加行数据 for (int i = 0; i < 5; i++) { TableItem item = new TableItem(table, SWT.NONE); item.setText(0, COLORS[i % COLORS.length]); item.setText(1, SIZES[i % SIZES.length]); } // 添加增加行按钮 Button addButton = new Button(shell, SWT.PUSH); addButton.setText("Add Row"); addButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { TableItem item = new TableItem(table, SWT.NONE); item.setText(0, COLORS[0]); item.setText(1, SIZES[0]); } }); // 添加删除行按钮 Button removeButton = new Button(shell, SWT.PUSH); removeButton.setText("Remove Row"); removeButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { int selectionIndex = table.getSelectionIndex(); if (selectionIndex != -1) { table.remove(selectionIndex); } } }); shell.setSize(400, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } } ``` 这个示例中,我们创建了一个带有两列的 SWT Table 件,第一列是包含颜色选项的 Combo 件,第二列是包含大小选项的 Combo 件。然后我们添加了两个按钮来添加和删除行。当添加行时,我们将新行添加到表格的末尾并将第一列和第二列的文本设置为默认值。当删除行时,我们从选择的行开始删除。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值