如何使用swt向表中添加一行 [英] How to add a row to a table using swt

查看:23
本文介绍了如何使用swt向表中添加一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Swing 并且对将行插入表有一个疑问.我的要求是我必须通过按添加按钮来添加一个新行.但我无法继续.请找到以下代码:

I am learning swing and have one doubt regarding insertion of row to a table. My requirement is such that I have to add a new row by pressing a add button. But I am not able to proceed. please find the code below:

如果有人知道请帮我....

If some one know please help me....

{public class TableShellExample {


Display d;
 Shell s;
 TableViewer tableViewer;
 CellEditor cellEditor;

 TableShellExample(){
  d = new Display();
  s = new Shell();
  s.setSize(250,250);
  s.setText("Table Shell Example");

  GridLayout g1 = new GridLayout();
  g1.numColumns = 3;
  s.setLayout(g1);
  final Table table = new Table(s,SWT.BORDER |SWT.CHECK|SWT.MULTI | SWT.FULL_SELECTION);
  GridData gd = new GridData(GridData.FILL_BOTH);
  gd.horizontalSpan = 3;
  table.setLayoutData(gd);
  table.setHeaderVisible(true);

  TableColumn tc1 = new TableColumn(table, SWT.LEFT);
  TableColumn tc2 = new TableColumn(table,SWT.CENTER);
  TableColumn tc3 = new TableColumn(table,SWT.CENTER);
  tc1.setText("FIRST NAME");
  tc2.setText("LAST NAME");
  tc3.setText("ADDRESS");
  tc1.setWidth(70);
     tc2.setWidth(70);
     tc3.setWidth(80);
  TableItem it1 = new TableItem(table,SWT.NONE);
  it1.setText(new String[]{"aaa","bbb","pune"});
  TableItem it2 = new TableItem(table,SWT.NONE);
  it2.setText(new String[]{"aaa","bbb","pune"});
  TableItem it3 = new TableItem(table,SWT.NONE);
  it3.setText(new String[]{"aaa","bbb","pune"});

  //tableViewer = new TableViewer(table);
  //tableViewer.setColumnProperties(tc1);
  //tableViewer.setContentProvider(new IContentProvider());
  //tableViewer.setLabelProvider(new TableLabelProvider());

  CellEditor[] editors = new CellEditor[2];
  //editors[0] = new TextCellEditor(table);
  //editors[1] = new TextCellEditor(table);
  //tableViewer.setCellEditors(editors);
  //tableViewer.setCellModifier(new ICellModifier());


  final Text input = new Text(s, SWT.SINGLE | SWT.BORDER);
     input.setTextLimit(5);
     final Button searchBtn = new Button(s, SWT.BORDER | SWT.PUSH);
     searchBtn.setText("Search");
  searchBtn.addSelectionListener(new SelectionAdapter() {
  public void widgetSelected(SelectionEvent e){
   TableItem[] tia = table.getItems();
   for(int i=0;i<tia.length;i++){
    tia[i].getText();
     //tia[i].setBackground(new Color(d, 129, 178, 127));
    //}


   }
  }

  });
  final Button addButton = new Button(s,SWT.BORDER | SWT.PUSH);
  addButton.setText("Add Row");
  addButton.setToolTipText("for addind a new row");
  addButton.addListener(SWT.Selection, new Listener() {

   public void handleEvent(Event arg0) {
    TableEditor te = new TableEditor(table);
    te.grabHorizontal = true;
    te.grabVertical = true;
    te.getItem();
    TableItem ti = table.getItem(0);
    ti.getText();


   }
  });
  s.open();
     while (!s.isDisposed()) {
       if (!d.readAndDispatch())
         d.sleep();
     }
     d.dispose();
 }
 public Vector rowToAdd() {
  Vector defaultRow = new Vector();
  defaultRow.add("column1");
  defaultRow.add("column1");
  return defaultRow;
  }

 public static void main(String[] argv){
  new TableShellExample();
 }
}

推荐答案

以下是一个简单的工作示例,说明如何在按下按钮时向表格添加项目:

Here's a simple working example of how to add items to a table when a button is pressed:

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    RowLayout layout = new RowLayout(SWT.VERTICAL);
    layout.fill = true;
    shell.setLayout(layout);
    shell.setSize(200, 200);
    final Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
    final Text text = new Text(shell, SWT.SINGLE | SWT.BORDER);
    text.setText("blahblah text");
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Push me");

    // this is the code you want
    button.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            TableItem item = new TableItem(table, SWT.NONE);
            item.setText(text.getText());
        }
    });

    for (int i = 0; i < 5; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText("*** Item " + i + "***");
    }
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

如果必须使用 TableViewer 系统来添加项目,则需要修改作为输入传递给查看器的任何对象.如果您使用 IStructuredContentProvider 作为查看器的内容提供程序,getElements 方法将返回一个数组,该数组将继续成为您的表的行.要在输入更改后更新它,只需调用 viewer.refresh()

If you must use the TableViewer system to add items, you need to modify whatever Object is passed into the viewer as its input. If you use an IStructuredContentProvider as your viewer's content provider, the getElements method returns an array that goes on to become your table's rows. To update it after a change to the input, just call viewer.refresh()

这篇关于如何使用swt向表中添加一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