我正在嘗試編寫一個方法,該方法將從表中獲取當前選擇,並從選擇中創建一個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();
}
});
由於錯誤提示,似乎你的表已被處置。你把它放在什麼地方?你將不得不提供更多的代碼。 – Baz
添加了更多代碼 – jkteater