使用 Java RCP 和 SWT 打开一个新窗口 [英] Opening a new Window with Java RCP and SWT
问题描述
我目前正在开发一个应用程序,为此,我将 Java RCP 与 SWT 结合使用.
我想要的:
我有一个窗口,当我点击一个按钮时,我需要打开一个完整的窗口.该窗口完美运行,如下所示:
当我按下它时,会打开一个新窗口.它看起来像这样:
Window2(是的,中间的图片很重要)>
目前是如何完成的:
窗口 1 是使用 Application.e4xmi 完成的 TrimmedWindow,其中包含一些部件.按钮包含在这些部件之一中.这是它的代码:
@PostConstruct公共无效postConstruct(复合父){Button b = new Button(parent, SWT.BORDER);b.setText("按我!");b.addListener(SWT.Selection, new Listener() {@覆盖公共无效handleEvent(事件事件){parent.getShell().dispose();新游戏(Display.getCurrent());}});}
如您所见,Window 2 是一个名为 Game 的新类.它的构造函数如下:
public Game(Display display) {this.display = 显示;this.shell = new Shell(this.display);this.setData();shell.setText("我工作不正常");shell.setMinimumSize(800, 600);this.buildUI();shell.pack();壳.open();而 (!shell.isDisposed()) {if (!this.display.readAndDispatch()) this.display.sleep();}this.display.dispose();}
有什么问题?:
当我用 Eclipse 启动项目时,一切都很好.我的意思是,真的.我点击,它打开,它加载,耶!但此后的想法是我将项目导出为可执行文件.所以我这样做.这是我的 .exe 文件.让我们开始吧.它不起作用.当我按下按钮时,没有任何反应.甚至没有错误消息,什么都没有.
我找到了一些解决方案,表明问题出在显示器上,因为 RCP 是单线程的.所以我按照说明操作,这是游戏构造函数的另一个版本:
公共游戏(){this.display = Display.getDefault();this.display.asyncExec(new Runnable() {@覆盖公共无效运行(){外壳 = 新外壳(显示);设置数据();shell.setText("我工作得更好,但...");shell.setMinimumSize(800, 600);构建UI();shell.pack();壳.open();而 (!shell.isDisposed()) {if (!display.readAndDispatch()) display.sleep();}display.dispose();}});}
所以出现了新问题:在此之后我无法调用图像(因此我在窗口 2 上显示了图片).因为 Image (SWT) 需要构造一个显示,并且因为如果没有 Runnable 实例,显示就不能很好地工作,所以在此之后我不能使用图像.我需要我的图像在这里(以及此后的其他地方).
在这种情况下我有一条错误消息.它说:
<块引用>org.eclipse.swt.SWTException : 无法执行 runnable(java.lang.IllegalArgumentException : 参数不能为空)
请问大家有什么解决办法吗?
提前致谢.
科斯尼鲁.
如果您在 Application.e4xmi 的Windows and Dialogs"部分添加了一个 Trimmed Window(或只是一个普通的 Window)并打开了To Be Rendered"关闭(保持可见)然后你可以使用:
@InjectEModelService 模型服务;@注入M应用程序;MUIElement window = modelService.find("window id", app);window.setToBeRendered(true);
I'm currently developing an app, and for this, I'm using Java RCP with SWT.
What I want :
I have a window, and when I click on a Button, i need a whole window to be opened. The window works perfectly and looks like this :
When I press it, a new window opens. It looks like this :
Window2 (Yup, the middle pic has its importance)
How it's currently done :
The Window 1 is a TrimmedWindow done with the Application.e4xmi, with some Parts in. The button is included in one of these parts. Here's its code :
@PostConstruct
public void postConstruct(Composite parent) {
Button b = new Button(parent, SWT.BORDER);
b.setText("Press me !");
b.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
parent.getShell().dispose();
new Game(Display.getCurrent());
}
});
}
The Window 2, as you can see, is a new class called Game. Its constructor is as follows :
public Game(Display display) {
this.display = display;
this.shell = new Shell(this.display);
this.setData();
shell.setText("I don't work properly");
shell.setMinimumSize(800, 600);
this.buildUI();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!this.display.readAndDispatch ()) this.display.sleep ();
}
this.display.dispose ();
}
What's the problem ? :
When I launch the project with Eclipse, everything goes WELL. I mean, really. I click, it opens, it loads, yay ! But the idea after this is I export the project as an executable. So I do it. Here's my .exe file. And let's start. And it doesn't work. When I press the button, nothing happens. Not even an error message, nothing.
I've found some solution that says the problem comes from the display, because RCP is single threaded. So I followed the instructions, and here's another version of the Game constructor :
public Game() {
this.display = Display.getDefault();
this.display.asyncExec(new Runnable() {
@Override
public void run() {
shell = new Shell(display);
setData();
shell.setText("I work better but...");
shell.setMinimumSize(800, 600);
buildUI();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
});
}
And so here comes the new problem : I can't call an Image after this (hence the pic I showed on Window 2). Because Image (SWT) requires a display to be constructed, and because display doesn't work well without the Runnable instance, I can't use an image after this. And I need my Image here (and also somewhere else after this).
Edit : I have an error message in that case. It says :
org.eclipse.swt.SWTException : Failed to execute runnable (java.lang.IllegalArgumentException : Argument cannot be null)
Any solutions anyone please ?
Thanks in advance.
Kosnyru.
If you add a Trimmed Window (or just a plain Window) to the 'Windows and Dialogs' section of the Application.e4xmi with 'To Be Rendered' turned off (leave Visible on) you can then show it using:
@Inject
EModelService modelService;
@Inject
MApplication app;
MUIElement window = modelService.find("window id", app);
window.setToBeRendered(true);
这篇关于使用 Java RCP 和 SWT 打开一个新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!