Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中向类内的方法添加侦听器_Java_Swt_Listener - Fatal编程技术网

在Java中向类内的方法添加侦听器

在Java中向类内的方法添加侦听器,java,swt,listener,Java,Swt,Listener,我很难实现方法的侦听器 我有一个方法fillTableRoWData(),可以创建一个表。该方法在两个if条件内同时调用 我已将我的所有值存储在地图中 问题是当我更新combobox值时:只更新第二个条件中的值 我想添加一个监听器,告诉方法如果条件被更新了,那么它将在相关的hashMap位置添加数据 此外,当我将数据输入任何TableColumn时,数据应该在HashMap中更新 package view; import java.util.HashMap; import org.eclip

我很难实现方法的侦听器

我有一个方法
fillTableRoWData()
,可以创建一个表。该方法在两个if条件内同时调用

我已将我的所有值存储在地图中

问题是当我更新combobox值时:只更新第二个条件中的值

我想添加一个监听器,告诉方法如果条件被更新了,那么它将在相关的hashMap位置添加数据

此外,当我将数据输入任何TableColumn时,数据应该在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();
    }

}
包视图;
导入java.util.HashMap;
导入org.eclipse.swt.swt;
导入org.eclipse.swt.custom.TableEditor;
导入org.eclipse.swt.events.SelectionEvent;
导入org.eclipse.swt.events.SelectionListener;
导入org.eclipse.swt.widgets.Combo;
导入org.eclipse.swt.widgets.Display;
导入org.eclipse.swt.widgets.Shell;
导入org.eclipse.swt.widgets.Table;
导入org.eclipse.swt.widgets.TableColumn;
导入org.eclipse.swt.widgets.TableItem;
导入org.eclipse.swt.widgets.Text;
公共类表示例{
显示d;
贝壳;
表格;
私有文本文本1、文本2、文本3、文本4;
private HashMap list2=新HashMap();
private HashMap list3=新HashMap();
私有静态整数a=0;
私有静态整数q=0;
表hellexample(){
d=新显示器();
s=新外壳(d);
s、 设置大小(250200);
s、 setText(“表外壳示例”);
表格=新表格(s、SWT.边框);
字符串[]标题={“零件名称”、“类别”、“状态”、“优先级”,
“说明”、“理由”};
对于(int i=0;i
您真的应该考虑将
表格查看器
编辑支持
一起使用。通过这样做,您不需要跟踪所选的值,它们将存储在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);
        }
    }
}
import org.eclipse.jface.layout.*;
导入org.eclipse.jface.viewers.*;
导入org.eclipse.swt.*;
导入org.eclipse.swt.layout.*;
导入org.eclipse.swt.widgets.*;
导入java.util.*;
导入java.util.List;
/**
*@作者塞巴斯蒂安·劳巴赫
*/
公共类堆栈溢出
{
公共静态void main(字符串[]args)
{
期末D