001    package util.swing;
002    
003    import javax.swing.*;
004    import javax.swing.event.*;
005    
006    /**
007     * A {@link JTextField} that writes its current content into a String referenced through an array. This is useful
008             * so that you will not need references to the <code>JTextField</code> in your code that might eventually lead
009     * to the text field being serialized which, in turn, might not work properly.
010     *
011     * <p><b>Note:</b> This class is not meant to be serialized!</p>
012     *
013     * @author Steffen Zschaler
014     * @version 2.0 28/07/1999
015     * @since v2.0
016     */
017    public class JTextInput extends JTextField {
018    
019        /**
020             * ID for serialization.
021             */
022            private static final long serialVersionUID = 8302445242306618528L;
023            
024            /**
025         * The currently referenced output observer. The current text of the input field can be found as the first
026         * element of the array.
027         *
028         * @serial This class is not meant to be serialized!
029         */
030        protected String[] m_asOutput;
031    
032        /**
033         * Create a new <code>JTextInput</code>.
034         *
035         * @param asOutput the output observer. The current text of the input field can be found as the first
036         * element of the array at any time.
037         * @param sInitial the initial value of the text field.
038         */
039        public JTextInput(String[] asOutput, String sInitial) {
040            super(sInitial);
041    
042            m_asOutput = asOutput;
043    
044            getDocument().addDocumentListener(new DocumentListener() {
045                public void changedUpdate(DocumentEvent e) {
046                    performUpdate();
047                }
048    
049                public void insertUpdate(DocumentEvent e) {
050                    performUpdate();
051                }
052    
053                public void removeUpdate(DocumentEvent e) {
054                    performUpdate();
055                }
056    
057                private void performUpdate() {
058                    m_asOutput[0] = getText();
059                }
060            });
061        }
062    }