Chapter 20. Formatted Text Fields


Swing provides extended functionality for text fields through the JFormattedTextField class introduced in SDK 1.4. A JFormattedTextField can display its value in a friendly (and locale-specific) way, enforce restrictions on its value, be used to edit non-String objects, and permit its value (or part of its value) to be incremented or decremented with the keyboard.

Figure 20-1 shows several JFormattedTextFields, but for the full effect you may wish to run the SimpleFTF program and play with it a bit. Most of the fields show locale-specific formatting. The Integer field puts delimiters between millions and thousands and between thousands and units. It changes its appearance (by dropping the delimiters) temporarily when it gains focus. An invalid value either adjusts to the closest valid value or reverts to the most recent valid value when a field loses focus, depending on the field. (For example, try changing the date to February 34.) Also, be sure to notice how elements of the Date field can be incremented and decremented with the up arrow and down arrow keys. (The L&F specifies keys for incrementing and decrementing, but existing L&Fs use the up arrow and down arrow. In addition, the Enter and Escape keys usually commit and cancel an edit, respectively.)

Figure 20-1. JFormattedTextFields in two different locales
figs/swng2.2001.gif

You might also notice some nonintuitive behavior. Attempting to edit the first Float field drops all but the first digit after the decimal point, and text input in the URL field defaults to overwrite mode (not the expected insert mode). As a workaround for the former, see the second Float field in SimpleFTF. For the latter, see the DefaultFormatter and its setOverwriteMode property later in this chapter.

Here's the code for SimpleFTF:

// SimpleFTF.java // import javax.swing.*; public class SimpleFTF extends JPanel {   public SimpleFTF( ) {     JFormattedTextField ftf[] = new JFormattedTextField[7];     String des[] = new String[ftf.length]; // Description of each field     des[0] = "Date";         ftf[0] = new JFormattedTextField(new java.util.Date( ));     des[1] = "Integer";      ftf[1] = new JFormattedTextField(new Integer(90032221));     des[2] = "Float";     ftf[2] = new JFormattedTextField(new Float(3.14));          des[3] = "Float work-around"; // Manually specify a NumberFormat.     ftf[3] = new JFormattedTextField(java.text.NumberFormat.getInstance( ));     ftf[3].setValue(new Float(3.14));     des[4] = "currency";     ftf[4] = new JFormattedTextField(java.text.NumberFormat.getCurrencyInstance( ));     ftf[4].setValue(new Float(5.99));     des[5] = "percent";     ftf[5] = new JFormattedTextField(java.text.NumberFormat.getPercentInstance( ));     ftf[5].setValue(new Float(0.33));     des[6] = "java.net.URL"; // Works via 1-arg String constructor and toString( )     java.net.URL u = null;     try {        u = new java.net.URL("http://www.ora.com/");     } catch (java.net.MalformedURLException ignored) { }     ftf[6] = new JFormattedTextField(u);     ftf[6].setColumns(24);     // Add each ftf[] to a BoxLayout.     setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));     for (int j=0; j < ftf.length; j+=1) {       JPanel borderPanel = new JPanel(new java.awt.BorderLayout( ));       borderPanel.setBorder(new javax.swing.border.TitledBorder(des[j]));       borderPanel.add(ftf[j], java.awt.BorderLayout.CENTER);       add(borderPanel);     }   }   public static void main(String argv[]) {     String localeString = java.util.Locale.getDefault( ).getDisplayName( );     JFrame f = new JFrame("SimpleFTF " + localeString);     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     f.setContentPane(new SimpleFTF( ));     f.pack( );     f.setVisible(true);   } }

Figure 20-2 shows the classes associated with JFormattedTextField. We'll describe each Swing class in turn.

Figure 20-2. JFormattedTextField class diagram
figs/swng2.2002.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