Java Swing - O Reilly } } Let’s look
Java Swing - O Reilly } } Let’s look at a few of the methods from this example. The first interesting method is called hashDefaultActions(). This method creates a Hashtable that maps action names (which we can get from the DefaultEditorKit constants) to Actions, letting us find actions we want to work with later. The next method, getHashedAction(), lets us get the actions we just hashed by providing the action name. Next, we have a method called makeActionsPretty(). This method adds icons to the actions we’re going to display, and changes the text for these actions, as well. This way, our user interface will display nice names like Cut instead of cut-to-clipboard. Next, we’ve defined a method called updateKeymap(). The purpose of this method is to add some common editor functionality: the ability to skip to the next or previous word by holding down CTRL and pressing the right or left arrow key. Holding down SHIFT at the same time will highlight the word, as well. For more details on what we did with the Keymap and KeyStroke classes, refer to discussion of Keymap in Chapter 22. The important thing here is to note that we used our hashed action table to map these keys to the appropriate actions. Subclasses of this editor can add mappings by overriding this method. In createToolBar(), we get instances of two inner classes, OpenAction and SaveAction, and add them to our JToolBar. We get these actions by calling getOpenAction() and getSaveAction() to allow subclasses to provide different implementations of these actions. We then use getHashedAction() again to get the cut, copy, and paste actions. We’ve chosen not to display the text for the actions in the toolbar, so we call setText(”") on the JButton returned by each add() call. For more details on Swing’s handy new JToolBar class, see Chapter 14. The createMenuBar() method is similar to createToolBar(). We add two additional actions here: exit and select-all. In this method, we don’t strip the text from the menu items, allowing both the icon and the text to be displayed. Finally, we define action classes for exiting the application, and for opening and saving files. These last two actions take advantage of the DefaultEditorKit read() and write() methods, which get called by JTextComponent’s read() and write() methods. For more details on another handy new Swing class we’ve used here, JOptionPane, see Chapter 10. 24.1.13 The StyledEditorKit Class StyledEditorKit extends DefaultEditorKit to provide additional features required for documents that allow styled text. It is the kit used by the JTextPane class. Like DefaultEditorKit, this class defines a number of new TextActions. In this class, all of these action classes are public. We’ll look at each of them at the end of this section. 24.1.13.1 Properties StyledEditorKit defines the properties and default values shown in Table 24.4. The actions property is the set of actions defined for DefaultEditorKit, augmented with actions for setting the font family, font size, font style attributes, and text alignment. The exact actions provided are shown in Table 24.4. In this table, the first column gives the name of the inner class, the next column gives the action name (Action.NAME), and the last column gives the parameter values that - 803
In case you need quality webspace to host and run your web applications, try our personal web hosting services.