gpt4 book ai didi

java - 将组合框设置为 java.swt 表中几列的 TableItems 的单元格内容

转载 作者:行者123 更新时间:2023-12-02 11:08:15 25 4
gpt4 key购买 nike

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

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

}

最佳答案

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

但下面是在 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();
}
}

关于java - 将组合框设置为 java.swt 表中几列的 TableItems 的单元格内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50773832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com