2012-09-14 54 views
1

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

方法:

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:49503 - TcLogger $ IC_LogListener.logging:? org.eclipse.core.runtime - org.eclipse.ui - 0 - 未处理的事件循环异常 org.eclipse.swt.SWTException:Widget在org.eclipse.swt.SWT.error处处理为 (SWT.java:3884 )org.eclipse.swt.SWT.error上的 (SWT.java:3799) at org.eclipse.swt.SWT.error(SWT.java:3770) at org.eclipse.swt.widgets.Widget.error (Widget.java:463) at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:336) at org.eclipse.swt.widgets.Table.getSelectionIndices(Table.java:2536) 等。 ......

问题是用这行代码

int[] row = table.getSelectionIndices(); 

再次..

我试图让用户选择表中的行,并把它们放在一个数组列表。

编辑添加更多的代码

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

由于错误提示,似乎你的表已被处置。你把它放在什么地方?你将不得不提供更多的代码。 – Baz

+0

添加了更多代码 – jkteater

回答

1

我建议你到用户表查看器,而不是表的按钮。它使你的生活更轻松。我看到你在对话框中显示表格。我猜你的对话框在你点击确定之前就已经关闭/处理掉了,然后它才得到getPlotterSelection()方法。

+0

谢谢Baz - 要使用Table Viewer – jkteater

+0

@jkteater我会感谢sambi,因为这是他/她的答案... – Baz

+0

opps - 我现在看到了 – jkteater