views:

540

answers:

2

Hello World !

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 dont 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: http://img198.imageshack.us/img198/3227/sosollsseinh.jpg Thanks for your attention

Marc

+1  A: 

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.

Gnoupi
Hi, 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....
Merc
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
yeah you are absoluty right ^^but the problem is that only the first row is shown.
Merc
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
ok now it nearly works !I ask the ComboBox for the EditorComponent and added a KeyListener, and evrytimes a key is typed the list will change.With this method there is only one problem !The Items are only shown 1 letter behind the actual input, because JTextFiel.getText() don't return the current Input, only the last befor the changeing. But a CaretListener produces a never ending loop, because everytime the data is updated a CaretEvent is send and so on. Any ideas ? ^^Here is a demo (video) of the problem: http://screenr.com/z4B
Merc
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
A: 

Can't you just as a JCombobox instead of a JList? 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));
jitter
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.
Merc
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
lol and for that you downvote?
jitter
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