FileDocCategorySizeDatePackage
CompletableJTextField.javaAPI DocExample4952Mon Jan 09 11:02:00 GMT 2006None

CompletableJTextField

public class CompletableJTextField extends JTextField implements ListSelectionListener

Fields Summary
Completer
completer
JList
completionList
DefaultListModel
completionListModel
JScrollPane
listScroller
JWindow
listWindow
Constructors Summary
public CompletableJTextField(int col)

        super (col);
        completer = new Completer();
        completionListModel = new DefaultListModel();
        completionList = new JList(completionListModel);
        completionList.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
        completionList.addListSelectionListener (this);
        listScroller =
            new JScrollPane (completionList, 
                             ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                             ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        listWindow = new JWindow();
        listWindow.getContentPane().add (listScroller);
    
Methods Summary
public voidaddCompletion(java.lang.String s)

        completer.addCompletion (s); 
public voidclearCompletions(java.lang.String s)

        completer.clearCompletions (); 
public voidremoveCompletion(java.lang.String s)

        completer.removeCompletion (s); 
public voidvalueChanged(javax.swing.event.ListSelectionEvent e)

        if (e.getValueIsAdjusting()) { return; }
        if (completionList.getModel().getSize() == 0) {return;}
        listWindow.setVisible (false);

        /*
        String completionString = 
            (String) completionList.getSelectedValue();
        // this causes an IllegalStateException:
        // "Attempt to mutate in notification"
        setText (completionString);
        */

        final String completionString =
            (String) completionList.getSelectedValue();
        Thread worker = new Thread() {
                public void run() {
                    setText (completionString);
                }
            };
        SwingUtilities.invokeLater (worker);