Tutorial 12 - Advanced Swing
This tutorial covers more advanced Java Swing topics such as inner class event listeners using inner classes and the MVC architecture. Dropdown menus, popup menus, toolbars, specialized containers, layered panes, split panes, tabbed panes, tables and trees are also included.
Advanced Event Listeners
One difficulty with the basic event listening technique (ie event sharing) previously demonstrated is that it becomes awkward to program for many buttons and widgets. Extended if statements or switch constructs can make code hard to maintain. There are better ways!
Inner (ie nested) class event handlers allow handler routines to be written for specific objects. For example, instead of using the addActionListener(this) method for several buttons you can have each button call its own inner class. Function code can still be mixed into GUI code. See the list of other event listeners.
NOTE: The implements clause is now placed in each inner class header and not in the main class header. Also the listener registration is of a specific Listener_class object, not the generic this.
Anonymous inner classes have no name (hence no name conflict) and are used when only one instance of the class is required. They also do not have constructors. Anonymous inner classes are most often found in event listeners with very simple actions. Beware! Anonymous listeners intermix action code with the GUI object. This is contrary to the separation of worker code from GUI that is a part of clean design. Avoid this construction if possible.
As event classes are interfaces, all methods within that event must be implemented. Adapter classes provide default do-nothing methods which you can chose to override. Here is a short example using the WindowAdapter class to avoid coding all seven window events.
Model-View-Controller Architecture
The Model-View-Controller (MVC) architecture factors function code from the GUI design using a controller module. The controller module ties event listeners in the view module to their actions in the model module. Good programming practice implies private properties with public accessor methods for those needing access from outside their container object. These three object modules can be designed at different times by different programmers. Refer to Using The GUI in the Guidebook. Stuart Davidson of Heriot Watt University has done an excellent job of explaining MVC with a working example.
The model object contains the run-time actions for the application. It often has methods for exit(), run(), about() and help() as these are common to most utilities. The view object constructor should accept a string that incorporates the utility title. The view object also requires methods to build the listeners. buttonActionListeners() includes addActionListener() and a setActionCommand(string) which is used to pass a reference of the pressed button. The controller module uses getActionCommand() to call the correct action method in the model. An example of MVC architecture using the SomeGUI example is:
Menu Bars and ToolBars
Dropdown/pullout menu bars are menus similar to those that appear across the top of most Windows programs. The menu bars are constructed from the JMenuBar class. Menus (aka bar items) are constructed from the JMenu class and menu items from the JMenuItem class. Item separators can either be added to a menu with addSeparator() or inserted with insertSeparator(posn). Menu items can be disabled or grayed out with the setEnabled(false) method and tested with the isEnabled() method. Menu items can have a hotkey shortcut added by using a setMnemonic(char) method. Accelerator keys are added with the setAccelerator() method. The menubar is added to the main frame by using this.setJMenuBar(obj). MyMenu demonstrates many of the features of the menu classes including mnemonics, accelerators keyed to platform, checkbox & radiobutton items and icons.
Dockable toolbars can be constructed from the JToolBar class. JToolBar(SwingConstants.VERTICAL) overrides the default orientation. The add() method adds components which are often icons. Toolbars are always fully exposed. MyToolBar demonstrates features of the toolbar class including action classes.
Popup menus are revealed by some user action and look similar to a dialog box. MyPop demonstrates features of the popup menu classes including specialized listeners.
Specialty Panes
Layered panes allow components such as buttons to be overlapped or layered. A simple example is:
Split panes allow you to place two components side by side in a single pane. It can also split the pane horizontally. A simple example of a split pane is:
Tabbed panes allow a multilayered pane with tabs for the user to access the layer he wants. Each tab contains a single component. A simple example of a tabbed pane is:
Tables and Trees
Tables represent data in a row/column two dimensional grid format. The constructor is JTable(data,headers) where data is a two dimension string and headers is a one dimension string. SimpleTable is a demo. For a more complete tutorial see Sun.com.
Trees allow visualization, traversal and manipulation of hierarchical information (ie. parent-children) much easier. Trees consist of nodes. Common examples of trees are directories, organizational charts, and family trees. In Java the JTree(var) constructor is used to create trees. The variable parameter can be one of Object, TreeNode, TreeModel, Vector, or Hashtable class. ObjectTree is an example which uses a hashtable to hold the actual tree.
Global Hotkeys
Global hotkeys allow a program to be activated from any other program. Java uses its events handler system to monitor the global hotkeys registry after a key sequence has been entered in the registry. Check out JIntellitype (Wintel) and JxGrabKey (Linux) or google 'global hotkeys in java'.
Tutorial Source Code
Obtain source for MyMenu, MyToolBar, MyPop, SimpleTable and ObjectTree here.