FileDocCategorySizeDatePackage
CombinationFormatter.javaAPI DocExample4441Thu Oct 24 20:14:26 BST 2002None

CombinationFormatter

public class CombinationFormatter extends DefaultFormatter

Fields Summary
Constructors Summary
public CombinationFormatter()

    setOverwriteMode(false);
  
Methods Summary
protected javax.swing.Action[]getActions()

    Action[] actions = { new CombinationIncrementer("increment", 1),
                         new CombinationIncrementer("decrement", -1) };
    return actions;
  
public static voidmain(java.lang.String[] argv)

    // a demo main() to show how CombinationFormatter could be used
    int comb1[] = { 35, 11, 19 };
    int comb2[] = { 10, 20, 30 };

    final JFormattedTextField field1 =
      new JFormattedTextField(new CombinationFormatter());
    field1.setValue(comb1);

    final JFormattedTextField field2 =
      new JFormattedTextField(new CombinationFormatter());
    field2.setValue(comb2);

    JPanel pan = new JPanel();
    pan.add(new JLabel("Change the combination from"));
    pan.add(field1);
    pan.add(new JLabel("to"));
    pan.add(field2);

    JButton b = new JButton("Submit");
    b.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent ae) {
        try {
          field1.commitEdit(); // make sure current edit (if any) gets committed
          field2.commitEdit();
        } catch (java.text.ParseException pe) { }
        int oldc[] = (int[])field1.getValue();
        int newc[] = (int[])field2.getValue();
        //
        // code to validate oldc[] and change to newc[] goes here
        //
      }
    });
    pan.add(b);

    JFrame f = new JFrame("CombinationFormatter Demo");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(pan);
    f.setSize(360, 100);
    f.setVisible(true);
  
public java.lang.ObjectstringToValue(java.lang.String string)

    // input: string of form "15-45-22" (any number of hyphen-delimeted numbers)
    // output: int array
    String s[] = string.split("-");
    int a[] = new int[s.length];
    for (int j=0; j<a.length; j+=1)
      try {
        a[j] = Integer.parseInt(s[j]);
      } catch (NumberFormatException nfe) {
        throw new java.text.ParseException(s[j] + " is not an int", 0);
      }
    return a;
  
public java.lang.StringvalueToString(java.lang.Object value)

    //  input: int array
    // output: string of numerals separated by hyphens
    if (value == null) return null;
    if (! (value instanceof int[]))
      throw new java.text.ParseException("expected int[]", 0);
    int a[] = (int[])value;
    StringBuffer sb = new StringBuffer();
    for (int j=0; j < a.length; j+=1) {
      if (j > 0) sb.append('-");
      sb.append(a[j]);
    }
    return sb.toString();