SWT应用程序在Windows 7中崩溃

时间:2010-01-06 17:09:36

标签: windows-7 swt

我有一个适用于Windows XP和Windows Vista的Java SWT应用程序。但是当我在Windows 7上运行它时,会发生奇怪的错误,并且它会崩溃。

例如,在我调用Table.removeAll()的方法中,我得到java.lang.ArrayIndexOutOfBoundsException: 0。该表格式为SWT.VIRTUAL

另一个问题是当我在文本框中写东西时(有一个过滤器的ModifyListener) - 在2个字符后,光标在开头(第一个字符之前)移动。

SWT版本是3.5,但我尝试使用eclipse网站(3.6M3)的最新版本,结果是一样的。

有任何已知问题吗?有没有人遇到过这个?


以下是在Windows 7中无法使用的代码段:

import java.util.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class TableCheck {

  Shell shell;

  Button button1, button2, button3;

  UltraTable ut;

  public TableCheck() {
    Display display = new Display();
    shell = new Shell(display);
    shell.setLayout(new GridLayout(4, false));
    Text text = new Text(shell, SWT.SINGLE | SWT.LEAD | SWT.READ_ONLY | SWT.BORDER);
    text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    text.setText("SWT " + SWT.getPlatform() + " " + SWT.getVersion() + "; "
            + System.getProperty("os.name") + " " + System.getProperty("os.version") + " "
            + System.getProperty("os.arch"));

    SelectionAdapter listener = new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            if (e.widget == button1) {
                ut.setContent(Arrays.asList("01", "34", "test", "test2", "123", "1test", "test2"));
            } else if (e.widget == button2) {
                ut.setContent(Arrays.asList("Str1", "Str2", "Str3"));
            } else {
                ut.setContent(Collections.emptyList());
            }
        }
    };
    button1 = new Button(shell, SWT.PUSH);
    button1.setText("Data 1");
    button1.addSelectionListener(listener);
    button2 = new Button(shell, SWT.PUSH);
    button2.setText("Data 2");
    button2.addSelectionListener(listener);
    button3 = new Button(shell, SWT.PUSH);
    button3.setText("Data 3");
    button3.addSelectionListener(listener);

    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 4, 1);
    gd.widthHint = gd.heightHint = 320;
    ut = new UltraTable(shell, SWT.MULTI | SWT.BORDER | SWT.VIRTUAL | SWT.FULL_SELECTION);
    ut.table.setLayoutData(gd);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
  }

  class UltraTable {
    private Table table;

    private static final String DATA_COLLECTION = "<<CC>>", DATA_LISTENER = "<<VL>>";

    public UltraTable(Composite parent, int style) {
        table = new Table(parent, style);
        table.setLinesVisible(true);
        table.setHeaderVisible(true);
        TableColumn tableColumn = new TableColumn(table, SWT.NONE);
        tableColumn.setText("Column");
        tableColumn.setWidth(64);
    }

    void setContent(Collection<?> collection) {
        if ((table.getStyle() & SWT.VIRTUAL) != 0) {
            table.setData(DATA_COLLECTION, collection);
            table.clearAll();
            table.setItemCount(collection.size());
            if (table.getData(DATA_LISTENER) == null) {
                Listener listenerSD = new Listener() {
                    public void handleEvent(Event event) {
                        Collection<?> collectionW = (Collection<?>) event.widget.getData(DATA_COLLECTION);
                        Object object = collectionW.toArray(new Object[collectionW.size()])[event.index];
                        ((TableItem) event.item).setText(0, object.toString());
                    }
                };
                table.setData(DATA_LISTENER, listenerSD);
                table.addListener(SWT.SetData, listenerSD);
            }
        }
    }
  }
  public static void main(String[] args) {
    new TableCheck();
  }
}

我检查的SWT版本是(如shell中的文字所示):

SWT win32 3550; Windows 7 6.1 x86
SWT win32 3617; Windows 7 6.1 x86

2 个答案:

答案 0 :(得分:1)

我在asking the same question之后解决了这个问题。

您必须删除SWT.VIRTUAL或更新为SWT的SVN版本。我在SWT错误页面上发布了a bug report

修改:您的代码段中有一个导致ArrayIndexOutOfBoundsException的错误。您可以通过更改包含listenerSD

的行来解决此问题
public void handleEvent(Event event) {
    Collection<?> collectionW = (Collection<?>) event.widget
            .getData(DATA_COLLECTION);
    if (collectionW.size() > event.index) {
        Object object = collectionW
                .toArray(new Object[collectionW.size()])[event.index];
        ((TableItem) event.item).setText(0, object
                .toString());
    }
}

答案 1 :(得分:0)

在Windows 7上,该表在调用clearAll()时立即更新可见项。所以
table.clearAll();
必须在行之后 table.setItemCount(collection.size());