0

我在使用 时遇到了一个问题,org.eclipse.ui.forms.widgets.Section如果我向其中添加子项并通过启用它们或在它们上设置文本来修改子项,我会遇到异常。我写了一个小片段来轻松重现以下问题:

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.forms.widgets.Section;

public class SectionTest {
    /**
     * @param args
     */
    public static void main(String[] args) {
        final Display display = new Display ();
        final Shell shell = new Shell (display);
        shell.setLayout(new GridLayout());

        Button b = new Button(shell, SWT.PUSH);
        b.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                addSection(shell);
            }
        });
        b.setText("Add");

        addSection(shell);

        shell.setSize(300, 300);
        shell.open();
        while (!shell.isDisposed()) {
            try {
                if (!display.readAndDispatch())
                    display.sleep();
            }
            catch (Exception e) {
                e.printStackTrace();
                MessageDialog.openError(shell, "Error", e.getMessage());
            }
        }
        display.dispose();
    }

    private static void addSection(Composite parent) {
        Section section = new Section(parent, Section.TITLE_BAR | Section.TREE_NODE | Section.CLIENT_INDENT);
        section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        section.setText("Hello");
        //section.setSize(1, 1);

        // Add the check button for enabling/disabling this group
        Button enableControl = new Button(section, SWT.CHECK);
        enableControl.setText("Enable");
        section.setTextClient(enableControl);

        Composite client = new Composite(section, SWT.NONE);
        section.setClient(client);
        client.setLayout(new GridLayout());

        Button b = new Button(client, SWT.PUSH);
        b.setText("one");

        b = new Button(client, SWT.PUSH);
        b.setText("two");
        parent.layout();
    }
}

如果您运行代码,将出现一个带有添加按钮的对话框。单击添加按钮将产生异常。

异常文本:

java.lang.IllegalArgumentException: Argument not valid
    at org.eclipse.swt.SWT.error(SWT.java:4342)
    at org.eclipse.swt.SWT.error(SWT.java:4276)
    at org.eclipse.swt.SWT.error(SWT.java:4247)
    at org.eclipse.swt.graphics.Image.init(Image.java:1558)
    at org.eclipse.swt.graphics.Image.<init>(Image.java:181)
    at org.eclipse.ui.forms.widgets.Section.onPaint(Section.java:344)
    at org.eclipse.ui.forms.widgets.ExpandableComposite$1.paintControl(ExpandableComposite.java:560)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:230)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1077)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1062)
    at org.eclipse.swt.widgets.Composite.WM_PRINTCLIENT(Composite.java:1639)
    at org.eclipse.swt.widgets.Control.windowProc(Control.java:4590)
    at org.eclipse.swt.widgets.Canvas.windowProc(Canvas.java:341)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:4976)
    at org.eclipse.swt.internal.win32.OS.CallWindowProcW(Native Method)
    at org.eclipse.swt.internal.win32.OS.CallWindowProc(OS.java:2440)
    at org.eclipse.swt.widgets.Button.callWindowProc(Button.java:343)
    at org.eclipse.swt.widgets.Control.windowProc(Control.java:4623)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:4976)
    at org.eclipse.swt.internal.win32.OS.SetWindowTextW(Native Method)
    at org.eclipse.swt.internal.win32.OS.SetWindowText(OS.java:3461)
    at org.eclipse.swt.widgets.Button._setText(Button.java:300)
    at org.eclipse.swt.widgets.Button.setText(Button.java:1099)
    at org.eclipse.swt.snippets.SectionTest.addSection(SectionTest.java:58)
    at org.eclipse.swt.snippets.SectionTest.access$0(SectionTest.java:50)
    at org.eclipse.swt.snippets.SectionTest$1.widgetSelected(SectionTest.java:28)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4169)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3758)
    at org.eclipse.swt.snippets.SectionTest.main(SectionTest.java:39)

Windows 7 64 位机器上的 32 位 JRE(和 eclipse)会出现此问题。在 Windows XP(32 位)上无法重现同样的问题。

我的问题是,我做错了什么导致了这个异常。如果您注意到我在外壳打开之前使用相同的方法添加了一个部分并且它运行没有任何问题,但是一旦外壳打开并且可见,调用相同的addSection(Composite)方法会导致异常。

注意:取消注释行 'section.setSize(1, 1);' inaddSection(Composite)将“隐藏”问题,但这似乎不是正确的做法。使用部分的正确方法是什么?


编辑:发现已经在 ecilpse 中报告了一个问题,https: //bugs.eclipse.org/bugs/show_bug.cgi?id=309443 ,这似乎与这个问题的发现相同。

4

0 回答 0