【发布时间】:2017-01-11 08:10:59
【问题描述】:
我很难实现一个方法的监听器。
我有一个创建表的方法fillTableRoWData()。该方法在两个 if 条件内同时调用。
我已将所有值存储在 hahMap 中。
问题是当我更新组合框值时:仅更新第二个条件中的值。
我想添加一个监听器,告诉方法哪个 if 条件被更新,并在相关的 hashMap 位置添加数据。
此外,当我将数据输入任何 TableColoumn 时,数据应该在 HashMap 中更新
package view;
import java.util.HashMap;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
public class TableShellExample {
Display d;
Shell s;
Table table;
private Text text_1, text_2, text_3, text_4;
private HashMap<Integer, String> list2 = new HashMap<>();
private HashMap<Integer, HashMap<Integer, String>> list3 = new HashMap<>();
private static Integer a = 0;
private static Integer q = 0;
TableShellExample() {
d = new Display();
s = new Shell(d);
s.setSize(250, 200);
s.setText("A Table Shell Example");
Table table = new Table(s, SWT.BORDER);
String[] titles = { "Theart Name", "Category", "Satus", "Priority",
"Description", "Justification" };
for (int i = 0; i < titles.length; i++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(150);
column.setText(titles[i]);
}
table.setHeaderVisible(true);
fillRows("1","2","3");
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
private void fillRows(String shortdesc, String categ, String descp) {
TableItem item = new TableItem(table, SWT.NONE);
// for Threat_Name
TableEditor editor = new TableEditor(table);
text_1 = new Text(table, SWT.READ_ONLY);
editor.grabHorizontal = true;
editor.setEditor(text_1, item, 0);
text_1.setText(shortdesc);
// list2.put(a++, text_1.getText());
System.out.println(a + " : " + list2);
// For Category_Name
//editor = new TableEditor(table);
text_2 = new Text(table, SWT.READ_ONLY);
editor.grabHorizontal = true;
editor.setEditor(text_2, item, 1);
text_2.setText(categ);
// list2.put(a++, text_2.getText());
System.out.println(a + " : " + list2);
// For Status_Name
editor = new TableEditor(table);
final Combo Status_Combo = new Combo(table, SWT.READ_ONLY);
Status_Combo.add("Mitigated");
Status_Combo.add("Not Applicable");
Status_Combo.add("Not Started");
Status_Combo.add("Needs Investigation");
editor.grabHorizontal = true;
editor.setEditor(Status_Combo, item, 2);
Status_Combo.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
System.out.println(Status_Combo.getText());
}
public void widgetDefaultSelected(SelectionEvent e) {
System.out.println(Status_Combo.getText());
}
});
// For Priority_Name
editor = new TableEditor(table);
final Combo priority_Combo = new Combo(table, SWT.READ_ONLY);
priority_Combo.add("High");
priority_Combo.add("Medium");
priority_Combo.add("Low");
editor.grabHorizontal = true;
editor.setEditor(priority_Combo, item, 3);
priority_Combo.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
System.out.println(priority_Combo.getText());
}
public void widgetDefaultSelected(SelectionEvent e) {
System.out.println(priority_Combo.getText());
}
});
// For Descrption_Name
editor = new TableEditor(table);
text_3 = new Text(table, SWT.READ_ONLY);
editor.grabHorizontal = true;
editor.setEditor(text_3, item, 4);
text_3.setText(descp);
// list2.put(a++, text_3.getText());
System.out.println(a + " : " + list2);
System.out.println(list3);
// For justification
editor = new TableEditor(table);
text_4 = new Text(table, SWT.MULTI | SWT.BORDER | SWT.WRAP
| SWT.V_SCROLL);
editor.grabHorizontal = true;
editor.setEditor(text_4, item, 5);
list3.put(q++, new HashMap() {
{
put(a, text_1.getText());
put((a + 1), text_2.getText());
put((a + 2), text_3.getText());
}
});
}
public static void main(String[] argv) {
new TableShellExample();
}
}
【问题讨论】:
-
没有解释的否决票是没有帮助的。他们还可以阻止新用户询问和寻求帮助或建议。我认为应该尽可能避免新用户的否决投票问题。对于那些我建议相反的人:解释而不投反对票。
-
这是一个相当长的代码,我不确定我是否理解问题所在。考虑发布minimal reproducible example 来演示问题,并更轻松地为您提供帮助。
-
现在很难为您提供帮助。请仔细阅读minimal reproducible example。使其最小化(“创建一个新程序,仅添加需要的内容”)。使用运行它所需的所有类和导入使其 COMPLETE。
-
我们的想法不是“把所有东西都放在这里”,而是“创建一个新程序,只添加需要的东西”。演示问题的小/短代码。
-
@S.Mehta 您提供的代码甚至无法编译。请确保它确实如此。人们不太可能浏览您的代码并弄清楚如何使其编译,然后再次浏览您的代码以找出问题所在。此外,它应该是独立的,即人们可以运行它。包括一个
main方法。