在Swing应用程序中运行SWT组件

时间:2021-01-10 22:24:03

I was wondering if anybody had any experience trying to run a complex SWT UI hosted inside a Swing component.

我想知道是否有人尝试运行Swing组件内托管的复杂SWT UI。

I've managed to get a very simple demo going but if anyone else has tried and failed / succeeded to do this it'd be great to learn from their experiences.

我已经设法得到一个非常简单的演示,但如果其他人尝试过并且失败/成功做到这一点,那么从他们的经验中学习是很好的。

So to reiterate my application is a Swing app I wish to make use of a complex SWT UI component (drag and drop, popup windows and dialogs) - anybody managed this?

所以重申我的应用程序是一个Swing应用程序我希望使用一个复杂的SWT UI组件(拖放,弹出窗口和对话框) - 任何人管理这个?

2 个解决方案

#1


I've used both Swing and SWT, although never in the same application/project before.

我使用过Swing和SWT,虽然以前从未在同一个应用程序/项目中使用过。

As long as your not trying to mix components together within the same windows I don't see any reason you can't have completely separate windows/dialogs each using a specific widget set. However, trying to mix SWT and Swing within a single window would likely never work, or at the very least cause lots of problems since both widget sets rely on completely different means of drawing/layout - Swing's components are all handled directly within Java and are completely separated from the underlying OS, while SWT explicitly relies on the underlying OS to do all (or most) of the GUI rendering. Trying to mix the two would likely cause all kinds of problems.

只要您不想在同一个窗口中混合组件,我就没有看到任何理由您无法使用特定的窗口小部件集完全分离窗口/对话框。但是,尝试在单个窗口中混合使用SWT和Swing可能永远不会起作用,或者至少会导致很多问题,因为两个窗口小部件集依赖于完全不同的绘制/布局方式 - Swing的组件都直接在Java中处理并且是与底层操作系统完全分离,而SWT明确依赖底层操作系统来完成所有(或大部分)GUI渲染。试图将两者混合可能会导致各种问题。

Of course, I'll throw out the usual "this isn't a recommended practice" response as well: Unless you have some really specific goal in mind here, mixing different widget sets in the same application is not a good idea. Swing and SWT have two completely different philosophies and underlying architectures. In fact SWT was born from the fact that IBM/OTI did not like the approach that was taken with Swing and so chose to implement their own widgets. With such contrasting approaches to GUI widgets, you're better off just choosing one and sticking with it, rather than mingling the two together.

当然,我也会抛弃通常的“这不是推荐的做法”的反应:除非你在这里考虑到一些非常具体的目标,否则在同一个应用程序中混合使用不同的小部件集并不是一个好主意。 Swing和SWT有两种完全不同的哲学和底层架构。事实上,SWT源于IBM / OTI不喜欢Swing采用的方法,因此选择实现自己的小部件。使用GUI小部件的这种对比方法,你最好只选择一个并坚持使用它,而不是将两者混合在一起。

#2


Mixing Swing and SWT isn't the best thing to do, but if you have no options, it's not a terribly difficult beast to slay. Just try to enforce some rules/standards throughout your code. eg. keep all dialogs in Swing (don't use a mix of both if there is no reason to), use naming conventions to differentiate between Swing and SWT components, etc.. Make sure you're well aware of the way that Swing and SWT components are handled by the OS/JVM before you start tackling any complex UI issues.

混合Swing和SWT并不是最好的选择,但是如果你没有选择,那么杀死它并不是一个非常困难的野兽。试着在整个代码中强制执行一些规则/标准。例如。保持Swing中的所有对话框(如果没有理由,不要混合使用两者),使用命名约定来区分Swing和SWT组件等。确保你很清楚Swing和SWT的方式在开始解决任何复杂的UI问题之前,组件由OS / JVM处理。

One thing to watch out for is drag and drop between Swing and SWT components. It's a bit tricky, but particularly so if you start throwing in custom data types.

需要注意的一件事是Swing和SWT组件之间的拖放。这有点棘手,但如果你开始投入自定义数据类型,尤其如此。

#1


I've used both Swing and SWT, although never in the same application/project before.

我使用过Swing和SWT,虽然以前从未在同一个应用程序/项目中使用过。

As long as your not trying to mix components together within the same windows I don't see any reason you can't have completely separate windows/dialogs each using a specific widget set. However, trying to mix SWT and Swing within a single window would likely never work, or at the very least cause lots of problems since both widget sets rely on completely different means of drawing/layout - Swing's components are all handled directly within Java and are completely separated from the underlying OS, while SWT explicitly relies on the underlying OS to do all (or most) of the GUI rendering. Trying to mix the two would likely cause all kinds of problems.

只要您不想在同一个窗口中混合组件,我就没有看到任何理由您无法使用特定的窗口小部件集完全分离窗口/对话框。但是,尝试在单个窗口中混合使用SWT和Swing可能永远不会起作用,或者至少会导致很多问题,因为两个窗口小部件集依赖于完全不同的绘制/布局方式 - Swing的组件都直接在Java中处理并且是与底层操作系统完全分离,而SWT明确依赖底层操作系统来完成所有(或大部分)GUI渲染。试图将两者混合可能会导致各种问题。

Of course, I'll throw out the usual "this isn't a recommended practice" response as well: Unless you have some really specific goal in mind here, mixing different widget sets in the same application is not a good idea. Swing and SWT have two completely different philosophies and underlying architectures. In fact SWT was born from the fact that IBM/OTI did not like the approach that was taken with Swing and so chose to implement their own widgets. With such contrasting approaches to GUI widgets, you're better off just choosing one and sticking with it, rather than mingling the two together.

当然,我也会抛弃通常的“这不是推荐的做法”的反应:除非你在这里考虑到一些非常具体的目标,否则在同一个应用程序中混合使用不同的小部件集并不是一个好主意。 Swing和SWT有两种完全不同的哲学和底层架构。事实上,SWT源于IBM / OTI不喜欢Swing采用的方法,因此选择实现自己的小部件。使用GUI小部件的这种对比方法,你最好只选择一个并坚持使用它,而不是将两者混合在一起。

#2


Mixing Swing and SWT isn't the best thing to do, but if you have no options, it's not a terribly difficult beast to slay. Just try to enforce some rules/standards throughout your code. eg. keep all dialogs in Swing (don't use a mix of both if there is no reason to), use naming conventions to differentiate between Swing and SWT components, etc.. Make sure you're well aware of the way that Swing and SWT components are handled by the OS/JVM before you start tackling any complex UI issues.

混合Swing和SWT并不是最好的选择,但是如果你没有选择,那么杀死它并不是一个非常困难的野兽。试着在整个代码中强制执行一些规则/标准。例如。保持Swing中的所有对话框(如果没有理由,不要混合使用两者),使用命名约定来区分Swing和SWT组件等。确保你很清楚Swing和SWT的方式在开始解决任何复杂的UI问题之前,组件由OS / JVM处理。

One thing to watch out for is drag and drop between Swing and SWT components. It's a bit tricky, but particularly so if you start throwing in custom data types.

需要注意的一件事是Swing和SWT组件之间的拖放。这有点棘手,但如果你开始投入自定义数据类型,尤其如此。