• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

How to show JComboBox's selected item in JTextfield when it selected otherwise not?

 
Ranch Hand
Posts: 630
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I wrote small program for search Person's Name via Text field as I can search in mobile phone Contacts.

I put 5 names in combo box, when I search 'a' ie wrote 'a' in textfield then it shows all 5 names in combo box pop up
because each have character 'a', then I select 3rd or 4th name by mouse, then it should show in Text field where I wrote 'a'.

Each time it replace 'a' in text field to 1st name in combo box, I want to write 2nd, 3rd character, or select from drop down list in combo box.

But I can't do that.

For reference here is my program
 
Rancher
Posts: 508
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what happened when I ran and tried the app:

  • I typed in 'd' in the text field
  • The combobox opened and the names 'Adam', 'Dawson' showed in the list
  • And, the text field was populated with 'Adam' (it populates the first item/name from the opened/listed combobox).
  • At this stage, I can select another name/item from the combobox list and the textfield is populated with that selected one.


  • Now, if I press the <backspace> key (or button) ONCE it doesn't clear the character in the text field; the character keep populating back again and cursor returns back to its original position. But, if I press backspace persistently (keep it pressed) the action clears the textfiled. Also, I can clear the textfield by selecting the text in the textfiled using the mouse and deleting it.

    Thats the current behaviour of the app.

    The issue is to use the backspace key and the action should do what it does normally - move one character back while removing that character. And, let the user enter another character, etc.

    At this point I have not attempted to solve the issue. The code need to be presented properly to read and understand . It requires proper formatting and with comments at appropriate places (so that one can understand what that bit of code is actually meant for), etc.
     
    Rancher
    Posts: 3242
    30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Not sure I understand the question but I will suggest you should NOT be using a KeyListener on the text field.

    Instead, use a DocumentListener. This listener will fire an event whenever text is added or removed from the text field.

    Read the section from the Swing tutorial on How to Write a DocumentListener for more information and working examples to get you started.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic