2008-09-10 98 views

回答

8

我找到了解决方法。我想我会让有这个问题的下一个人知道。

基本上。不是在ComboBox上设置inputVerifier,而是将其设置为“Editor Component”。

JComboBox combo = new JComboBox(); 
JTextField tf = (JTextField)(combo.getEditor().getEditorComponent()); 
tf.setInputVerifier(verifyer); 
1

向我们展示一小段代码。

package inputverifier; 

import javax.swing.*; 

    class Go { 
    public static void main(String[] args) { 
     java.awt.EventQueue.invokeLater(new Runnable() { public void run() { 
      runEDT(); 
     }}); 
    } 
    private static void runEDT() { 
     new JFrame("combo thing") {{ 
      setLayout(new java.awt.GridLayout(2, 1)); 
      add(new JComboBox() {{ 
       setEditable(true); 
       setInputVerifier(new InputVerifier() { 
        @Override public boolean verify(JComponent input) { 
         System.err.println("Hi!"); 
         return true; 
        } 
       }); 
      }}); 
      add(new JTextField()); 
      setDefaultCloseOperation(EXIT_ON_CLOSE); 
      pack(); 
      setVisible(true); 
     }}; 
    }  
} 

Looks like it's a problem with JComboBox being a composite component.我建议避免这种讨厌的UI解决方案。

相关问题