How to add autocompletion to jtable

Hi, i want to add auto completion jtextfield in the jtable cell. How can i add a auto completion textfield so that when the cell gets editing mode , i can display a JList containing all the words starting with entered word.

Thanks
Computer07Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
I guess you can use keyListener and modify your list with each key - probably not very simple stuff
0
Computer07Author Commented:
currently i am using a jtextfield  and jlist. And i am able to display all the words that matches in jtextfield.  How can i render my jtextfield to each jtable cell and i want to the show the jlist only when we select the table cell for editing, u can say as a popup list ??
0
for_yanCommented:
I gues you need to register some cell selection event
and popup the list when selection ocuurs.

Perhaps this will be  of some use:

http://www.exampledepot.com/egs/javax.swing.table/SelEvent.html

http://www.exampledepot.com/egs/javax.swing.table/GetSel.html


0
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Computer07Author Commented:
I have added a jtextfield and default table cell editor. when we add a new row  and start editing the cell , a dialog opens below the textfiled that display a jlist of all suggetation. I can select the word with mouse that display in jtextfield.  But when i select any thing in list keyboard  and press enter key , the selected word does not display in text field ,  instead it call the table model listener. How can i make so that when press enter key on list view it display the selected text in text field, and after if i press the enter key table model listener will be callled ??
0
gordon_vt02Commented:
You need to subclass javax.swing.AbstractCellEditor and have your class implement javax.swing.table.TableCellEditor.  Your class should contain both the JTextField and JList components as well as a JPanel that contains them.  The getTableCellEditorComponent() method should configure the values of the text field and list, then return the panel as the editor component.  (You don't need to recreate the GUI elements every time they are needed, that will just waste memory and slow down your system.)

You should attach a KeyListener to the text field (enable key events) and update the list accordingly as the text changes.  When a selection is made in the list, you should update the text field and, if that selection should trigger the end of the cell editing, call fireEditingStopped() to notify the table.
0
gordon_vt02Commented:
public class AutoCompleteCellEditor extends AbstractCellEditor implements TableCellEditor {
    private JTextField textField;
    private JList list;
    private JPanel panel;
    
    public AutoCompleteCellEditor() {
        textField = new JTextField();
        textField.addKeyListener(new AutoCompleteKeyListener());
        // you should also add an ActionListener that notifies the table
        // editing has stopped when Enter is pressed in the text field
        
        list = new JList();
        // add a ListSelectionListener and/or MouseListener to monitor for
        // selection or double-click events to trigger an update of the
        // text field from the list
        
        panel = new JPanel();
        // init panel with an appropriate layout and add the textField and list
        // objects to it
    }
    
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int col) {
        String text = (value != null ? value.toString() : "");
        textField.setText(text);
        updateAutoComplete();
        return panel;
    }
    
    public Object getCellEditorValue() {
        return textField.getText();
    }
    
    private void setValueFromList(String selected) {
        // sets the text field value and notifies table that editing is complete
        textField.setText(selected);
        fireEditingStopped();
    }
    
    private void updateAutoComplete() {
        String text = textField.getText();
        String[] values = getAutoCompleteValues(text);
        list.setListData(values);
    }
    
    private String[] getAutoCompleteValues(String text) {
        // get auto-complete values for the given string
    }
    
    private class AutoCompleteKeyListener extends KeyAdapter {
        // override the appropriate method: keyTyped, keyPressed, keyReleased
        // keyTyped is likely the one you want, but try the others if that
        // doesn't work
        public void keyTyped(KeyEvent e) {
            updateAutoComplete();
        }
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.