JCombox模糊匹配。点击Item,鼠标事件为什么没用?
/**
* 模糊匹配的Jcombox控件
* 2012-9-19 下午04:18:57
*/
public class AutoFillCombox extends JComboBox
{
// 可选项
private ArrayList<String>items= null;
public AutoFillCombox(ArrayList<String> items)
{
super();
if(items == null)
{
items = new ArrayList<String>();
}
this.items = items;
setupAutoComplete(items);
}
// 是否调整
private boolean isAdjusting(JComboBox cbInput)
{
if(cbInput.getClientProperty("is_adjusting") instanceof Boolean)
{
return (Boolean) cbInput.getClientProperty("is_adjusting");
}
return false;
}
/**
* @param cbInput
* 设置调整
* @param adjusting void
*/
private void setAdjusting(JComboBox cbInput,boolean adjusting)
{
cbInput.putClientProperty("is_adjusting", adjusting);
}
public void setupAutoComplete(final ArrayList<String> items)
{
final DefaultComboBoxModel model = new DefaultComboBoxModel();
setModel(model);
setEditable(true);
setAdjusting(this, false);
setSelectedItem(null);
final JTextField txtInput = (JTextField) getEditor().getEditorComponent();
this.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if( !isAdjusting(AutoFillCombox.this))
{
if(AutoFillCombox.this.getSelectedItem() != null)
{
txtInput.setText(AutoFillCombox.this.getSelectedItem().toString());
}
}
}
});
// 支持键盘事件
txtInput.addKeyListener(new KeyAdapter()
{
@Override
public void keyPressed(KeyEvent e)
{
setAdjusting(AutoFillCombox.this, true);
if(e.getKeyCode() == KeyEvent.VK_SPACE)
{
if(AutoFillCombox.this.isPopupVisible())
{
e.setKeyCode(KeyEvent.VK_ENTER);
}
}
if(e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN)
{
e.setSource(AutoFillCombox.this);
AutoFillCombox.this.dispatchEvent(e);
if(e.getKeyCode() == KeyEvent.VK_ENTER)
{
if(AutoFillCombox.this.getSelectedItem() != null)
{
txtInput.setText(AutoFillCombox.this.getSelectedItem().toString());
}
AutoFillCombox.this.setPopupVisible(false);
}
}
if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
{
AutoFillCombox.this.setPopupVisible(false);
}
setAdjusting(AutoFillCombox.this, false);
}
});
txtInput.getDocument().addDocumentListener(new DocumentListener()
{
public void insertUpdate(DocumentEvent e)
{
updateList();
}
public void removeUpdate(DocumentEvent e)
{
updateList();
}
public void changedUpdate(DocumentEvent e)
{
updateList();
}
private void updateList()
{
setAdjusting(AutoFillCombox.this, true);
AutoFillCombox.this.setPopupVisible(false);
try
{
model.removeAllElements();
}catch(Exception e)
{
// TODO: handle exception
}
String input = txtInput.getText().toString();
if( !input.isEmpty())
{
for(String item:items)
{
if(item.toLowerCase().contains(input.toLowerCase()))
{
try
{
model.addElement(item);
}catch(Exception e)
{
// TODO: handle exception
}
}
}
}
AutoFillCombox.this.setPopupVisible(model.getSize() > 0);
setAdjusting(AutoFillCombox.this, false);
}
});
}
public static void main(String args[])
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 默认打开时最大
frame.setExtendedState(Frame.NORMAL);
frame.setMinimumSize(new Dimension(400, 300));
frame.setLocationRelativeTo(null);
frame.setLayout(new GridBagLayout());
ArrayList<String> arrylist = new ArrayList<String>();
arrylist.add("123");
arrylist.add("234");
arrylist.add("289");
arrylist.add("456");
arrylist.add("567");
arrylist.add("789");
AutoFillCombox autoFillCombox = new AutoFillCombox(arrylist);
frame.add(autoFillCombox, (new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)));
frame.setVisible(true);
}
}
[解决办法]
事件实现这个接口ItemListener就可以了。@Override
public void itemStateChanged(ItemEvent e) {
// 当下拉框的值改变的时候会触发这个事件
if (e.getStateChange() == ItemEvent.SELECTED) {
值改变后把处理代码放这里
}
}