|
|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SOA World Conference
Virtualization Conference $200 Savings Expire May 16, 2008... – Register Today!
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON Java Desktop
GUI Design Patterns
Choosing data from long lists
By: Heman Robinson
Digg This!
Long lists of data present a problem for GUI development. This occurs when choosing records from large database tables or recipients from a long list of e-mail addresses, or any time a subset of data must be chosen from a long list or table. Two issues arise when choosing from long lists of data. First, what is the best GUI for long lists so that the users understand how to navigate and manipulate their data? Second, if the list holds more than a few hundred items, performance become an issue. In this article, simple examples show how to optimize usability for this common GUI design pattern. In a later article, more complex examples will show how to optimize performance for long lists. GUI Design Patterns The Add-and-Remove design pattern has many variations. One common enhancement is to provide "Move Up" and "Move Down" buttons to reorder the chosen list. Sometimes the chosen list is displayed as a table (as in Figure 2) to show additional information. This GUI design pattern has several advantages. By presenting the original and chosen lists in a symmetric layout, learning is simplified: once users learn one part of the GUI, the corresponding part works consistently. Also, because objects are moved from one location to another, directional icons and disabling can be used to guide the user. The most important advantage of a standard GUI design like this is that because it is widely known, users will most likely have seen it before and therefore understand it quickly. A standard GUI design pattern is the simplest way to achieve usability. Lay Out the Components To support the Add-and-Remove pattern, the list contents must be mutable; i.e., the lists must dynamically change in response to the button presses. The DefaultListModel provides mutable storage that performs satisfactorily for lists of moderate size. In Listing 1, the ActionListener interface connects the buttons to the lists. The actionPerformed method uses the button text to determine which of the four buttons was pressed. The code in Listing 1 produces the GUI shown in Figure 4. The DefaultListModel is not the default ListModel. By default, JLists use an internal ListModel that is immutable: you can't change its contents. This internal ListModel is efficient when applications only need to view a list because it doesn't carry the overhead of changing list contents. To change list contents, as in the Add-and-Remove pattern, requires a mutable ListModel. Swing provides one in javax.swing.DefaultListModel, and it gives good performance for most applications. For long lists DefaultListModel is not particularly fast, and a later article will show how to improve on its performance. Follow the Guidelines _originalLabel.setBorder( The Guidelines also recommend mnemonics to support keyboard equivalents of mouse actions, as shown in the code below. _add.setMnemonic( 'A' ); Note the difference between Figures 4 and 5. By following the guidelines, the GUI appearance is improved. This example uses the guidelines for the Java ("Metal") Look and Feel. If you target a different platform the guidelines will be different. For example, Windows Guidelines specify different spacings between the components. Whatever the target platform, the reason for following guidelines is to make life easier for the user. When your application looks and works the way the users expect, they have less to learn. Help the Users Learn _add.setIcon( new ImageIcon( These simple icons transform the buttons into a diagram of information flow through the GUI. The icons make the users' possible actions clear before they read the buttons' text, enabling new users to understand the buttons' functions at a glance. The icons in Figure 6 are chosen because they're familiar to most users; similar icons appear on most tape and disk players. "The Java Look and Feel Guidelines, Advanced Topics" illustrate other icons specific to the Java look and feel. Whatever icons are chosen, the program should provide equal margins on the left and right to ensure consistent spacing between the icons and the button text. A slight margin below the icon, as shown in Figure 7, can improve vertical alignment. Prevent User Errors This effective GUI design pattern is trivially easy to code. The ActionListener interface enables the "Add All" and "Remove All" buttons when the lists contain objects, and disables them when the lists are empty. public void actionPerformed( The ListSelectionListener interface enables the "Add" and "Remove" buttons when objects are selected, and disables them when no objects are selected. public void valueChanged( As shown in Listing 1, the ListChooser registers itself as an ActionListener to receive events when buttons are pressed. To receive events when list contents are selected, the ListChooser registers itself as a ListSelectionListener. Also, at the end of the constructor, two dummy events are initiated to set the default enabled states. public ListChooser( The effect of this code is shown in Figure 8. Rather than trying to deal with the user pressing the wrong button, the program uses disabling to prevent the mistake from occurring. Disabling unavailable actions is one of the simplest and most effective GUI design patterns. GUI Variations The GridBagLayout code is omitted from Listing 2 as it is similar to the simpler variation of Listing 1. If "Move Up" and "Move Down" buttons are provided, the GridBagLayout contains seven rows instead of five. Following "The Java Look and Feel Guidelines," the spacing between these optional buttons is six pixels greater than between the "Add" and "Remove" buttons. The ActionListener implementation is slightly different in Listing 2 because of the use of TableModels instead of ListModels. The code in Listing 2 produces the GUI shown in Figure 2. Details of the JTable and TableModel can be found in The JFC Swing Tutorial (by Kathy Walrath and Mary Campione), along with descriptions of the JList and ListModel. The JTable is more complex than the JList, but when displaying long lists they share many of the same performance issues. A future article will discuss these issues and how to optimize performance for this common GUI design pattern. Conclusion Resources
LATEST JAVA STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING JAVA NEWS
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||