SWT - 从表中获取选定的行

时间:2012-09-14 20:22:51

标签: swt

我正在尝试编写一个方法,该方法将从表中获取当前选择并从选择中创建一个ArrayList。

方法:

 public void getPlotterSelection() {
   selPrinters = new ArrayList<PrinterProfile>();
   int[] row = table.getSelectionIndices();
   Arrays.sort(row);
   if (row.length > 0) {
     for(int i = row.length-1; i >= 0; i--){
        PrinterProfile pp = new PrinterProfile(aa.get(i).getPrinterName(), aa.get(i).getProfileName());
        selPrinters.add(pp);
     }
  }
}

这是我得到的错误

错误:16:16:49,503 - TcLogger $ IC_LogListener.logging:? org.eclipse.core.runtime - org.eclipse.ui - 0 - 未处理的事件循环异常 org.eclipse.swt.SWTException:处理小部件         在org.eclipse.swt.SWT.error(SWT.java:3884)         在org.eclipse.swt.SWT.error(SWT.java:3799)         在org.eclipse.swt.SWT.error(SWT.java:3770)         在org.eclipse.swt.widgets.Widget.error(Widget.java:463)         在org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:336)         在org.eclipse.swt.widgets.Table.getSelectionIndices(Table.java:2536)         等.......

问题在于这行代码

int[] row = table.getSelectionIndices();

再次..

我试图让用户在表格中选择行并将它们放在一个arraylist中。

修改添加更多代码

 //////////////////////////////////////////////////////////////////////////
 //                       createDialogArea()                             //
 //////////////////////////////////////////////////////////////////////////
 protected Control createDialogArea(Composite parent) {
  final Composite area = new Composite(parent, SWT.NONE);
  final GridLayout gridLayout = new GridLayout();
  gridLayout.marginWidth = 15;
  gridLayout.marginHeight = 10;
  area.setLayout(gridLayout);

  GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
  area.setLayoutData(gridData);
  checkingArray();
  createCopyNumber(area);
  createPlotterTable(area);
  return area;
}

public void checkingArray() {
   aa = abd.getPrintersArray();
}

//////////////////////////////////////////////////////////////////////////
//                     createPlotterTable()                             //
//////////////////////////////////////////////////////////////////////////
private void createPlotterTable(Composite parent) {
  Composite composite = new Composite(parent, SWT.BORDER); 
  GridLayout gridLayout = new GridLayout(1, false);
  composite.setLayout(gridLayout);
  GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
  composite.setLayoutData(gridData);

  //gridData = new GridData(SWT.FILL, SWT.FILL, true, true ); 
  table = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
  table.setHeaderVisible(true);
  table.setLinesVisible(true);

  TableColumn[] column = new TableColumn[2];
  column[0] = new TableColumn(table, SWT.FILL);
  column[0].setText("Printer Name");
  column[0].setWidth(200);

  column[1] = new TableColumn(table, SWT.FILL);
  column[1].setText("Profile Name");
  column[1].setWidth(200);

  gridData = new GridData();
  gridData.verticalAlignment = GridData.FILL;
  gridData.horizontalAlignment = GridData.FILL;
  gridData.grabExcessHorizontalSpace = true;
  gridData.grabExcessVerticalSpace = true;
  table.setLayoutData(gridData);

  fillTable(table);
  table.setRedraw(true);
} 

private void fillTable(Table table) {
  table.setRedraw(false);

  for(Iterator iterator = abd.getPrintersArray().iterator();iterator.hasNext();){
     PrinterProfile printer = (PrinterProfile) iterator.next();
     TableItem item = new TableItem(table, SWT.FILL);
     int c = 0;
     item.setText(c++, printer.getPrinterName());
     item.setText(c++, printer.getProfileName());
  }
table.setRedraw(true);
} 

 public void getPlotterSelection() {

   selPrinters = new ArrayList<PrinterProfile>();

   int[] row = table.getSelectionIndices();

   Arrays.sort(row);
   if (row.length > 0) {
     for(int i = row.length-1; i >= 0; i--){
        PrinterProfile pp = new PrinterProfile(aa.get(i).getPrinterName(), aa.get(i).getProfileName());
        selPrinters.add(pp);
     }
   }
}  

这是调用方法

的按钮
 Button okButton = createButton(parent, IDialogConstants.OK_ID, "OK", true);
   okButton.setEnabled(true);
   okButton.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
         getPlotterSelection();
      }
   });

1 个答案:

答案 0 :(得分:1)

我建议您使用Table viewer而不是Table。它让您的生活更轻松。我看到你在对话框中显示表格。我猜你的对话框在你到达getPlotterSelection()方法之前单击OK时会被关闭/处理。