我遇到了使用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位)上无法重现。
我的问题是,我做错了导致此异常。如果您注意到我在shell打开之前使用相同的方法添加了一个部分并且它运行没有任何问题,但是一旦shell打开并且可见,调用相同的addSection(Composite)
方法会导致异常。
注意:取消注释'section.setSize(1,1);'行在
addSection(Composite)
中将“隐藏”问题,但这似乎不是正确的事情。使用Sections的正确方法是什么?
编辑:在ecilpse https://bugs.eclipse.org/bugs/show_bug.cgi?id=309443中发现了一个已经报告过的问题,这个问题似乎与此问题相同。