我很难实现方法的监听器。
我有一个创建表的方法fillTableRoWData()
。如果条件同时,则在两个内部调用该方法。
我已将所有值存储在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();
}
}
答案 0 :(得分:1)
您应该真正考虑将TableViewer
与EditingSupport
一起使用。通过这样做,您不需要跟踪所选的值,它们将存储在您的bean类中。这是一个非常简单的示例,它显示了一个包含一个组合框列的表,您可以从中选择两个值:
import org.eclipse.jface.layout.*;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import java.util.*;
import java.util.List;
/**
* @author Sebastian Raubach
*/
public class StackOverflow
{
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout());
createMasterPart(shell);
shell.pack();
shell.setSize(400, 300);
shell.open();
shell.layout(true, true);
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static void createMasterPart(Composite parentComposite)
{
Composite composite = new Composite(parentComposite, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
composite.setLayout(new GridLayout(1, false));
Composite tableComposite = new Composite(composite, SWT.NONE);
TableColumnLayout tableColumnLayout = new TableColumnLayout();
tableComposite.setLayout(tableColumnLayout);
tableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
TableViewer tableViewer = new TableViewer(tableComposite, SWT.BORDER | SWT.FULL_SELECTION);
tableViewer.setContentProvider(ArrayContentProvider.getInstance());
Table table = tableViewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn tableColumn = tableViewerColumn.getColumn();
tableColumn.setText("Sample");
tableViewerColumn.setLabelProvider(new ColumnLabelProvider()
{
@Override
public String getText(Object element)
{
Dummy p = (Dummy) element;
return p.getValue();
}
});
tableViewer.addSelectionChangedListener(new ISelectionChangedListener()
{
@Override
public void selectionChanged(SelectionChangedEvent selectionChangedEvent)
{
StructuredSelection selection = (StructuredSelection) selectionChangedEvent.getSelection();
System.out.println(((Dummy) selection.getFirstElement()).getValue());
}
});
List<Dummy> elements = new ArrayList<>();
for (int i = 0; i < 20; i++)
{
elements.add(new Dummy("First option"));
}
tableViewer.setInput(elements);
tableColumnLayout.setColumnData(tableColumn, new ColumnWeightData(1, true));
/* Set the editing support here */
tableViewerColumn.setEditingSupport(new FirstValueEditingSupport(tableViewer));
}
private static class Dummy
{
public String value;
public Dummy(String value)
{
this.value = value;
}
public String getValue()
{
return value;
}
public void setValue(String value)
{
this.value = value;
}
}
public static class FirstValueEditingSupport extends EditingSupport
{
private final TableViewer viewer;
private final CellEditor editor;
private final String[] possibleValues = {"First option", "Second option"};
public FirstValueEditingSupport(TableViewer viewer)
{
super(viewer);
this.viewer = viewer;
this.editor = new ComboBoxCellEditor(viewer.getTable(), possibleValues);
}
@Override
protected CellEditor getCellEditor(Object element)
{
return editor;
}
@Override
protected boolean canEdit(Object element)
{
return true;
}
@Override
protected Object getValue(Object element)
{
Dummy dummy = (Dummy) element;
int index = 0;
for (int i = 0; i < possibleValues.length; i++)
{
if (Objects.equals(possibleValues[i], dummy.getValue()))
{
index = i;
break;
}
}
return index;
}
@Override
protected void setValue(Object element, Object value)
{
Dummy dummy = (Dummy) element;
int index = (Integer) value;
dummy.setValue(possibleValues[index]);
viewer.update(element, null);
}
}
}
选择项目时,它会打印当前选择。