// // another example of the Java AWT, showing different ways to process // changes in TextField Objects. // see class definition for details. // // observation suggests that on some systems, when "all" is chosen // this program may not be able to keep up with a fast-typing // user, leading to very strange behavior (jumbled-up text in // the TextField being edited). // import java.awt.* ; import java.awt.event.* ; // // a MirrorLine object is a CloseableFrame containing two TextField // objects whose contents are to be kept "in sync" (equivalent). // if syncOnAll = true, the fields are resynchronized every time // one of them changes; otherwise, the fields are resynchronized // only when the user presses "return" in the field currently // receiving keyboard input. // class MirrorLine extends CloseableFrame { MirrorLine(boolean syncOnAll) { super("MirrorLine") ; // use BorderLayout so TextField objects (at "north" // and "south" positions) keep their original // height. setLayout(new BorderLayout()) ; String startString = "Hello!" ; int startWidth = 40 ; TextField t1 = new TextField(startString, startWidth) ; TextField t2 = new TextField(startString, startWidth) ; add(t1, "North") ; add(t2, "South") ; if (syncOnAll) { // one adapter has source t1 and target t2 t1.addTextListener(new SyncAllAdapter(t2)) ; // other adapter has source t2 and target t1 t2.addTextListener(new SyncAllAdapter(t1)) ; } else { // one adapter has source t1 and target t2 t1.addActionListener( new SyncReturnAdapter(t2)) ; // other adapter has source t2 and target t1 t2.addActionListener( new SyncReturnAdapter(t1)) ; } pack() ; show() ; } // // ---- main --------------------------------------------------- // // command-line argument: all/return // "all" -> syncOnAll = true (see comments for class). // "return" -> syncOnAll = false. // public static void main(String[] args) { if (args.length < 1) { System.out.println("Argument is all/return") ; System.exit(-1) ; } boolean syncOnAll = false ; if (args[0].equals("all")) { syncOnAll = true ; } else if (!args[0].equals("return")) { System.out.println("Argument is all/return") ; System.exit(-1) ; } MirrorLine m = new MirrorLine(syncOnAll) ; } // // ---- inner classes ------------------------------------------ // // // a SyncAllAdapter object resyncs its target and source // TextField objects every time the source's text changes. // class SyncAllAdapter implements TextListener { TextField target ; SyncAllAdapter(TextField t) { target = t ; } public void textValueChanged(TextEvent e) { TextField src = (TextField) e.getSource() ; if (!target.getText().equals(src.getText())) target.setText(src.getText()) ; } } // // a SyncReturnAdapter object resyncs its target and source // TextField objects every time the user presses "return" // in its source field. // class SyncReturnAdapter implements ActionListener { TextField target ; SyncReturnAdapter(TextField t) { target = t ; } public void actionPerformed(ActionEvent e) { TextField src = (TextField) e.getSource() ; if (!target.getText().equals(src.getText())) target.setText(src.getText()) ; } } }