2017-08-01 1 views
0

J'essaye de faire un jComoBox qui permet toutes les entrées sauf les chiffres. Mais quand je l'essaie avec jComoBox ça ne marche pas.caractères d'entrée jComoBox seulement (pas de chiffres)

Je l'ai fait avec succès jTextFiled (mais le oppsite- pas de chiffres):

Code de l'événement TimeKeyTyped pour i_borow jTextFiled:

private void i_borowTimeKeyTyped(java.awt.event.KeyEvent evt) {          
    char c = evt.getKeyChar(); 
    if(!(Character.isDigit(c) || c == KeyEvent.VK_BACK_SPACE || c == KeyEvent.VK_DELETE)) { 
     evt.consume(); 
     l_msg2.setForeground(Color.red); 
    } else { 
     l_msg2.setForeground(Color.black); 
    } 
} 

J'ai essayé de faire la même chose à la jComoBox (c_title) :

private void c_titleKeyTyped(java.awt.event.KeyEvent evt) {         
    System.out.println("ssss"); 
    char c = evt.getKeyChar(); 
    System.out.println(c); 
    if(Character.isDigit(c)){ 
     evt.consume(); 
     l_noNum.setForeground(Color.red); 
    } else { 
     l_noNum.setForeground(Color.black); 
    } 
} 

Le code ne fonctionne pas. De plus, cela n'imprime pas la chaîne "ssss". Pourquoi ça ne marche pas sur jComboBox? merci.

Répondre

3

Voici un exemple de la manière de fournir la vérification dont vous avez besoin pour une zone de liste modifiable. La même approche que vous pouvez également utiliser pour un champ de texte (c'est mieux que d'utiliser des écouteurs). Cette approche fonctionne également lorsque l'utilisateur colle du texte dans une zone de liste déroulante.

import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 
import javax.swing.WindowConstants; 
import javax.swing.plaf.basic.BasicComboBoxEditor; 
import javax.swing.text.AbstractDocument; 
import javax.swing.text.AttributeSet; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.DocumentFilter; 

public class FilterTryout { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       JFrame frm = new JFrame("Combo test"); 
       JComboBox<String> combo = new JComboBox<>(new String[] {"One", "Two", "Three"}); 
       combo.setEditor(new ComboEditor()); 
       combo.setEditable(true); 
       frm.add(combo); 
       frm.pack(); 
       frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
       frm.setLocationRelativeTo(null); 
       frm.setVisible(true); 
      } 
     }); 
    } 

    private static class ComboEditor extends BasicComboBoxEditor { 
     @Override 
     protected JTextField createEditorComponent() { 
      JTextField fld = super.createEditorComponent(); 
      ((AbstractDocument) fld.getDocument()).setDocumentFilter(new NoDigitsFilter()); 
      return fld; 
     } 
    } 

    private static class NoDigitsFilter extends DocumentFilter { 
     @Override 
     public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { 
      if (isNoDigits(string)) { 
       super.insertString(fb, offset, string, attr); 
      } 
     } 

     @Override 
     public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { 
      if (isNoDigits(text)) { 
       super.replace(fb, offset, length, text, attrs); 
      } 
     } 

     private boolean isNoDigits(String text) { 
      boolean noDigits = true; 
      for (int i = 0; i < text.length() && noDigits; i++) { 
       noDigits = !Character.isDigit(text.charAt(i)); 
      } 
      return noDigits; 
     } 
    } 
} 
+0

Merci, ça marche. Mais il y a une erreur: Exception dans le fil "AWT-EventQueue-0" java.lang.IllegalArgumentException: mauvaise position: 1 –

+0

@DocLevi Fonctionne bien pour moi. Comment puis-je reproduire cette exception? –

+0

J'utilise: AutoCompleteDecorator.decorate (JComboBox) aussi. peut-être que c'est la cause? –