TableViewer使用java.lang.ClassCastException添加项false:java.lang.String无法强制转换为[Ljava.lang.String;

时间:2017-12-25 09:01:49

标签: java eclipse swt jface

当用户按下“添加”按钮时,我想在表格中添加一些项目。但是我得到了一个java.lang.ClassCastException:当我写一个添加项的测试时,java.lang.String不能转换为[Ljava.lang.String。我需要一些帮助。谢谢!我的代码显示如下:

Button btnAddConstraint = new Button(compositeContent, SWT.NONE);
fd_btnEditConstraint.top = new FormAttachment(btnAddConstraint, 0, 
SWT.TOP);
fd_btnEditConstraint.right = new FormAttachment(btnAddConstraint, -63);
FormData fd_btnAddConstraint = new FormData();
fd_btnAddConstraint.bottom = new FormAttachment(100);
fd_btnAddConstraint.left = new FormAttachment(0, 500);
btnAddConstraint.setLayoutData(fd_btnAddConstraint);
btnAddConstraint.setText("Add Constraint");
btnAddConstraint.addSelectionListener(new SelectionAdapter() {
@Override 
public void widgetSelected(SelectionEvent e) {
    String[] str = {"a","n"};
    tableViewer.add(str);

        }
    });

Button btnDeleteConstraint = new Button(compositeContent, SWT.NONE);
fd_btnAddConstraint.right = new FormAttachment(btnDeleteConstraint, -60);
FormData fd_btnDeleteConstraint = new FormData();
fd_btnDeleteConstraint.left = new FormAttachment(0, 710);
fd_btnDeleteConstraint.right = new FormAttachment(100, -2);
fd_btnDeleteConstraint.bottom = new FormAttachment(100);
btnDeleteConstraint.setLayoutData(fd_btnDeleteConstraint);
btnDeleteConstraint.setText("Delete Constraint");

btnDeleteConstraint.addSelectionListener(new SelectionAdapter() {
        @Override
public void widgetSelected(SelectionEvent e) {
ISelection selection = tableViewer.getSelection();
logger.debug("datatype selected");
if (selection != null || selection instanceof 
IStructuredSelection) {
IStructuredSelection sel = (IStructuredSelection) selection;
Iterator iterator = sel.iterator();
while(iterator.hasNext()) {
    Object obj = iterator.next();
    tableViewer.remove(obj);
                }

                //tableViewer.refresh();
            }

        }
    });


tableViewer = new TableViewer(compositeContent, SWT.MULTI | SWT.H_SCROLL
            | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
Table table = tableViewer.getTable();
FormData fd_table = new FormData();
fd_table.bottom = new FormAttachment(btnEditConstraint, -6);
fd_table.top = new FormAttachment(0);
fd_table.left = new FormAttachment(0);
fd_table.right = new FormAttachment(100);
table.setLayoutData(fd_table);
table.setHeaderVisible(true);
table.setLinesVisible(true);

TableViewerColumn tcolOracle = new TableViewerColumn(tableViewer, 
    SWT.NONE);
TableColumn tcOracle = tcolOracle.getColumn();
tcOracle.setText("oracle");
tcOracle.setWidth(300);
tcolOracle.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
    String[] t = (String[]) element;
    return t[0];
        }
    });

TableViewerColumn tcolHighgo = new TableViewerColumn(tableViewer, 
    SWT.NONE);
TableColumn tcHighgo = tcolHighgo.getColumn();
tcHighgo.setText("hgdb");
tcHighgo.setWidth(300);
tcolHighgo.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
    String[] t = (String[]) element;
    return t[1];
        }
    });

Exception看起来像那样:

java.lang.ClassCastException: java.util.ArrayList cannot be cast to [Ljava.lang.String;
at com.highgo.admin.migrator.ui.DatatypePage$3.getText(DatatypePage.java:134)
at org.eclipse.jface.viewers.ColumnLabelProvider.update(ColumnLabelProvider.java:34)
at org.eclipse.jface.viewers.ViewerColumn.refresh(ViewerColumn.java:141)
at org.eclipse.jface.viewers.AbstractTableViewer.doUpdateItem(AbstractTableViewer.java:387)
at org.eclipse.jface.viewers.StructuredViewer$UpdateItemSafeRunnable.run(StructuredViewer.java:475)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.ui.internal.JFaceUtil.lambda$0(JFaceUtil.java:44)
at org.eclipse.ui.internal.JFaceUtil$$Lambda$8/1642679791.run(Unknown Source)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:173)
at org.eclipse.jface.viewers.StructuredViewer.updateItem(StructuredViewer.java:2159)
at org.eclipse.jface.viewers.AbstractTableViewer.createItem(AbstractTableViewer.java:279)
at org.eclipse.jface.viewers.AbstractTableViewer.add(AbstractTableViewer.java:265)
at org.eclipse.jface.viewers.AbstractTableViewer.add(AbstractTableViewer.java:317)
at com.highgo.admin.migrator.ui.DatatypePage$1.widgetSelected(DatatypePage.java:80)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:249)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:86)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4428)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4238)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3817)
at org.eclipse.jface.window.Window.runEventLoop(Window.java:818)
at org.eclipse.jface.window.Window.open(Window.java:794)
at com.highgo.admin.migrator.handler.OpenMigratorHandler.execute(OpenMigratorHandler.java:30)
at com.highgo.admin.NavigatorActionExecuteAdvanceTool.executeTool(NavigatorActionExecuteAdvanceTool.java:46)
at com.highgo.admin.NavigatorActionExecuteAdvanceTool.run(NavigatorActionExecuteAdvanceTool.java:37)
at org.jkiss.dbeaver.ui.ActionUtils$1.run(ActionUtils.java:267)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:473)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:565)
at org.eclipse.jface.action.ActionContributionItem.lambda$4(ActionContributionItem.java:397)
at org.eclipse.jface.action.ActionContributionItem$$Lambda$148/2021466012.handleEvent(Unknown Source)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:86)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4428)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4238)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3817)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1155)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1044)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:153)
at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:680)
at org.eclipse.ui.internal.Workbench$$Lambda$14/1659286984.run(Unknown Source)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:594)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:148)
at org.jkiss.dbeaver.core.application.DBeaverApplication.start(DBeaverApplication.java:160)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:388)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:243)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:653)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:590)
at org.eclipse.equinox.launcher.Main.run(Main.java:1499)
at org.eclipse.equinox.launcher.Main.main(Main.java:1472)

0 个答案:

没有答案