Class AutocompletionDocument

  • All Implemented Interfaces:
    NestingDocument, javax.swing.text.Document

    public class AutocompletionDocument
    extends java.lang.Object
    implements NestingDocument
    An adapter for the document in the JTextComponent of a JComboBox that extends the JComboBox to automatically complete the field text using entries from the combo box list.
    Author:
    Chris Jennings
    • Field Summary

      • Fields inherited from interface javax.swing.text.Document

        StreamDescriptionProperty, TitleProperty
    • Constructor Summary

      Constructors 
      Constructor Description
      AutocompletionDocument​(javax.swing.JComboBox<?> comboBox)
      Create a new AutocompletionDocument that is wrapped around the existing document within a combo box.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addDocumentListener​(javax.swing.event.DocumentListener listener)  
      void addUndoableEditListener​(javax.swing.event.UndoableEditListener listener)  
      javax.swing.text.Position createPosition​(int offs)  
      boolean getAutocompletion()  
      java.text.Collator getCollator()
      Return the list order Collator, or null if none is set.
      javax.swing.JComboBox<?> getComboBox()
      Return the JComboBox that this document is autocompleting.
      javax.swing.text.Element getDefaultRootElement()  
      javax.swing.text.Document getDocument()
      Return the original document that is being wrapped by this adapter.
      javax.swing.text.Position getEndPosition()  
      int getLength()  
      java.lang.Object getProperty​(java.lang.Object key)  
      javax.swing.text.Element[] getRootElements()  
      javax.swing.text.Position getStartPosition()  
      java.lang.String getText​(int offset, int length)  
      void getText​(int offset, int length, javax.swing.text.Segment txt)  
      void insertString​(int offset, java.lang.String str, javax.swing.text.AttributeSet a)  
      static AutocompletionDocument install​(javax.swing.JComboBox<?> comboBox)
      Create and install an AutocompletionDocument on an existing JComboBox, causing it to begin autocompleting the text field content.
      static AutocompletionDocument install​(javax.swing.JComboBox<?> comboBox, boolean casesensitive)
      Create and install an AutocompletionDocument on an existing JComboBox, causing it to begin autocompleting the text field content.
      boolean isCaseSensitive()  
      void putProperty​(java.lang.Object key, java.lang.Object value)  
      void remove​(int offs, int len)  
      void removeDocumentListener​(javax.swing.event.DocumentListener listener)  
      void removeUndoableEditListener​(javax.swing.event.UndoableEditListener listener)  
      void render​(java.lang.Runnable r)  
      void setAutocompletion​(boolean autocompletion)  
      void setCaseSensitive​(boolean casesensitive)  
      void setCollator​(java.text.Collator collator)
      If set to a non-null object, it is assumed that list elements are sorted according to collator.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • AutocompletionDocument

        public AutocompletionDocument​(javax.swing.JComboBox<?> comboBox)
        Create a new AutocompletionDocument that is wrapped around the existing document within a combo box. The combo box's editor must be a subclass of JTextComponent.

        The new document is not installed in the combo box, so autocompletion is not started automatically. To easily create and install an adapter when possible, use install(JComboBox).

        Parameters:
        comboBox -
        Throws:
        java.lang.IllegalArgumentException - is the combo box's editor is not a JTextComponent
    • Method Detail

      • getDocument

        public javax.swing.text.Document getDocument()
        Return the original document that is being wrapped by this adapter.
        Specified by:
        getDocument in interface NestingDocument
        Returns:
        the wrapped document
      • getComboBox

        public javax.swing.JComboBox<?> getComboBox()
        Return the JComboBox that this document is autocompleting.
        Returns:
        the autocompleted combo box
      • install

        public static AutocompletionDocument install​(javax.swing.JComboBox<?> comboBox)
        Create and install an AutocompletionDocument on an existing JComboBox, causing it to begin autocompleting the text field content.
        Parameters:
        comboBox - the JComboBox to modify
        Returns:
        the newly installed document if installation was successful, otherwise null if it failed because the JComboBox is not using a JTextComponent
      • install

        public static AutocompletionDocument install​(javax.swing.JComboBox<?> comboBox,
                                                     boolean casesensitive)
        Create and install an AutocompletionDocument on an existing JComboBox, causing it to begin autocompleting the text field content.
        Parameters:
        comboBox - the JComboBox to modify
        casesensitive - whether autocompletion matching should be case sensitive
        Returns:
        the newly installed document if installation was successful, otherwise null if it failed because the JComboBox is not using a JTextComponent
      • remove

        public void remove​(int offs,
                           int len)
                    throws javax.swing.text.BadLocationException
        Specified by:
        remove in interface javax.swing.text.Document
        Throws:
        javax.swing.text.BadLocationException
      • insertString

        public void insertString​(int offset,
                                 java.lang.String str,
                                 javax.swing.text.AttributeSet a)
                          throws javax.swing.text.BadLocationException
        Specified by:
        insertString in interface javax.swing.text.Document
        Throws:
        javax.swing.text.BadLocationException
      • getCollator

        public java.text.Collator getCollator()
        Return the list order Collator, or null if none is set.
        Returns:
        the Collator by which elements are ordered
      • setCollator

        public void setCollator​(java.text.Collator collator)
        If set to a non-null object, it is assumed that list elements are sorted according to collator. A faster search algorithm can be employed when looking for an autocomplete match.
        Parameters:
        collator - the Collator by which elements are ordered
      • getLength

        public int getLength()
        Specified by:
        getLength in interface javax.swing.text.Document
      • addDocumentListener

        public void addDocumentListener​(javax.swing.event.DocumentListener listener)
        Specified by:
        addDocumentListener in interface javax.swing.text.Document
      • removeDocumentListener

        public void removeDocumentListener​(javax.swing.event.DocumentListener listener)
        Specified by:
        removeDocumentListener in interface javax.swing.text.Document
      • addUndoableEditListener

        public void addUndoableEditListener​(javax.swing.event.UndoableEditListener listener)
        Specified by:
        addUndoableEditListener in interface javax.swing.text.Document
      • removeUndoableEditListener

        public void removeUndoableEditListener​(javax.swing.event.UndoableEditListener listener)
        Specified by:
        removeUndoableEditListener in interface javax.swing.text.Document
      • getProperty

        public java.lang.Object getProperty​(java.lang.Object key)
        Specified by:
        getProperty in interface javax.swing.text.Document
      • putProperty

        public void putProperty​(java.lang.Object key,
                                java.lang.Object value)
        Specified by:
        putProperty in interface javax.swing.text.Document
      • getText

        public java.lang.String getText​(int offset,
                                        int length)
                                 throws javax.swing.text.BadLocationException
        Specified by:
        getText in interface javax.swing.text.Document
        Throws:
        javax.swing.text.BadLocationException
      • getText

        public void getText​(int offset,
                            int length,
                            javax.swing.text.Segment txt)
                     throws javax.swing.text.BadLocationException
        Specified by:
        getText in interface javax.swing.text.Document
        Throws:
        javax.swing.text.BadLocationException
      • getStartPosition

        public javax.swing.text.Position getStartPosition()
        Specified by:
        getStartPosition in interface javax.swing.text.Document
      • getEndPosition

        public javax.swing.text.Position getEndPosition()
        Specified by:
        getEndPosition in interface javax.swing.text.Document
      • createPosition

        public javax.swing.text.Position createPosition​(int offs)
                                                 throws javax.swing.text.BadLocationException
        Specified by:
        createPosition in interface javax.swing.text.Document
        Throws:
        javax.swing.text.BadLocationException
      • getRootElements

        public javax.swing.text.Element[] getRootElements()
        Specified by:
        getRootElements in interface javax.swing.text.Document
      • getDefaultRootElement

        public javax.swing.text.Element getDefaultRootElement()
        Specified by:
        getDefaultRootElement in interface javax.swing.text.Document
      • render

        public void render​(java.lang.Runnable r)
        Specified by:
        render in interface javax.swing.text.Document
      • getAutocompletion

        public boolean getAutocompletion()
      • setAutocompletion

        public void setAutocompletion​(boolean autocompletion)
      • isCaseSensitive

        public boolean isCaseSensitive()
      • setCaseSensitive

        public void setCaseSensitive​(boolean casesensitive)