Results 1 to 4 of 4
  1. #1
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default JComboBox search for match list

    Hi, here again :)
    I am trying to search equivalent list in JCombobox based on user input. JComboBox is set to editable. JComboBox' KeyListener does not work, ActionListener works on lost focus. I have no idea what to search in google. I am totally clueless now.

    Here is what I try:
    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class JComboTrial extends JFrame implements ActionListener, KeyListener
    {	JComboBox jc1 = null;
    	JButton b1 = new JButton("OK");
    	JPanel p = new JPanel();
    
    	public JComboTrial()
    	{	String[] strList = {"DE 0001", "DF 0504", "DF 0604", "FR 5971", "GE 0587", "GKL 684"};
    		jc1 = new JComboBox(strList);
    
    		jc1.addActionListener(this);
    		jc1.addKeyListener(this);
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		this.setLayout(new BorderLayout());
    		this.add(p, BorderLayout.CENTER);
    		p.setLayout(new BorderLayout());
    		p.add(jc1, BorderLayout.CENTER);
    		p.add(b1, BorderLayout.LINE_END);
    		pack();
    
    		jc1.setEditable(true);
    
    	}
    
    	public void actionPerformed(ActionEvent ae)
    	{	System.out.println("From Action event");
    		cmbSelectInput(jc1, jc1.getSelectedItem().toString());
    	}
    
    	public void keyTyped(KeyEvent ae)
    	{	System.out.println("FROM Typed");
    		cmbSelectInput(jc1, jc1.getSelectedItem().toString());
    	}
    
    	public void keyPressed(KeyEvent ae)
    	{	System.out.println("FROM Pressed");
    		cmbSelectInput(jc1, jc1.getSelectedItem().toString());
    	}
    
    	public void keyReleased(KeyEvent ae)
    	{	System.out.println("FROM Released");
    		cmbSelectInput(jc1, jc1.getSelectedItem().toString());
    	}
    
        	void cmbSelectInput(JComboBox combo, String input)
        	{   	int cmb_count = combo.getItemCount() - 1;
            		int inputlen = combo.getSelectedItem().toString().length()-1;
            		String combo_item = null;
            		for(int i=0; i < cmb_count; i++)
            		{	combo_item = combo.getItemAt(i).toString().substring(0, inputlen);
                			if(input.equals(combo_item))
                			{   	combo.setSelectedIndex(i);
                    			break;
                			}
            		}
        	}//end of cmbSelectInput
    
    	public static void main(String[] args)
    	{	new JComboTrial().setVisible(true);
    	}
    
    }
    Thanks in advance,
    geje

  2. #2
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    click the below link it explains there

    KeyListener in JComboBox Chad.Stever
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    JComboBox is set to editable. JComboBox' KeyListener does not work
    Are these incompatible? Who is to get the key strokes, the JComboBox or your listener?
    What happens if its not editable?

  4. #4
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    What happens if its not editable?
    KeyListener works as expected if JCombobox is not editable. But I have to set editable to true because
    I should allow user to input and base on their input I should find its match on Jcombobox list

    Thanks alacn. I was able to solve tha problem in KeyListener.

    Here is what I have done. Thanks.
    Java Code:
        import javax.swing.*;
        import java.awt.event.*;
        import java.awt.*;
    
        public class JComboTrial extends JFrame
        {	JComboBox jc1 = null;
                JButton b1 = new JButton("OK");
                JPanel p = new JPanel();
    
                public JComboTrial()
                {	String[] strList = {"DE 0001", "DF 0504", "DF 0604", "FR 5971", "GE 0587", "GKL 684"};
                        jc1 = new JComboBox(strList);
    
                                jc1.getEditor().getEditorComponent().addKeyListener(new KeyListener()
                                {	public void keyTyped(KeyEvent ek)
                                {cmbSelectInput(ek, jc1);	}
    
                                public void keyPressed(KeyEvent ek)
                                {
                                }
    
                                public void keyReleased(KeyEvent ek)
                                {}
                                });
    
                                jc1.getEditor().getEditorComponent().addFocusListener(new FocusListener()
                        {   	public void focusGained(FocusEvent ef)
                                        {	jc1.setPopupVisible(true);
                                        }
                                        public void focusLost(FocusEvent ef){}
                                });
    
                        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
                        this.setLayout(new BorderLayout());
                        this.add(p, BorderLayout.CENTER);
                        p.setLayout(new BorderLayout());
                        p.add(jc1, BorderLayout.CENTER);
                        p.add(b1, BorderLayout.LINE_END);
                        pack();
    
                        jc1.setEditable(true);
    
                }
    
    
                void cmbSelectInput(KeyEvent ek, JComboBox combo)
                {	int cmb_count = combo.getItemCount() - 1;
                                Color color = new Color(51,153,255);
    
    
                        String combo_item = null;
                                JTextField tf = null;
    
                                tf = (JTextField)combo.getEditor().getEditorComponent();
    
                        String text = tf.getText();
                        if (text!=null && (ek.getKeyChar() ==KeyEvent.VK_BACK_SPACE || 
                            ek.getKeyCode() ==KeyEvent.VK_DELETE) && text.length()>0)
                        {
                                text = text.substring(0, text.length() -1);
                                tf.setText(tf.getText());
                                        tf.setSelectionColor(color);
                                        tf.setSelectedTextColor(Color.WHITE);
                                        tf.setSelectionStart(text.length());
                                        tf.setSelectionEnd(tf.getText().length());
                        }
                        else
                        {	if(! "".equals(text) && text != null && text.length() > 0)
                                { 	for(int i=0; i < cmb_count; i++)
                                                {        	combo_item = combo.getItemAt(i).toString();
    
                                                        if(combo_item.toLowerCase().startsWith(text.toLowerCase()))
                                                        {	System.out.println("inputlen: " + text.length() + " - " + combo_item + ": " + combo_item.length());
                                                        combo.setSelectedIndex(i);
                                                        tf.setText(combo_item);
                                                                tf.setSelectionColor(color);
                                                                tf.setSelectedTextColor(Color.WHITE);
                                                                tf.setSelectionStart(text.length());
                                                                tf.setSelectionEnd(tf.getText().length());
    
                                                                break;
                                                        }
                                                }
                                }
                        }
                }//end of cmbSelectInput
    
                public static void main(String[] args)
                {	new JComboTrial().setVisible(true);
                }
    
        }
    Thanks again,
    geje

Similar Threads

  1. Replies: 2
    Last Post: 12-10-2009, 02:58 PM
  2. How at save list in Jcombobox...
    By pra.deep in forum AWT / Swing
    Replies: 1
    Last Post: 10-29-2009, 06:57 AM
  3. JComboBox and drop-down list.....HELP!!!!!
    By Anna in forum AWT / Swing
    Replies: 1
    Last Post: 06-18-2008, 06:16 AM
  4. vector list search
    By hezfast2 in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 07:48 PM
  5. How to Search a List in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •