Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Unless you apply particular behavior to your list, you could try another way, by adding a combobox as editor of this table. Check <a href="http://java.sun.com/javase/6/docs/api/javax/swing/DefaultCellEditor.html" rel="nofollow noreferrer">DefaultCellEditor</a> for an example of it.</p> <p>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 <a href="http://java.sun.com/javase/6/docs/api/javax/swing/table/TableCellEditor.html" rel="nofollow noreferrer">TableCellEditor</a>, so that you will be able to change the values from it depending on when it is called (in the method getTableCellEditorComponent()).</p> <hr> <p><strong>Edit:</strong> About the <strong>KeyListener</strong> 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 <code>getText()</code> returns the value without the new character.</p> <p>However, since this is a <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/KeyEvent.html" rel="nofollow noreferrer">KeyEvent</a>, you have access to the typed character, with <code>evt.getKeyChar()</code> directly, or <code>evt.getKeyCode()</code>, to check if this is actually a letter which was typed. With these methods, you can know the complete "text" that you need.</p> <hr> <p>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.</p> <p>If you are able (and allowed) to use extra libraries, I would recommend you to have a look at <strong>SwingX components</strong> (<a href="http://swinglabs.org/" rel="nofollow noreferrer">http://swinglabs.org/</a>). 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. </p> <p>In their package "<strong>autocomplete</strong>", 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.</p> <p>You can <a href="http://swinglabs.org/downloads.jsp" rel="nofollow noreferrer">download the .Jar and the javadoc</a> on their site. <a href="http://pirlwww.lpl.arizona.edu/resources/guide/software/SwingX/org/jdesktop/swingx/autocomplete/package-summary.html" rel="nofollow noreferrer">Here is a copy of the javadoc</a> from another site, for the autocomplete package, though it could have changed in the meantime, but it will give you an idea.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload