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?
 
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
 
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.