2009-05-01 58 views
3

我正在研究验证可编辑JComboBox输入的各种方法。目前,我需要将输入限制在指定范围内的数字。到目前为止,我发现了三种不同的方式。任何想法来解决这个问题的最佳方式?Java - 可编辑组合框验证

​​
  1. 通过实施一个专门的文献重写方法insertString和除去越过用户输入控制。

    // get the combo boxes editor component 
    JTextComponent editor = 
         (JTextComponent) comboBox.getEditor().getEditorComponent(); 
    // change the editor's document 
    editor.setDocument(new BadDocument()) 
    
  2. 用JFormattedTextField替换JComboBox的JTextField。

  3. 您可以使用输入验证作为替代自定义格式

    // set the input verifier 
    setInputVerifier(verifier); 
    
    class MyVerifier extends InputVerifier implements ActionListener 
    { 
        public boolean shouldYieldFocus(JComponent input) {} 
    } 
    

感谢。

回答

2

这就是InputVerifier的设计目的。我会从其中一个开始,但真的应该这样做。您的要求有什么特别的原因,为什么它不起作用?

+0

没有理由。刚刚注意到大量的选项,并希望更好地了解目前的最佳做法。 – javacavaj 2009-05-01 20:09:10