2016-09-03 4 views
-1

メソッドにリスナーを実装するのに苦労しています。Javaのクラス内のメソッドにリスナーを追加する

テーブルを作成する方法fillTableRoWData()があります。このメソッドは、2つのif条件の中で同時に呼び出されます。

私はすべての値をhahMapに保存しました。

問題は、コンボボックスの値を更新するときです。第2の条件の値のみが更新されます。

条件が更新された場合にメソッドに通知し、関連するhashMap位置にデータを追加するリスナーを追加します。私はどのTableColoumnにデータを入力すると

はまた、データはHashMapのあなたは本当にTableViewerEditingSupportとを使用してになります

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

} 
+1

説明のない票は役に立たない。 また、ヘルプやアドバイスを求めて求めている新しいユーザーを阻止する可能性もあります。 新しいユーザーの投票の問題はできるだけ避けるべきだと思います。 私は反対にすることをお勧めします:ダウン投票なしの説明。 – c0der

+1

これはかなり長いコードで、問題の原因がわかりません。問題を示すために[MCVE]を投稿し、あなたの手助けをより簡単にすることを検討してください。 – c0der

+0

私はコードを編集しました。私が望むのは、comboBoxの値をhashmap値で更新するよりも値を変更する場合です。@ c0der –

答えて

1

に更新する必要があります。これにより、選択した値を追跡する必要はなく、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); 
     } 
    } 
} 

項目を選択すると、それは現在の選択を印刷します:ここでは、二つの値から選択することができます1つのコンボボックス列を持つ表を示して非常に単純な例です。

関連する問題