Ver Mensaje Individual
  #2 (permalink)  
Antiguo 29/03/2011, 12:28
Avatar de sivadmp
sivadmp
 
Fecha de Ingreso: febrero-2011
Ubicación: La Paz - Bolivia
Mensajes: 293
Antigüedad: 13 años, 2 meses
Puntos: 20
Respuesta: Problema con jcombobox

hola mijaya...

para resolver tu problema tienes que utilizar la Clase KeySelectionManager.

este codigo que hice, te puede ayudar

Código java:
Ver original
  1. public class DmComboBox extends JComboBox {
  2.     private static final long serialVersionUID = 1L;
  3.  
  4.     public DmComboBox() {
  5.  
  6.         setKeySelectionManager(new MyKeySelectionManager());
  7.         this.setLocation(100, 100);
  8.     }
  9.  
  10.     class MyKeySelectionManager implements JComboBox.KeySelectionManager {
  11.         long lastKeyTime = 0;
  12.         String pattern = "";
  13.  
  14.         public int selectionForKey(char aKey, ComboBoxModel model) {
  15.             int selIx = 01;
  16.             Object sel = model.getSelectedItem();
  17.             if (sel != null) {
  18.                 for (int i = 0; i < model.getSize(); i++) {
  19.                     if (sel.equals(model.getElementAt(i))) {
  20.                         selIx = i;
  21.                         break;
  22.                     }
  23.                 }
  24.             }
  25.  
  26.             long curTime = System.currentTimeMillis();
  27.  
  28.             if (curTime - lastKeyTime < 300) {
  29.                 pattern += ("" + aKey).toLowerCase();
  30.             } else {
  31.                 pattern = ("" + aKey).toLowerCase();
  32.             }
  33.  
  34.             lastKeyTime = curTime;
  35.  
  36.             for (int i = selIx + 1; i < model.getSize(); i++) {
  37.                 String s = model.getElementAt(i).toString().toLowerCase();
  38.                 if (s.startsWith(pattern)) {
  39.                     return i;
  40.                 }
  41.             }
  42.  
  43.             for (int i = 0; i < selIx; i++) {
  44.                 if (model.getElementAt(i) != null) {
  45.                     String s = model.getElementAt(i).toString().toLowerCase();
  46.                     if (s.startsWith(pattern)) {
  47.                         return i;
  48.                     }
  49.                 }
  50.             }
  51.             return -1;
  52.         }
  53.     }
  54.  
  55.     public static void main(String[] args) {
  56.         JFrame fr = new JFrame();
  57.         DmComboBox dm = new DmComboBox();
  58.         dm.addItem("Beatriz");
  59.         dm.addItem("Ana");
  60.         dm.addItem("Vanessa");
  61.         dm.addItem("Janeth");
  62.         dm.addItem("Yola");
  63.         dm.addItem("Silvia");
  64.         dm.addItem("Jhovana");
  65.         dm.addItem("Lizet");
  66.         fr.add(dm, BorderLayout.CENTER);
  67.         fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  68.         fr.setSize(300, 60);
  69.         fr.setVisible(true);
  70.     }
  71. }


espero haberte ayudado, saludos
__________________
agradecer no cuesta nada
Entre más información proporciones más fácil será ayudarte, y asi evitar adivinar el problema con el que cuentas.