首页 > 解决方案 > 将组合框设置为 java.swt 表中几列的 TableItems 的单元格内容

问题描述

我正在构建一个表,并且正在迭代“methodtraces2”列表以设置该表的每一行。我希望 4 列有一个组合框作为它们的单元格内容。更具体地说,我希望“Callers”、“Callees”、“CallersExecuted”和“CalleesExecuted”列将组合框作为其单元格内容。组合框的每个条目都应对应于适当列表元素的 ToString 行为。“呼叫者”列中的每个单元格都应填充一个组合框,并且每个组合框应包含List<Method2Representation>呼叫者中的所有条目。同样,“被调用者”列中的每个单元格都应该填充一个组合框,并且每个组合框都应该包含List<Method2Representation>被调用者。同样适用于“callersexecuted”和“calleesexecuted”。我需要组合框的列在下面的代码中表示为注释,用于填充这些组合框的列表也在下面的代码中表示为注释:

    shell = new Shell();
    shell.setSize(1500, 1500);
    shell.setText("SWT Application TEST");

    table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
    table.setBounds(0, 10, 2000, 2000);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    TableColumn tblclmnMethodID = new TableColumn(table, SWT.NONE);
    tblclmnMethodID.setWidth(100);
    tblclmnMethodID.setText("MethodID");

    TableCursor tableCursor = new TableCursor(table, SWT.NONE);

    TableColumn tblclmnMethodName = new TableColumn(table, SWT.NONE);
    tblclmnMethodName.setWidth(100);
    tblclmnMethodName.setText("MethodName");

    TableColumn tblclmnRequirementID = new TableColumn(table, SWT.NONE);
    tblclmnRequirementID.setWidth(153);
    tblclmnRequirementID.setText("RequirementID");

    TableColumn tblclmnRequirementName = new TableColumn(table, SWT.NONE);
    tblclmnRequirementName.setWidth(150);
    tblclmnRequirementName.setText("RequirementName");

    TableColumn tblclmnClassID = new TableColumn(table, SWT.NONE);
    tblclmnClassID.setWidth(100);
    tblclmnClassID.setText("ClassID");

    TableColumn tblclmnClassName = new TableColumn(table, SWT.NONE);
    tblclmnClassName.setWidth(100);
    tblclmnClassName.setText("ClassName");

    TableColumn tblclmnGold = new TableColumn(table, SWT.NONE);
    tblclmnGold.setWidth(100);
    tblclmnGold.setText("Gold");

    TableColumn tblclmnSubject = new TableColumn(table, SWT.NONE);
    tblclmnSubject.setWidth(100);
    tblclmnSubject.setText("Subject");

    TableColumn tblclmnGoldpredictionCaller = new TableColumn(table, SWT.NONE);
    tblclmnGoldpredictionCaller.setWidth(150);
    tblclmnGoldpredictionCaller.setText("GoldPredictionCaller");

    TableColumn tblclmnGoldpredictionCallee = new TableColumn(table, SWT.NONE);
    tblclmnGoldpredictionCallee.setWidth(170);
    tblclmnGoldpredictionCallee.setText("GoldPredictionCallee");
    //COMBO BOX NEEDED FOR CALLERS RETRIEVED FROM 
    // List<Method2Representation> callers
    TableColumn tblclmnCallers = new TableColumn(table, SWT.NONE);
    tblclmnCallers.setWidth(100);
    tblclmnCallers.setText("Callers");
    //COMBO BOX NEEDED FOR CALLEES RETRIEVED FROM 
    // List<Method2Representation> callees  
    TableColumn tblclmnCallees = new TableColumn(table, SWT.NONE);
    tblclmnCallees.setWidth(100);
    tblclmnCallees.setText("Callees");
    //COMBO BOX NEEDED FOR CALLERSEXECUTED RETRIEVED FROM 
    // List<Method2Representation> callersExecuted
    TableColumn tblclmnCallersExecuted = new TableColumn(table, SWT.NONE);
    tblclmnCallersExecuted.setWidth(150);
    tblclmnCallersExecuted.setText("CallersExecuted");
    //COMBO BOX NEEDED FOR CALLEESEXECUTED RETRIEVED FROM 
    // List<Method2Representation> calleesExecuted
    TableColumn tblclmnCalleesExecuted = new TableColumn(table, SWT.NONE);
    tblclmnCalleesExecuted.setWidth(150);
    tblclmnCalleesExecuted.setText("CalleesExecuted");



    for(MethodTrace2 meth: methodtraces2) {
        TableItem item1 = new TableItem(table, SWT.NONE);
        //callees combobox should populate tblclmnCallees
        List<Method2Representation> callees = meth.getCalleesList(); 
        //callers combobox should populate tblclmnCallers
        List<Method2Representation> callers = meth.getCallersList(); 
        //callersExecuted combobox should populate tblclmnCallersExecuted 
        List<Method2Representation> callersExecuted = meth.getCallersListExecuted(); 
        //calleesExecuted combobox should populate tblclmnCalleesExecuted 
        List<Method2Representation> calleesExecuted = meth.getCalleesListExecuted(); 
        item1.setText(new String[] { meth.MethodRepresentation.getMethodid(), meth.MethodRepresentation.getMethodname(), meth.Requirement.getID(), meth.Requirement.RequirementName, meth.ClassRepresentation.classid, meth.ClassRepresentation.classname, meth.gold
                , meth.subject, meth.goldpredictionCaller, meth.goldpredictionCallee});
    }

