Link to home
Start Free TrialLog in
Avatar of Computer07
Computer07

asked on

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
Avatar of for_yan
for_yan
Flag of United States of America image

I guess you can use keyListener and modify your list with each key - probably not very simple stuff
Avatar of Computer07
Computer07

ASKER

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 ??
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


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 ??
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.
ASKER CERTIFIED SOLUTION
Avatar of gordon_vt02
gordon_vt02

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial