FileDocCategorySizeDatePackage
StringDistance.javaAPI DocExample3164Sat Mar 13 14:24:48 GMT 2004None

StringDistance

public class StringDistance extends InlineJavaPerlCaller
The test class.

Fields Summary
JFrame
frame
JTextField[]
tf
JTextField
dist
JButton
go
JButton
exit
Constructors Summary
public StringDistance(String[] strs)

    frame = new JFrame("StringDistance");
    Container p = frame.getContentPane();
    p.setLayout(new GridLayout(0,2));

    // The input fields, including labels:
    tf = new JTextField[2];
    for (int i=0; i<2; i++) {
      p.add(new JLabel("String " + i + ":"));
      tf[i] = new JTextField(20);
      if ((strs != null) && (i < strs.length)) tf[i].setText(strs[i]);
      p.add(tf[i]);
    }

    // The output field, including label:
    p.add(new JLabel("Distance:"));
    dist = new JTextField(5);
    dist.setEditable(false);
    p.add(dist);

    // The main action button:
    go = new JButton("Compute distance");
    go.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent ae) {
                 dist.setText(Integer.toString(match(tf[0].getText(),
                                                     tf[1].getText())));
               }
             }
           );
    p.add(go);

    // To finish off:
    exit = new JButton("Exit");
    exit.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent ae) {
                 frame.dispose(); System.exit(0);
               }
             }
           );
    p.add(exit);

    if ((strs != null) && (strs.length > 1))
      dist.setText(Integer.toString(match(tf[0].getText(),
                   tf[1].getText())));
    frame.pack();
  
public StringDistance(String s0, String s1)

    this(new String[] { s0, s1 });
  
public StringDistance(String s0)

    this(new String[] { s0 });
  
public StringDistance()

    this((String[])null);
  
Methods Summary
public intmatch(java.lang.String s0, java.lang.String s1)
The central interface function to Perl.

    try {
      String str = (String)CallPerl("Text::Levenshtein", "distance",
                                    new Object [] {s0, s1});
      return Integer.parseInt(str);
    } catch (InlineJavaPerlException e) {
      System.err.println("Inline Java Perl Exception: " + e);
    } catch (InlineJavaException e) {
      System.err.println("Inline Java Exception: " + e);
    }
    return 0;
  
public voidsetText(int fieldno, java.lang.String str)

    tf[fieldno].setText(str);
  
public voidshow()
This shows everything

 frame.setVisible(true);