?
Solved

Add a KeyAdapter to an editable JComboBox

Posted on 2007-10-19
7
Medium Priority
?
373 Views
Last Modified: 2011-10-03
I need to retrieve any given input in the text field of an editable JComboBox whenever it is entered. I can't seem to make a KeyAdapter work, how can I get the String from the JComboBox whenever the user types something in it?
0
Comment
Question by:Cmerf486
  • 3
  • 2
  • 2
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20110522
Please post your code so far
0
 

Author Comment

by:Cmerf486
ID: 20110659
This code is a fragment out of a much larger work that deals specifically with my problem. The method displayDimensionCalculation() works perfectly with the IndexedItemListener as well as with IndexedKeyAdapters attached to JTextFields.

            if ( Main.getPresetStrings() != null && listSource != null )
            {
               Vector<PresetDropListValue> presets = Main.getPresetStrings();
               Vector<String> listValues = new Vector<String>();
               
               for ( int j = 0; j < presets.size(); j++ )
               {
                  if ( presets.get( j ).getListName().equals( listSource ) )
                  {
                     listValues.add( presets.get( j ).getValue() );
                  }
               }
               qListFields.add( new JComboBox( listValues ) );
               qListFields.lastElement().setPreferredSize( new Dimension( 366, 21 ) );
               qListFields.lastElement().setEditable( true );
               qListFields.lastElement().setFont( new java.awt.Font( "Arial", 1, 14 ) );
               associationObjects.add( new AssociationObject( newRecord.lastElement(), qListFields.lastElement(), jlabel3, jlabel5, Main.getOnAirColumnHeaders().get( i ) ) );
               displayDimensionCalculation( associationObjects.lastElement() );
               jlabel2.setVisible( true );
               jlabel3.setVisible( true );
               
               qListItemListeners.add( new IndexedItemListener( associationObjects.lastElement() ) {
                   public void itemStateChanged( java.awt.event.ItemEvent evt ) {
                      displayDimensionCalculation( getContextObject() );
                   }
               });
               
               qListKeyAdapters.add( new IndexedKeyAdapter( qListFields.size() - 1, this, associationObjects.lastElement() ) {
                  public void keyReleased( java.awt.event.KeyEvent evt ) {
                     displayDimensionCalculation( getContextObject() );
                  }
               });
               
               qListFields.lastElement().addItemListener( qListItemListeners.lastElement() );
               qListFields.lastElement().addKeyListener( qListKeyAdapters.lastElement() );
               getContentPane().add( qListFields.lastElement(), gbc );
            }
...
The code for IndexedKeyAdapter:

/*
 * IndexedKeyAdapter.java
 *
 * Created on October 15, 2007, 12:07 PM
 */

package comquestrddataentry;
import java.awt.event.KeyAdapter;

public class IndexedKeyAdapter extends KeyAdapter {
   
   private int associatedIndex;
   private AddToOnAirForm frame;
   private AssociationObject contextObject;
   
   /** Creates a new instance of IndexedKeyAdapter */
   public IndexedKeyAdapter() {
      super();
   }
   
   public IndexedKeyAdapter( int associatedIndex, AddToOnAirForm frame, AssociationObject contextObject ) {
      super();
      this.associatedIndex = associatedIndex;
      this.frame = frame;
      this.contextObject = contextObject;
   }
   
   public void setAssociatedIndex( int associatedIndex )
   {
      this.associatedIndex = associatedIndex;
   }
   
   public void setFrame( AddToOnAirForm frame )
   {
      this.frame = frame;
   }
   
   public void setContextObject( AssociationObject contextObject )
   {
      this.contextObject = contextObject;
   }
   
   public int getAssociatedIndex()
   {
      return associatedIndex;
   }
   
   public AddToOnAirForm getFrame()
   {
      return frame;
   }
   
   public AssociationObject getContextObject()
   {
      return contextObject;
   }
   
}
0
 

Author Comment

by:Cmerf486
ID: 20110695
My code is written like this because the form that it fits into is built dynamically based on entries in a given table in an Oracle database. This is my first attempt at ever writing anything with a GUI.

qListFields is of type Vector<JComboBox>
qListItemListeners is of type Vector<IndexedItemListeners>
qListKeyAdapters is of type Vector<IndexedKeyAdapters>
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:CEHJ
ID: 20114131
Do you mean you want an incremental search? e.g. if you have

Lemons
Limes

in the combo and you type in 'l' (the letter) you'll get Lemons selected and if you type in 'li' you'll get Limes?
0
 
LVL 92

Expert Comment

by:objects
ID: 20117492
try the approach used here:

http://www.orbital-computer.de/JComboBox/
0
 

Author Comment

by:Cmerf486
ID: 20123577
I don't need a search, I just need to be able to get the string in the editable field every single time a user enters or deletes a character or string. I know exactly what I need to do with the string once I get it, but I can't seem to get the string on every keystroke that affects the combobox.
0
 
LVL 92

Accepted Solution

by:
objects earned 1500 total points
ID: 20127009
use a Docuemnt for your text field and add the functionality you need to the document and/or add a listener to that document

Document doc = new PlainDocuemnt();
JTextComponent field = (JTextComponent) combo.getEditor().getEditorComponent();
field.setDocument(doc);
doc.addDocumentListener(mylistener);
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month15 days, 22 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question