|
In This Issue
Welcome to the Core Java Technologies Tech Tips for December 2006. Core Java Technologies Tech Tips provide tips and hints for using core Java technologies and APIs provided in the Java 2 Platform, Standard Edition (Java SE).
In this issue provides tips for the following: These tips were developed using Java SE 6. You can download Java SE 6 from the Java SE Downloads page.
When you build applications, you often need to hook multiple components together so that components do things in response to events in other components. When you build custom
components, you may be tempted to build a custom set of listeners as well. This seems like good component etiquette. After all, this is how most of the Swing follows the Java Beans pattern for naming properties. Additionally, nearly all properties of Swing components are bound properties. All This tech tip uses a bitmap tile editor example. The following image shows the unfinished user interface (UI).
The "+" button makes the grid larger. When you press the button, the grid editor panel makes itself bigger. When the grid becomes bigger, a small label should show the grid's
new size. A chain of events must be implemented that links your pressing the "+" button, the grid resizing, and the label change. You could create custom events (like GridSizeChangeEvents or
something equally horrendous), but that's a lot of work for what boils down to a simple "update yourself" request. Instead, consider how this would work with a The editor is a custom public class TileBuilderEditorPanel extends JPanel { ... public void setGridWidth(int gridWidth) { this.oldGridWidth = this.gridWidth; this.gridWidth = gridWidth; rebuildGrid(); } private void rebuildGrid() { int[][] newgrid = new int[gridWidth][gridHeight]; ... this.gridcells = newgrid; repaint(); firePropertyChange("gridWidth", oldGridWidth, gridWidth); }}Back in the main class, you need to update the label whenever the grid size changes. This is simple with an anonymous listener class like this: editor.addPropertyChangeListener("gridWidth", new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent propertyChangeEvent) { viewer.repaint(); grid_size.setText(realeditor.getGridWidth() + " x " + realeditor.getGridHeight()); }});Notice that the The next step is to create an action listener for the "+" button that controls the grid width. For this example, this button is called the "width_plus" button. The
listener should make the grid width one cell bigger whenever the private void width_plusActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here: editor.setGridWidth(realeditor.getGridWidth()+1);}
Many integrated development tools, like NetBeans IDE 5.5, can create and attach event handlers for you. NetBeans
created the When you press the For more information on the
An ActionListener object encapsulates application specific functionality that is usually triggered by a user interface gesture (this isn't always true, javax.swing.Timer also uses ActionListener). For example clicking the back
button in a browser might notify an implementation of ActionListener that is responsible for navigating to the previous page. This might look something like:
private class GoBackActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { browserComponent.show(getLastURL()); }}backButton.addActionListener(new GoBackActionListener());backButton.setIcon(...);backButton.setEnabled(false);If you've written any Swing/AWT applications, you've probably have code like this. It's simple, and it works. Consider what happens if you want to conditionally enable the button based on whether you can go back a page. In this case you need a handle to the The following code shows configuring a menu item and wiring it to a backMenuItem.addActionListener(new GoBackActionListener());
backMenuItem.addActionListener(new GoBackActionListener());backMenuItem.setText(...);backMenuItem.setMnemonic(...);backMenuItem.setDisplayedMnemonicIndex(...);backMenuItem.setAccelerator(...);backMenuItem.setEnabled(false);
The purpose of the private class GoBackAction extends AbstractAction { public GoBackAction() { putValue(Action.NAME, "..."); putValue(Action.LARGE_ICON_KEY, ...); putValue(Action.SMALL_ICON, ...); putValue(Action.MNEMONIC_KEY, ...); putValue(Action.DISPLAYED_MNEMONIC_INDEX_KEY, ...); putValue(Action.ACCELERATOR_KEY, ...); putValue(Action.SHORT_DESCRIPTION, ...); setEnabled(false); } public void actionPerformed(ActionEvent e) { browserComponent.show(getLastURL()); }}Action goAction = new GoBackAction();backButton.setHideActionText(true);backButton.setAction(goAction);backMenuItem.setAction(goAction);
You'll notice a number of differences with this example and the earlier ones. In particular, rather than configuring the text, icon and other properties of the user interface component, the
Those that haven't followed the latest changes in Java SE 6 will notice some new keys and methods. In particular When using backButton.setEnabled(...);backMenuItem.setEnabled(...);
Using the goBackAction.setEnabled(...);Any user interface components attached to the The ability to change the For more information about the | ||||||||||||
|
| ||||||||||||