这是我的 Method2Representation 类,我希望组合框中的每一行都等于 Method2Representation 类的 toString 行为。这是下面的课程:

public class Method2Representation {
String methodid; 
String methodname;
List<Requirement2> requirements; 
public Method2Representation(String methodid, String methodname) {
    super();
    this.methodid = methodid;
    this.methodname = methodname;
}
public Method2Representation() {
    // TODO Auto-generated constructor stub
}
public String getMethodid() {
    return methodid;
}
public void setMethodid(String methodid) {
    this.methodid = methodid;
}
public String getMethodname() {
    return methodname;
}
public void setMethodname(String methodname) {
    this.methodname = methodname;
}
@Override
public String toString() {
    return "Method2Representation [methodid=" + methodid + ", methodname=" + methodname + "]";
}
public List<Requirement2> getRequirements() {
    return requirements;
}
public void setRequirements(List<Requirement2> requirements) {
    this.requirements = requirements;
}
public Method2Representation(String methodid, String methodname, List<Requirement2> requirements) {
    super();
    this.methodid = methodid;
    this.methodname = methodname;
    this.requirements = requirements;
}

}       

标签: javacomboboxswt

解决方案


我个人更喜欢使用 JfaceTableViewerComboBoxCellEditor进行单元格编辑。

但下面是在单元格中添加组合的示例SWT Table

public class TableExmaple {
  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.MULTI);
    table.setLinesVisible(true);
    for (int i = 0; i < 3; i++) {
      TableColumn column = new TableColumn(table, SWT.NONE);
      column.setWidth(100);
    }
    for (int i = 0; i < 12; i++) {
      new TableItem(table, SWT.NONE);
    }
    TableItem[] items = table.getItems();
    for (int i = 0; i < items.length; i++) {
      TableEditor tableEditor = new TableEditor(table);
      CCombo combo = new CCombo(table, SWT.NONE);
      combo.setText("CCombo");
      combo.add("combo item 1");
      combo.add("combo item 2");
      tableEditor.grabHorizontal = true;
      tableEditor.setEditor(combo, items[i], 0);
      tableEditor = new TableEditor(table);
      Text text = new Text(table, SWT.NONE);
      text.setText("Text");
      tableEditor.grabHorizontal = true;
      tableEditor.setEditor(text, items[i], 1);
      tableEditor = new TableEditor(table);
      Button button = new Button(table, SWT.CHECK);
      button.pack();
      tableEditor.minimumWidth = button.getSize().x;
      tableEditor.horizontalAlignment = SWT.LEFT;
      tableEditor.setEditor(button, items[i], 2);
    }
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}

推荐阅读