suggestions for declarative GUI programming in Java

前端 未结 14 1047
有刺的猬
有刺的猬 2020-12-07 09:39

I wonder if there are any suggestions for declarative GUI programming in Java. (I abhor visual-based GUI creator/editor software, but am getting a little tired of manually i

相关标签:
14条回答
  • 2020-12-07 10:15

    I strongly recommend MiG Layout - it takes a few days to get used to the syntax, but once you've got it, it works wonders. I used JGoodies Forms for quite awhile, and Karsten's builder concept works well, but it is a bit cryptic... MiG is easier to pick up, and results in wonderfully concise code.

    0 讨论(0)
  • 2020-12-07 10:16

    SDL/Swing does exactly what you need. Its a tiny (283k), unobtrusive, easy to learn declarative Swing framework.

    menus {
        "File" {
            "Open" do="open" // calls "open()" in the controller
            "---"
            "Exit" do="exit"
        }
    }
    

    SDL/Swing is open source but enjoys commercial support. We (Ikayzo.com) developed it over a period of years and have deployed it in production systems for many customers ranging from life science companies to banks.

    0 讨论(0)
  • 2020-12-07 10:16

    I've tried many solutions, such as SWIXML, Javabuilders, MigLayout, Cookswing. I finally found the javaFX and javaFX-Scenebuilder the best an fastest solution, XML-based GUI tool. you'd like the way scenebuilder creates GUI (with drag & drop items!). plus, it uses CSS (Cascading Style Sheets) for the GUI theme. Jsut trust the Oracle, it's the best GUI tool for java applications. take a tour for creating javaFX apps with scenebuilder, here: http://docs.oracle.com/javafx/scenebuilder/1/get_started/prepare-for-tutorial.htm#CEGJBHHA

    0 讨论(0)
  • 2020-12-07 10:17

    If you're willing to step slightly outside plain Java, Groovy's "builder" concept works pretty well with GUIs. Of course you can interop between Groovy and Java fairly easily. See the Swing Builder page for more information.

    0 讨论(0)
  • 2020-12-07 10:18

    As the author of CookSwing, a tool that does what you need, I've given this subject a long hard look before doing the actual implementation. I made a living writing Java Swing GUI applications.

    IMO, if you are going to use any kind of imperative programming languages to describe Java Swing component, you might as well just use Java. Groovy etc only adds complications without much simplification.

    Declarative languages are much better, because even non-programmers can make sense out of it, especially when you need to delegate the task of fine tuning of specific layouts to artists. XML is perfect for declarative languages (over other choices) because of simplicity, readability, and plenty of editors/transformation tools etc available.

    Here are the problems faced in declarative GUI programming, not in any particular order. These issues have been addressed in CookSwing.

    1. Readability and simplicity. (JavaFX is not any simpler than XML. Closing tags of XML helps reading quite a bit, and doesn't add extra typing much since XML editors usually do it for you)
    2. Extensibility. Very important, because custom Swing components will come up for any non-trivial projects.
    3. GUI layouts. Also very important. Being able to handle BorderLayout, GridBagLayout, JGoodies FormsLayout, etc are practically a must.
    4. Simplicity of copy/paste. In the course of the designing the layout, it is necessary to try out different ones. So one need to be able to copy / paste and moving things around. XML is better because the hierarchy of components and layouts are easy to see. JavaFX is somewhat problematic due to multi-line attributes and indentation issues. Having a good editor is a must, and there are plenty of good XML editors.
    5. Templates (i.e. being able to include another layout file) is very useful for consistent look. For example, one might want to have a consistent look of dialogs, button panels, etc.
    6. Interactions with Java code. This is crucial. Some GUI components can only be created with Java code (for whatever the reason). It is thus necessary to be able load these objects. It is also necessarily being able to directly hook up listeners and other Java objects/components within the XML code. Using ids to hook them up later WILL not work well, as it is very tedious.
    7. Internationalization (i18n). Being able to load text / string from a resource bundle rather than hard coded text. This feature can be crucial for some applications.
    8. Localization (l10n). The advantage of declarative programming (particularly with XML) is that you can just switch to a different GUI form for a specific locale and that's it. If you code with Java or any other imperative languages, it is not so easy.
    9. Error check / tolerance. Initial designs often will contain errors here and there. Sometimes the error might be because the corresponding Java code hasn't been designed yet. Or an icon resource is missing. Dealing with errors with imperative coding is extremely tedious. Thus it is desirable to be able to locate the errors, yet at the same time being error tolerant, so the preview of the GUI layout can be made as early as possible.
    10. GUI component replacement. That is, replace textfield which used to have JTextField with some fancier version of components. Replace the meaning of dialog with some fancy UI dialogs (such as JIDE's) instead of JDialog. This feature can save significant amount of efforts. XML itself is also useful due to XSLT and other transformation tools.
    11. Beyond Swing. Because sooner or later you will find many component configurations use object types such as arrays, icons, images, vectors, etc.
    0 讨论(0)
  • 2020-12-07 10:24

    Although it is not declarative and is limited exclusively to layouts, you might want to take a look at DesignGridLayout which allows to programmatically define Swing layouts in a very concise manner (it's open source).

    Main advantages:

    • easy and quick to learn.
    • concise code (1 line of code per row of components in a form) that also enable easy maintenance
    • compile-time checking (which declarative UI can't have)
    • respect of platform look & feel (baseline alignment, gaps between components...) without any hard-coded length value
    0 讨论(0)
提交回复
热议问题