My JComboBox has a KeyListener with the following line: String valueStr = (String)(myJComboBox.getEditor().getItem()); So as i enter a single character, the KeyListener picks up the action of a character being typed into my JComboBox and then tries to display a string valueStr. Everything works fine, EXCEPT that the above mentioned string never displays the last character: for example if i make four key strokes, "ABCD", this is the output i will get (using System.out.println(valueStr) ): valueStr = //just typed A (expect to see A, but don't!! Only see empty string) valueStr = A //just typed B (expect to see AB, but don't!! Only see A) valueStr = AB //just typed C (expect to see ABC, but don't!! Only see AB) valueStr = ABC //just typed D (expect to see ABCD, but don't!! Only see ABC)
In general, does anyone know of a reliable way to get the value the user typed (NOT selected it) into editable JComboBox but not hit <return> yet? The way i suggested above works, but partialy. And this is not enough. I really need that last character!
posted 14 years ago
I figured it out. The underlying editor is a JTextField. Getting it's text by .getText() method in the KeyReleased (not KeyTyped or KeyPressed) does the trick.
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads