devxlogo

Fatten Up Your Java UIs with JGoodies

Fatten Up Your Java UIs with JGoodies

erhaps the single biggest challenge in writing Java applications is dealing with UI issues. Regardless of the application’s power and flexibility, people will always want it to look familiar?to look like a Windows application. With JGoodies you can give them what they want, and a whole lot more besides!

Java has never really been a competitor for premium desktop systems. Its very nature being that of write once, run anywhere leads to a lowest common denominator approach. For example, some operating systems support mice with three buttons, some with two and some with only one. For a Java application to run ‘everywhere,’ it can only support a single mouse button. The extra button functionality that could be available on other systems is lost. There are other user interface widgets and design patterns that are similarly unexploitable, and the ultimate effect is that Java never really penetrated the applications market. Visual Basic reined supreme.

However, when it comes to middleware or serverware, Java provides an amazing proposition. The same code runs your transactions, your analytics, your business apps regardless of the operating platform! Is Solaris too expensive? Replace it with Microsoft. Your software will still work. Is Microsoft too unstable? Replace it with Linux. Your software will still work!

However, the Java-based desktop may be making a comeback. Some of the pending APIs from Sun as well as innovations coming out of the open-source community are redrawing the map and will change the perception of how Java works in the desktop application environment.

For example, BEA’s Weblogic Workshop 8.1, sports a truly beautiful user interface using Java. Oracle and others are also developing Java IDEs with great-looking Java UIs.One of these transformative look-and-feel APIs is JGoodies. JGoodies is freely available and can be used in your own applications.By browsing the JGoodies site at http://www.jgoodies.com, you’ll see examples of user interfaces built using the JGoodies API. One example is shown in Figure 1, below.

Figure 1. Previewing the Goodies: A sample user interface made using the JGoodies API is shown.

Unpacking the JGoodies
To get started using JGoodies, download the Look and Feel and the Forms APIs and then unzip the files to a directory on your hard drive (e.g. c:jgoodieslooks). The result should look like Figure 2.

Figure 2. Unpack the Goodies: This is what your unzipped JGoodies directory structure should look like before you begin developing.

For the purposes of this article you will only need the Look and Feel API, but in order to compile and test the demo application from the Look and Feel API, you will need the JAR files that come with the Forms API as well.

There are two ways to work with the JGoodies Look and Feel API:

  • You can refer directly to JGoodies classes, which is done at compile time. This is the preferred method and generally less prone to errors than indirect references. It requires you to import the precompiled classes to your project, so you should only use it if you know that the classes will be present at runtime.
    Example:
      try {       UIManager.setLookAndFeel(new Plastic3DLookAndFeel());   } catch (Exception e) {}
  • Indirect reference using class names. If you are not sure whether the classes will by available at runtime you can reference them in this manner.
    Example:
    try {      UIManager.setLookAndFeel("com.jgoodies.plaf.windows.ExtWindowsLookAndFeel");   } catch (Exception e) {}

The class must be one of the following:

  • com.jgoodies.plaf.windows.ExtWindowsLookAndFeel
  • com.jgoodies.plaf.plastic.PlasticLookAndFeel
  • com.jgoodies.plaf.plastic.Plastic3DLookAndFeel
  • com.jgoodies.plaf.plastic.PlasticXPLookAndFeel

The Plastic themes use a default color scheme that is appropriate for your operating system. However a method setMyCurrentTheme is provided that allows you to override this. For example:

	PlasticLookAndFeel.setMyCurrentTheme(new DesertBlue());

Here, DesertBlue() is a color theme specified in the com.jgoodies.plaf.plastic.theme package.

‘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

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist