简体   繁体   English

如何使用swt向表中添加行

[英]How to add a row to a table using swt

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();
}

If you must use the TableViewer system to add items, you need to modify whatever Object is passed into the viewer as its input. 如果必须使用TableViewer系统添加项目,则需要修改传递给查看器的任何Object作为其输入。 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. 如果使用IStructuredContentProvider作为查看器的内容提供程序,则getElements方法将返回一个继续成为表的行的数组。 To update it after a change to the input, just call viewer.refresh() 要在更改输入后更新它,只需调用viewer.refresh()

Easy way for learning :Text field record add in Table and selected row can display on text field using SWT 简单的学习方法 :文本字段记录添加在表格中,选定的行可以使用SWT显示在文本字段上


package rcp_demo.Views;

import org.eclipse.swt.widgets.Composite;

public class TaskView extends ViewPart {

    public static Table table;
    public static TableViewer tableViewer;
    public static TableItem std_item;
    private  Text txt_1,txt_2,txt_3;

    Student stud=new Student();
    List<Student> std=new ArrayList<Student>();

    public TaskView() {
        setTitleImage(SWTResourceManager.getImage("D:\\Icon\\Tasksview.png"));
    }

    @Override
    public void createPartControl(Composite parent) {
        parent.setLayout(null);
        //Three Text
        txt_1 = new Text(parent, SWT.BORDER);
        txt_1.setBounds(21, 10, 76, 19);

        txt_2 = new Text(parent, SWT.BORDER);
        txt_2.setBounds(119, 10, 76, 19);

        txt_3 = new Text(parent, SWT.BORDER);
        txt_3.setBounds(222, 10, 76, 19);

        tableViewer = new TableViewer(parent, SWT.BORDER | SWT.FULL_SELECTION);
        table = tableViewer.getTable();

        //how to pass values of a selected row from tableviewer to a text box in SWT

         table.addListener(SWT.Selection, new Listener()
            {
                @Override
                public void handleEvent(Event e) {
                     Table table = (Table) e.widget;
                        TableItem item = table.getItem(table.getSelectionIndex());

                        /* Fill the texts */
                        for (int col = 0; col < table.getColumnCount(); col++)
                        {
                            if(col==0)
                            {
                                txt_1.setText(item.getText(col));
                            }
                            else if(col==1)
                            {
                                txt_2.setText(item.getText(col));
                            }
                            else if(col==2)
                            {
                                txt_3.setText(item.getText(col));
                            }
                        }
                }
            });
        table.setBounds(21, 47, 290, 213);
        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        //ADD Button

        Button btnAdd = new Button(parent, SWT.NONE);
        btnAdd.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {

                std.add(new Student(txt_1.getText(),txt_2.getText(),txt_3.getText()));
                std_item=new TableItem(table, SWT.NONE);
                std_item.setText(0,txt_1.getText());
                std_item.setText(1,txt_2.getText());
                std_item.setText(2,txt_3.getText());

            }
        });
        btnAdd.setBounds(304, 10, 68, 23);
        btnAdd.setText("Add");

                //Dynamic add column Name using TableViewerColumn 

                String[] Col_names={"Stud_id","Stud_Name","Stud_Gender"};
                for(int i=0;i<Col_names.length;i++)
                {
                    TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
                    TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
                    tblclmnNewColumn.setWidth(100);
                    tblclmnNewColumn.setText(Col_names[i]);

                }
                //(class: Student)static Item/Data Add

                std.add(new Student("110","Deni","Male"));
                std.add(new Student("111","Hina","Female"));
                std.add(new Student("112","Jem","Male"));


                for(Student s:std)
                {
                    TableItem std_item=new TableItem(table, SWT.NONE);
                    std_item.setText(0,s.getStd_id());
                    std_item.setText(1,s.getStd_nm());
                    std_item.setText(2,s.getStd_gender());
                }

    }
      public TableViewer getViewer() {
          return tableViewer;
  }
    @Override
    public void setFocus() {
        tableViewer.getControl().setFocus();

    }
}

Class name : Student.java 班级名称:Student.java


package rcp_demo.TableView;

public class Student {

private String std_id;
private String std_nm;
private String std_gender;

public Student() {
    // TODO Auto-generated constructor stub
}
public Student(String sid,String snm,String sgender) {
    std_id=sid;
    std_nm=snm;
    std_gender=sgender;
}

public String getStd_id() {
    return std_id;
}
public void setStd_id(String std_id) {
    this.std_id = std_id;
}
public String getStd_nm() {
    return std_nm;
}
public void setStd_nm(String std_nm) {
    this.std_nm = std_nm;
}
public String getStd_gender() {
    return std_gender;
}
public void setStd_gender(String std_gender) {
    this.std_gender = std_gender;
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM