JComboBox 实现类似百度 的过滤式 搜索 匹配内容 以及选中后事件监听
final JComboBox comboBox = new JComboBox ( ) ;
comboBox. setEditable ( true ) ;
final List< String> bill = new ArrayList < > ( ) ;
for ( BillTable billTable : list) {
bill. add ( billTable. getName ( ) ) ;
model. addElement ( billTable. getName ( ) ) ;
}
comboBox. setModel ( model) ;
comboBox. addItemListener ( new ItemListener ( ) {
public void itemStateChanged ( ItemEvent evt) {
if ( evt. getStateChange ( ) == 1 ) {
}
}
} ) ;
comboBox. getEditor ( ) . getEditorComponent ( ) . addKeyListener ( new KeyListener ( ) {
public void keyTyped ( KeyEvent e) {
comboBox. setPopupVisible ( true ) ;
}
public void keyPressed ( KeyEvent e) {
String str = comboBox. getEditor ( ) . getItem ( ) . toString ( ) ;
model. removeAllElements ( ) ;
for ( int i = 0 ; i < array. length ; i ++ ) {
if ( ( ( String) array[ i] ) . indexOf ( str) >= 0 ) {
model. addElement ( array[ i] ) ;
}
}
comboBox. getEditor ( ) . setItem ( str) ;
}
public void keyReleased ( KeyEvent e) {
}
} ) ;
comboBox. setBounds ( 133 , 60 , 115 , 23 ) ;
contentPane. add ( comboBox) ;