开发者

Is there a nice way to abstract the GUI so SWT or Swing can be used?

开发者 https://www.devze.com 2022-12-15 10:10 出处:网络
I\'m writing an application that has an SWT GUI currently, but would like the end-users to be able to choose between SWT and Swing. I\'ve experimented with abstracting the GUI details before at differ

I'm writing an application that has an SWT GUI currently, but would like the end-users to be able to choose between SWT and Swing. I've experimented with abstracting the GUI details before at different layers in the program, but have never been really satisfied with the results. Is there an agreed-开发者_运维知识库upon or nice way to do this?


Unfortunately I don't believe there's a toolkit-agnostic API or similar.

So perhaps it's worth looking at the model-view-controller pattern. You need to abstract as much functionality away from the GUI into the controller such that the GUI components are thin and are dedicated to the particular windowing toolkit that you've chosen.

This will allow you to slot in a Swing view in place of a GWT view (or vice versa) with the minimum of duplicated code.

Note also that this makes testing a lot easier, since as much as possible has been folded into the controller or model.


If your only goal is to let users choose between SWT or Swing drawing their UI, then SWTSwing would have been an option. You could code in SWT and choose the SWT implementation jar during startup and adapt your classpath accordingly.

SWTSwing is an implementation of the SWT API using Swing. It does the same thing that every native SWT implementation does: it provides the bridge to the underlying GUI API.

Why do I write "would have been"? Unfortunately the project seems dead, stuck in an unfinished implementation of SWT-3.2, even though lots of the work seems to be done as you can see in the webstart demo. The sister project EoS (Eclipse on Swing) had even a running prototype. So I cannot really recommend using it, even though I like the idea.


Is there an agreed-upon or nice way to abstract away GUI-specific code?

The answer, in short, is no.

The trouble using any particular GUI library is that each GUI comes with a set of basic design tenets that influence every use of the library. Unless one comes across two such libraries that agree in every particular on these design tenets, there's no simple substitution of one GUI for another.

There are a number of libraries that attempt to impose their design upon a bunch of disparate GUIs underneath each with their own design, but these libraries require ferocious amounts of programming. As well, the attempt to force one set of design paradigms into another is not usually completely successful.

Examples of these libraries are QT, wxWidgets, and, of course, Java's base AWT.

In the end, you pretty much have to accept that you are going to choose a library and get stuck with it.


Yes, using SwingWT, an implementation of Swing using SWT as the backend.

Many people prefer the higher-level Swing APIs, but the SWT uses native widgets. This library apparently gives you both.

It also lets Swing applications run unmodified using an SWT backend (since your program is already written using SWT that doesn't help you, but it will others). According to the description, the implementation works using a custom class loader that replaces Swing calls with calls to SwingWT. It should be possible to let the user select whether to do this.

The author doesn't seem to be actively developing it, but he's still reviewing/applying patches received from others (most recently, earlier in 2012).

0

精彩评论

暂无评论...
验证码 换一张
取 消