Swing, show a JList over a editable jTable to select entries to autocomplete the text?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

Unless you apply particular behavior to your list, you could try another way, by adding a combobox as editor of this table. Check DefaultCellEditor for an example of it.

Up vote 2 down vote favorite 1 share g+ share fb share tw.

I have a JTable with editable cells. Each Cell contains a CarretListener for quick validation of the entered text. But in one special cell you should be able to select entrys out of a list.

The List is generated when you enter a text. The Programm serach in a list for entrys equal so the entered text, like google suggest. So far its all good.

But I don't get it how to show the list at the right position. I tried the GlassPane but this doesn't work so well. I have problems to get the Cordinates of the cell and to show the JList.

Set the row height so show the whole list also dosn't work because I don't want to change the whole row. Maybe there is a trick in the TableCellRenderer or so...? I don't want a complete sourcecode or so, but a I need a push in the right direction. Here is a pic of the programm and hwo it should look like:

Thanks for your attention Marc java swing jtable jlist link|improve this question edited Nov 10 '09 at 10:24 asked Nov 10 '09 at 9:49user173269.

Unless you apply particular behavior to your list, you could try another way, by adding a combobox as editor of this table. Check DefaultCellEditor for an example of it. If you modify the content of the combobox when it's called, with the correct values, it will match your need, I think.

For this, you will need to make your own CellEditor, most likely by implementing TableCellEditor, so that you will be able to change the values from it depending on when it is called (in the method getTableCellEditorComponent()). Edit: About the KeyListener problem you are talking about in comment, you have to think that a Key event is sent before the actual text is updated in the JTextField. So it is normal that your call to getText() returns the value without the new character.

However, since this is a KeyEvent, you have access to the typed character, with evt.getKeyChar() directly, or evt.getKeyCode(), to check if this is actually a letter which was typed. With these methods, you can know the complete "text" that you need. Also, from the video in your comment, it seems that you want a list which is in fact adapting according to what the user started typing, and restraining choice according to what was already entered.

If you are able (and allowed) to use extra libraries, I would recommend you to have a look at SwingX components (http://swinglabs.org/). This library proposes in general plenty of useful components to use in swing interfaces. There is a demo on their site, though it seems to not be available at this hour, maybe a later day.

In their package "autocomplete", you will be able to find a class named "AutoCompleteDecorator", and other useful ones, which will allow you to improve your Combobox editor, so that it will try to complete the user input itself, and scroll to the good position in the list (I think it can also filter the list, the exact behavior you want, but I'm not fully sure). If you can use this, it would actually save you the hassle of taking care of the caret events yourself, as well as updating the list, as it would most likely do it for you. You can download the .

Jar and the javadoc on their site. Here is a copy of the javadoc from another site, for the autocomplete package, though it could have changed in the meantime, but it will give you an idea.

Thanks for the quick answer. I already have my own CellEditor. But when I use a jList instead of a JTextField only the first row is shown, thats the problem.

I Think is best way would be to show a list under the Cell with the TextField. Nom i'm trying to workaround with InternalFrames.... – user173269 Nov 10 '09 at 10:23 I'm not sure I understand. A field + a list, is more or less a combobox, that's what I mean.

A workaround with InternalFrames for a popup list seems a bit overkill :/ – Gnoupi Nov 10 '09 at 10:45 yeah you are absoluty right ^^ but the problem is that only the first row is shown. – user173269 Nov 10 '09 at 10:47 Still not sure about what you mean by "only the first row is shown" ;) In a Combobox, if you have more than 1 element in it, it will display more than one row in the proposition list, simply. I really recommend you to try a solution based on a Combobox Editor before experimenting things like making your own popup, etc. It would save you some work, really.

Unless you have a particular reason to not want to use a Combobox. – Gnoupi Nov 10 '09 at 13:25 1 With event listener loops of that kind, the usual solution is to add a valueIsAdjusting flag or similar -- your listener sets the flag before it does its thing, and clears it afterwards (preferably with try/finally); and the first thing it does is check whether the flag is set, so it can ignore events that are a side effect of its own action. – David Moles 9 Nov3 at 8:41.

That would be easier no. As DefaultCellEditor supports JCombobox out of the box, TableColumn X = table.getColumnModel(). GetColumn(Y); JComboBox cb = new JComboBox(VECTOR_OF_ITEMS); X.

SetCellEditor(new DefaultCellEditor(cb)).

Okyou both are right. But how do I manage that always 4 rows under the input Field of the comboBox are shown? I can type and but he don't show the later entries.

– user173269 Nov 10 '09 at 11:54 Thanks jitter for... saying exactly what I said. However he needs to be able to change the content of this combobox depending on the cell, so he can't use a DefaultCellEditor. – Gnoupi Nov 10 '09 at 13:20 lol and for that you downvote?

– jitter Nov 10 '09 at 13:42 Besides the fact that I believe that duplicating answers is not really of any benefit, your answer is not working for his needs, like explained, simply. – Gnoupi Nov 10 '09 at 14:17.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions