Creating a font formatting tool bar

As we have created functionality to manipulate font settings as described in the previous chapters, now it would be handy to have certain font formatting functions available in a tool bar as it is done in other text processors too.

Creating a font formatting tool bar for that purpose is easily done though a mechanism we already know from SimplyHTML's dynamic menu creation function. Method createToolBar uses the same technique by reading a tool bar definition string and turning it into a tool bar.

Method createToolBar

To create a tool bar a tool bar definition string from the resource file is read having the key for each element in the tool bar delimited by blanks (e.g. fontFamily fontBold fontItalic). The keys are in the order as elements shall appear in the tool bar.

Standard tool bar buttons

The typical case is to add a button on the tool bar for an action defined in the commands Hashtable of class FrmMain. Class JToolBar has a constructor returning a newly created button by passing an action to the constructor. The constructor will do all the connections between the tool bar button and the action automatically.

Combo box elements

Some of the elements in the tool bar however require special handling. FontFamilyPicker and FontSizePicker for instance are subclasses of JComboBox. In their case, createToolbar creates an instance of the component and uses method add of JToolBar.

Toggle buttons

FontComponents other than FontFamilyPicker and FontSizePicker are instances of ToggleFontAction. For ToggleFontActions we need a JToggleButton instead of a JButton in the tool bar and we have to make sure, the JToggleButton is property connected to its ToggleFontAction.

For each JToggleButton in the tool bar a ToggleActionChangedListener associated with the corresponding ToggleFontAction is created. ToggleActionChangedListener implements interface PropertyChangeListener and will always adjust the JToggleButton according to the action's current state. An ActionListener in turn is registered for the JToggleButton invoking the action when the button is pressed.