I l@ve RuBoard |
![]() ![]() |
20.8 The DefaultFormatterFactory ClassDefaultFormatterFactory is Swing's only concrete implementation of AbstractFormatterFactory. It holds one or more formatters and decides which one to give to the field depending on whether the field has focus and whether the field's value is null. 20.8.1 PropertiesTable 20-7 shows the formatter properties defined by DefaultFormatterFactory.
defaultFormatter is used if one of the other formatter properties is null. It is common for defaultFormatter to be the only non-null formatter property, in which case the field uses the defaultFormatter exclusively. displayFormatter is intended for use when the field does not have focus. It may be null, in which case defaultFormatter is used instead. editFormatter is intended for use when the field has focus. It may be null, in which case defaultFormatter is used instead. nullFormatter is intended for use when the field's content is null. It may be null, in which case displayFormatter or editFormatter (depending on whether the field has focus) is used instead. (If displayFormatter/editFormatter is also null, defaultFormatter is used.) 20.8.2 Constructors
20.8.3 Public Method
20.8.4 ExampleThis program demonstrates two simple ways to use DefaultFormatterFactory. The first creates a factory that provides different formatters depending on whether the field has focus. The second changes a field's format "midstream" by completely replacing its factory. Figure 20-4 shows what the example looks like. Try tabbing between the fields and watch how the format of the top field changes (as the bottom's does when you click on the change format button). // FactoryDemo.java // import javax.swing.*; import javax.swing.text.*; import java.awt.*; import java.awt.event.*; import javax.swing.border.TitledBorder; import java.text.ParseException; public class FactoryDemo { public static JPanel demo1( ){ // Demo 1: Field with different formats with and without focus JPanel pan = new JPanel(new BorderLayout( )); pan.setBorder(new TitledBorder("Demo 1: format toggles with focus")); MaskFormatter withFocus = null, withoutFocus = null; try { withFocus = new MaskFormatter("LLLL"); withoutFocus = new MaskFormatter("UUUU"); } catch (ParseException pe) { } DefaultFormatterFactory factory = new DefaultFormatterFactory(withoutFocus, null, withFocus); JFormattedTextField field = new JFormattedTextField(factory); field.setValue("Four"); pan.add(field, BorderLayout.CENTER); return pan; } public static JPanel demo2( ){ // Demo 2: Change the format of a field when the user presses a button. We can't // call field.setFormatter( ) because it's a protected method. (It wouldn't work // anyway. The old factory would replace our new formatter with an "old" one next // time the field gains or loses focus.) Instead, send a new factory to // field.setFormatterFactory( ). JPanel pan = new JPanel(new BorderLayout( )); pan.setBorder(new TitledBorder("Demo 2: change format midstream")); MaskFormatter lowercase = null; try { lowercase = new MaskFormatter("LLLL"); } catch (ParseException pe) { } final JFormattedTextField field = new JFormattedTextField(lowercase); field.setValue("Fore"); pan.add(field, BorderLayout.CENTER); final JButton change = new JButton("change format"); JPanel changePanel = new JPanel( ); changePanel.add(change); pan.add(changePanel, BorderLayout.SOUTH); change.addActionListener(new ActionListener( ) { public void actionPerformed(ActionEvent ae) { try { field.commitEdit( ); // Commit current edit (if any). MaskFormatter uppercase = new MaskFormatter("UUUU"); DefaultFormatterFactory factory = new DefaultFormatterFactory(uppercase); field.setFormatterFactory(factory); change.setEnabled(false); } catch (ParseException pe) { } } }); return pan; } public static void main(String argv[]) { JFrame f = new JFrame("FactoryDemo"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane( ).add(demo1( ), BorderLayout.NORTH); f.getContentPane( ).add(demo2( ), BorderLayout.SOUTH); f.setSize(240, 160); f.setVisible(true); } } Figure 20-4. Formatter factory variations![]() |
I l@ve RuBoard |
![]() ![]() |