Text autocomplete , how to implement for each word

This is the code for text auto completed

Hi this code will check only the first entered text any one help me out check each word

ie if we entered "o" the text organization will display after we gave a "space" then enter the "o" again its not displaying the "organization again, any one can help me out

package swing;

import javax.swing.text.*;

import javax.swing.*;

import java.util.List;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Iterator;

publicclass AutoCompleteDocumentextends PlainDocument{

/**

*

*/

privatestaticfinallong serialVersionUID = 1;

private List<String> dictionary =new ArrayList<String>();

private JTextComponent comp;

public AutoCompleteDocument( JTextComponent field, String[] aDictionary ){

comp = field;

dictionary.addAll( Arrays.asList( aDictionary ) );

}

publicvoid addDictionaryEntry( String item ){

dictionary.add( item );

}

publicvoid insertString(int offs, String str, AttributeSet a)

throws BadLocationException{

super.insertString( offs, str, a );

String word = autoComplete( getText( 0, getLength() ) );

if( word !=null ){

super.insertString( offs + str.length(), word, a );

comp.setCaretPosition( offs + str.length() );

comp.moveCaretPosition( getLength() );

}

}

public String autoComplete( String text ){

for( Iterator i = dictionary.iterator(); i.hasNext(); ){

String word = (String) i.next();

if( word.startsWith( text ) ){

return word.substring( text.length() );

}

}

returnnull;

}

/**

* Creates a auto completing JTextField.

*

* @param dictionary an array of words to use when trying auto completion.

* @return a JTextField that is initialized as using an auto

* completing textfield.

*/

publicstatic JTextField createAutoCompleteTextField( String[] dictionary ){

JTextField field =new JTextField();

AutoCompleteDocument doc =new AutoCompleteDocument( field,dictionary );

field.setDocument(doc);

return field;

}

//field.addKeyListener(new java.awt.event.KeyAdapter()

//{

//public void keyTyped(java.awt.event.KeyEvent e) {

//String aa = field.getText();

// // TODO Auto-generated Event stub keyTyped()

//System.out.println("keyTyped"+aa);

//}

//

//});

@SuppressWarnings({"deprecation","deprecation","deprecation","deprecation","deprecation"})

publicstaticvoid main(String args[]){

javax.swing.JFrame frame =new javax.swing.JFrame("foo");

frame.setDefaultCloseOperation( javax.swing.JFrame.EXIT_ON_CLOSE );

String[] dict ={"Team","meeting","project","review","client","call","orginization","team"};

JTextField field = AutoCompleteDocument.createAutoCompleteTextField( dict );

System.out.println("keyTyped"+dict);

BoxLayout layout =new BoxLayout( frame.getContentPane(),BoxLayout.X_AXIS );

frame.getContentPane().setLayout( layout );

frame.getContentPane().add(new javax.swing.JLabel("Text Field: ") );

frame.getContentPane().add(field);

frame.show();

}

}

[6131 byte] By [rayees1234a] at [2007-11-27 11:53:24]
# 1

IF i am understanding you right.. you want the text in the field to change to "organization" as soon as you press the 'o' key? If so then you need to add a KeyListener to your text field..

field.addKeyListener( new KeyAdapter()

{

public void keyTyped( KeyEvent e )

{

field.setText( autoComplete( field.getText().substring(0,1) );

}

});

smithdale87a at 2007-7-29 18:50:04 > top of Java-index,Desktop,Core GUI APIs...
# 2

Here am added the key listener --> but the code which are given is getting error , What cani do ? Now the code is more clear ..... pls check the listener then u will be more clear

package swing;

import javax.swing.text.*;

import javax.swing.*;

import java.util.List;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Iterator;

public class AutoCompleteDocument extends PlainDocument {

/**

*

*/

private static final long serialVersionUID = 1;

private List<String> dictionary = new ArrayList<String>();

private JTextComponent comp;

public static String z;

public static String[] dict = { "Team", "meeting", "project", "review","client","call","orginization","team" };

public AutoCompleteDocument( JTextComponent field, String[] aDictionary ) {

comp = field;

dictionary.addAll( Arrays.asList( aDictionary ) );

}

public void addDictionaryEntry( String item ) {

dictionary.add( item );

}

public void insertString(int offs, String str, AttributeSet a)

throws BadLocationException {

super.insertString( offs, str, a );

String word = autoComplete( getText( 0, getLength() ) );

if( word != null ) {

super.insertString( offs + str.length(), word, a );

comp.setCaretPosition( offs + str.length() );

comp.moveCaretPosition( getLength() );

}

}

public String autoComplete( String text ) {

for( Iterator i = dictionary.iterator(); i.hasNext(); ) {

String word = (String) i.next();

if( word.startsWith( text ) ) {

return word.substring( text.length() );

}

}

return null;

}

/**

* Creates a auto completing JTextField.

*

* @param dictionary an array of words to use when trying auto completion.

* @return a JTextField that is initialized as using an auto

* completing textfield.

*/

public static JTextField createAutoCompleteTextField( String[] dictionary ) {

JTextField field = new JTextField();

AutoCompleteDocument doc = new AutoCompleteDocument( field,dictionary );

field.setDocument(doc);

// z = field.getText();

System.out.println("Stirng out side the key pressed "+z);

field.addKeyListener(new java.awt.event.KeyAdapter()

{

public void keyTyped(java.awt.event.KeyEvent e) {

char aa = e.getKeyChar();

//System.out.println("Stirng In side the key pressed "+z);

// TODO Auto-generated Event stub keyTyped()

System.out.println("keyTyped"+aa);

if(aa==' ')

{

System.out.println("You pressed Space");

// here i need to call again ->

}

}

});

return field;

}

public static void main(String args[]) {

javax.swing.JFrame frame = new javax.swing.JFrame("foo");

frame.setDefaultCloseOperation( javax.swing.JFrame.EXIT_ON_CLOSE );

//String[] dict = { "Team", "meeting", "project", "review","client","call","orginization","team" };

JTextField field = AutoCompleteDocument.createAutoCompleteTextField( dict );

System.out.println("keyTyped"+dict);

BoxLayout layout = new BoxLayout( frame.getContentPane(),BoxLayout.X_AXIS );

frame.getContentPane().setLayout( layout );

frame.getContentPane().add( new javax.swing.JLabel("Text Field: ") );

frame.getContentPane().add(field);

frame.show();

}

}

rayees1234a at 2007-7-29 18:50:04 > top of Java-index,Desktop,Core GUI APIs...
# 3

Any one can help me ?

rayees1234a at 2007-7-29 18:50:04 > top of Java-index,Desktop,Core GUI APIs...