20.8 The DefaultFormatterFactory Class


DefaultFormatterFactory 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 Properties

Table 20-7 shows the formatter properties defined by DefaultFormatterFactory.

Table 20-7. DefaultFormatterFactory properties

Property

Data type

get

is

set

Default value

defaultFormatter

JFormattedTextField.AbstractFormatter

·

 

·

null

displayFormatter

JFormattedTextField.AbstractFormatter

·

 

·

null

editFormatter

JFormattedTextField.AbstractFormatter

·

 

·

null

nullFormatter

JFormattedTextField.AbstractFormatter

·

 

·

null

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

public DefaultFormatterFactory( )

Create a new DefaultFormatterFactory with no formatters.

public DefaultFormatterFactory(JFormattedTextField.AbstractFormatter defaultFormatter)
public DefaultFormatterFactory(JFormattedTextField.AbstractFormatter defaultFormatter, JFormattedTextField.AbstractFormatter displayFormatter)
public DefaultFormatterFactory(JFormattedTextField.AbstractFormatter defaultFormatter, JFormattedTextField.AbstractFormatter displayFormatter, JFormattedTextField.AbstractFormatter editFormatter)
public DefaultFormatterFactory(JFormattedTextField.AbstractFormatter defaultFormatter, JFormattedTextField.AbstractFormatter displayFormatter, JFormattedTextField.AbstractFormatter editFormatter, JFormattedTextField.AbstractFormatter nullFormatter)

Create a new DefaultFormatterFactory with the specified defaultFormatter, displayFormatter, editFormatter, and nullFormatter. Versions that lack one or more of the parameters act as though null were passed for the missing parameter.

20.8.3 Public Method

public JFormattedTextField.AbstractFormatter getFormatter(JFormattedTextField tf)

If tf.getValue( ) is null and nullFormatter is not, this method returns nullFormatter. If tf has focus and editFormatter is not null, this method returns editFormatter. If tf does not have focus and displayFormatter is not null, this method returns displayFormatter. Otherwise, this method returns defaultFormatter.

20.8.4 Example

This 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
figs/swng2.2004.gif


Java Swing
Graphic Java 2: Mastering the Jfc, By Geary, 3Rd Edition, Volume 2: Swing
ISBN: 0130796670
EAN: 2147483647
Year: 2001
Pages: 289
Authors: David Geary

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net