简体   繁体   中英

Best way to build Java Swing GUI?

Till now, I had been using Netbeans Swing GUI generation feature to build GUI for my java applications.

Now I have started using Eclipse for my projects and have dropped Netbeans. So apart from coding manually all the code for GUI, what are the other ways with which I can build GUI quickly and in a much better way.

I have been using Visual Editor and Windows Builder plugin for eclipse IDE since last 2 years. I found Windows Builder works perfect! It provides very clean and highly readable code for you. So i prefer to use Windows Builder, so that you can do everything what ever you found in Microsofts Visual Studio features " drag and drop and all ".

You can keep coding it manually but using GroovyBuilders ( link ) that are enough faster than using plain java but you will need to embed Groovy lib inside your application..

otherwise there exists a layout manager, called MIG Layout , that works quite well (it's much more practical that normal gui building)..

I also used a tool called JVider to build some simple applications, but using a visual approach you get always the same nasty thing: keeping interface and backend synchronized, considering also the "bindings" between visual objects and variables name..

I ran into a similar situation, but discovered that NetBeans actually has a nifty 'import eclipse project' functionality. So, I ended up doing the visual GUI design work in NetBeans, but used Eclipse for coding, debugging and profiling. Netbeans adds some files to your project, and you may want to tweak the auto-code generation templates in NetBeans, but this is a solution that works for me. Of course, Eclipse also ships with its own visual GUI builder, but I prefer NetBeans for visual GUI design and development.

Importing Eclipse projects in NetBeans.

The Eclipse Visual Editor project

The Eclipse Visual Editor project mentioned by Luhar is quite nice, it can use any bean style swing or awt component and supports editing already existing classes since it relies on reflection instead of separate meta-data.
Also there are no limitiations as how you can manipulate the generated code as VEP will do its best to keep it when modifying the source file.
I would recommend a rather highend system if you want to use it for more complex components as it is quite resource hungry.
Nice things about VEP:

  • Supports Swing/AWT/SWT
  • Uses the source-code and reflection no additional data required
  • Can be used with already existing components (see above)
  • Can edit code directly or in preview window
  • Generates clean looking code without breaking existing code

Not so nice things:

  • Slow, don't forget to pause the preview function when editing source code directly, it will try to refresh almost constantly.
  • Don't use the Preview window while you have compilation errors in your code, any changes made to the preview may cause a messed up edit in the source. You have to clean up any compilation errors before using it again.

You may also consider Seesaw , which requires embedding Clojure .

You're GUI code would then look something like:

(defn -main [& args]
  (invoke-later
    (-> (frame :title "Hello",
               :content "Hi there",
               :on-close :exit)
        pack!
        show!)))

Where "Hi there" is automatically "converted" to a JLabel, but instead you could put any Swing component for :content .

This more complex example from my seesaw-buttons sample project:

(ns seesaw-buttons.core
  (:use seesaw.core))

(defn -main [& args]
  (invoke-later
    (let [open-action (action
                    :handler (fn [e] (alert "I should open a new something."))
                    :name "Open ..."
                    :key  "menu O"
                    :tip  "Open a new something something.")
      exit-action (action
                    :handler (fn [e] (.dispose (to-frame e)))
                    :name "Exit"
                    :tip  "Close this window")]
      (-> (frame :title "Hello",
                 :content (border-panel
                            :north (toolbar :items [open-action exit-action])
                            :center "More content here..."),
                 :on-close :exit)
          pack!
          show!))))

The advantage of Seesaw and Clojure is the elimination of a lot of boilerplate code and Seesaw has added some functionality and conventions that speed up development.

The experience I've made is that Netbeans is pretty much as good as it gets for building Swing GUIs if you want to be able to just click your application together. Just like you however, I enjoy coding in eclipse a lot more than in Netbeans (personal preference, Netbeans is still great editor imo). For a while I ended up creating my GUIs in Netbeans and then just importing the projects into eclipse.

This works alright, but in the end I think you'll come to the same conclusion I did, that it's just best to learn the layout managers and code the GUI by hand. Maybe take a look at the ones provided by JGoodies as well if the ones provided by Sun just don't do it for you.

Another approach is to use some XML library, like SwixML . To work with it requires some code changes but separates your UI from your code quite well. On the other hand, it is only a wrapper around the swing classes. This is IMHO both an advantage and a disadvantage. It is also a quite nice library for rapid prototyping, because you can (usually) try a dialog out until it is sufficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM