Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Fatten Up Your Java UIs with JGoodies : Page 2

Think you can get your end users to exclaim: "My! What beautiful native-looking Windows apps you build in Java"? A basketful of JGoodies is all you need.


advertisement
'Tiny' Example
Included in the download with the JGoodies Look and Feel API is a simple example called 'tiny.'

This application, upon initialization calls the configureUI() method from the UIManger. This method simply sets up the fonts and the window style that the application will use.

private void configureUI() { UIManager.put(Options.USE_SYSTEM_FONTS_APP_KEY, Boolean.TRUE); Options.setGlobalFontSizeHints(FontSizeHints.MIXED); Options.setDefaultIconSize(new Dimension(18, 18)); String lafName = LookUtils.isWindowsXP() ? Options.getCrossPlatformLookAndFeelClassName() : Options.getSystemLookAndFeelClassName(); try { UIManager.setLookAndFeel(lafName); } catch (Exception e) { System.err.println("Can't set look & feel:" + e); } }

The first three lines of this code force the system to use System Fonts and sizes the default icons at 18 pixels by 18 pixels. After that the code is set up to use the appropriate look and feel. The LookUtils class determines if your operating system is Windows XP or not. If it is, then it pulls the name of the default theme for a cross-platform look and feel. Otherwise it pulls the standard System look-and-feel theme. It then sets the look and feel on the UIManager to the appropriate version.



Figure 3 shows the application on Windows XP.

Figure 3. Cross Platform: Here is the look and feel of a simple application specified as cross-platform, rendered in Windows XP.

A simple override—to specify the Windows look and feel as outlined above will produce an application that looks like that in Figure 4. On the surface it appears very similar to Figure 3, but upon closer inspection there are a number of differences. The menu is a traditional Windows 'soft button' type menu, the folders in the treeview are oriented differently, and the scrollbar looks different. On more complex dialogs, the differences between the widgets will be more apparent.

Figure 4. Windows-esque: Here is the Tiny application with a Windows look and feel.
Building a simple application with JGoodies
Included in the download for this article (see left column), are two Java source files, Article1Before.Java and Article1After.Java. These were developed using the excellent freeware Swing GUI editor called 'Java GUI Builder' from Davdstudio.com. Article1Before is the unimproved application GUI, as designed. Article1After is the same GUI but with the code to enhance it with JGoodies added. You will see the difference between the two at run time. Figures 5 and 6 show the GUI before and after it has had the enhanced looks set up.

The difference between the two is obvious: Figure 6 looks more like a traditional Windows XP application but simplified. The example uses very simple controls like a text box and button. With others such as tabs and menus, the difference is more striking. However for the sake of simplicity, you will deal with just the simple case for the purposes of this demonstration.

The fundamental difference in how the GUI is handled is in the order that the GUI is defined. In Art1Before.Java you will notice that the GUI components are declared in the constructor of the application. In this case if you add the calls to set up the UIManager as described earlier in this article, nothing will happen. The UIManager will only affect those components declared after it is called.


Figure 5. No Goodies: This is a simple GUI app before JGoodies is enabled.
 
Figure 6. With Goodies: Here is the same GUI app from Figure 5, but with JGoodies code added.

In Art1After.java you will see that the GUI declaration code has been moved to a member function, which is called by the main function. Before this is done however, the main function calls the configureUI helper function, which sets up JGoodies as we saw earlier in the article.

The main function, which configures the window to be drawn with JGoodies, is shown below. The configureUI function simply initializes the JGoodies engine.

art1after window = new art1after(); // Add this for JGoodies window.configureUI(); window.buildGUI(); // window.setTitle("art1"); window.pack(); window.show();

You may also tweak your UI to your exact specifications by overriding the default visual properties of each control using the putClientProperty method on any GUI object. This takes as a parameter an Options object that describes the various settings that you can use. For example, the code below shows how to alter a menu bar using the putClientProperty:

JMenuBar menuBar = new JMenuBar(); menuBar.putClientProperty(Options.HEADER_STYLE_KEY, HeaderStyle.SINGLE); menuBar.putClientProperty(PlasticLookAndFeel.IS_3D_KEY, Boolean.FALSE);

Figure 7. The Simple Looks demo: This packaged demo is only the beginning of what you can do with Java on the desktop using JGoodies.
In this case the menu bar is instructed to be a single header style, but not to be three-dimensional. Check the JGoodies documentation for all the options.

Great Potential
The Simple Looks demo that comes with JGoodies shows the extent of UI complexity that is possible with the API. Figure 7 is a screen shot of the demo app.

The performance and cleanliness of the GUI is apparent. The menus are OfficeXP style with icons and submenus. And this is all developed using Swing controls that are overridden using JGoodies.

If you are looking to develop desktop Java applications, and if your requirements are to make them as close to native windows UI look and feel as possible, JGoodies is an excellent platform choice



Laurence Moroney is a freelance enterprise architect who specializes in designing and implementing service-oriented applications and environments using .NET, J2EE, or (preferably) both. He has authored books on .NET and Web services security, and more than 30 professional articles. A former Wall Street architect, and security analyst, he also dabbles in journalism, reporting for professional sports. You can find his blog at http://www.philotic.com/blog.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date