Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JComboBox question

 
lev grevnin
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
lev grevnin
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic