如何在JComboBox.java中修改selectWithKeyChar()的函数?

如何在JComboBox.java中修改selectWithKeyChar()的函数?

问题描述:

我有一个组合框,其中包含以特殊字符(即。À)开头的项目。如何在JComboBox.java中修改selectWithKeyChar()的函数?

当前,使用组合框时,如果按下某个键(即“a”),它会选择以相同的第一个字母开头的项目 - 但不会选择以特殊项目开头的项目字符..

有没有办法修改JComboBox.java中的方法selectWithKeyChar()来忽略特殊字符并将其视为普通字符?

解决方案我已经想到了,但不知道如何实现:

1)在临时组合框模型selectWithKeyChar()不包括口音传递。如果这是可能的,你如何将模型传递给selectWithKeyChar()?

2)覆盖selectWithKeyChar()方法?

3)制作自定义方法。在这种情况下,如何让它运行而不是JComboBox.java中已经存在的那个?

这是我设置的一个小例子:

  • 创建一个新项目
  • 创建具有一个JPanel表单文件类型
  • 添加组合框与下列项目的新文件:“阿普尔比”, “测试”, “能”
  • 呼叫的JPanel主:

    SwingUtilities.invokeLater(new Runnable(){ 
         @Override 
         public void run(){ 
          JFrame frame = new JFrame(); 
          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
          frame.getContentPane().add(new B()); 
          frame.pack(); 
          frame.setVisible(true); 
         } 
        }); 
    

1)传输到一个临时组合框模型selectWithKeyChar(),该 不包括口音。如果这是可能的,你如何通过 模型selectWithKeyChar()?

2)覆盖selectWithKeyChar()方法?

3)制作自定义方法。在这种情况下,如何让它运行 而不是JComboBox.java中已经存在的那个?

所有这一切都是可能的,你可以创建一个tempComoBoxModel,你extends的JComboBox在您CustomComboBox覆盖selectWithKeyCharacter。下面是一个代码示例:

CustomComboBox.java

public class CustomComboBox extends JComboBox<String>{ 

private static final long serialVersionUID = 1L; 

public CustomComboBox(String[] items) { 
    super(items); 
} 

@Override 
public boolean selectWithKeyChar(char keyChar) { 
    int index; 

    ComboBoxModel<String> tempModel = getModel(); // Put the model on temp variable for saving it for later use. 

    String[] itemsArr = new String[getModel().getSize()]; 
    for(int i = 0; i < getModel().getSize(); i++) { 
     // This will normalizes the Strings 
     String normalize = Normalizer.normalize(getModel().getElementAt(i), Normalizer.Form.NFD); 
     normalize = normalize.replaceAll("\\p{M}", ""); 
     itemsArr[i] = normalize; 
    } 

    if (keySelectionManager == null) 
     keySelectionManager = createDefaultKeySelectionManager(); 

    setModel(new DefaultComboBoxModel<String>(itemsArr)); // Set the Temporary items to be checked. 
    index = keySelectionManager.selectionForKey(keyChar,getModel()); 
    setModel(tempModel); // Set back the original items. 

    System.out.println(index); 
    if (index != -1) { 
     setSelectedIndex(index); 
     return true; 
    } 
    else 
     return false; 
} 
} 

如果你将测试它,即使你进入正常字符“A”,它会在与JComboBox的口音选择元素或与非重音。

TestMain。java

public class TestMain { 
public static void main(String[] args) { 
    String[] lists = {"ápa","Zabra","Prime"}; 
    CustomComboBox combo = new CustomComboBox(lists); 
    System.out.println(combo.selectWithKeyChar('A')); 

    JFrame frame = new JFrame("Sample"); 
    frame.setSize(400, 400); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLayout(null); 
    frame.setVisible(true); 
    combo.setSize(70, 30); 
    combo.setLocation(100, 100); 
    frame.add(combo); 
} 
} 
+0

嗨,我试过这个,但它不能正常工作,如果有多个项目具有相同的第一个字母。为什么每次按下按钮时都会关闭组合框?感谢您的回应 – Confucius