Results 1 to 4 of 4
Thread: JComboBox search for match list
- 07-07-2010, 06:46 AM #1
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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:
Thanks in advance,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); } }
geje
- 07-07-2010, 01:03 PM #2
click the below link it explains there
KeyListener in JComboBox « Chad.SteverTeaching myself java so that i can eventually join the industry! Started in June 2010
- 07-07-2010, 01:04 PM #3
Are these incompatible? Who is to get the key strokes, the JComboBox or your listener?JComboBox is set to editable. JComboBox' KeyListener does not work
What happens if its not editable?
- 07-08-2010, 08:48 AM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
KeyListener works as expected if JCombobox is not editable. But I have to set editable to true becauseWhat happens if its not editable?
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.
Thanks again,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); } }
geje
Similar Threads
-
Activate JComboBox 1 when object is selected in JComboBox 2...
By bahumbaba in forum AWT / SwingReplies: 2Last Post: 12-10-2009, 01:58 PM -
How at save list in Jcombobox...
By pra.deep in forum AWT / SwingReplies: 1Last Post: 10-29-2009, 05:57 AM -
JComboBox and drop-down list.....HELP!!!!!
By Anna in forum AWT / SwingReplies: 1Last Post: 06-18-2008, 05:16 AM -
vector list search
By hezfast2 in forum New To JavaReplies: 2Last Post: 06-14-2008, 06:48 PM -
How to Search a List in Java
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:38 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks