博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
swt combo 自动补全
阅读量:4320 次
发布时间:2019-06-06

本文共 1845 字,大约阅读时间需要 6 分钟。

public
class
AutoCompleteComboMain {
    
static
final
Display display =
new
Display();
    
static
final
Shell shell =
new
Shell(display);
    
static
String[] items =
new
String[] {
"Monday"
,
"Tuesday"
,
"Wednesday"
,
"Thursday"
,
"Friday"
,
"Saturday"
,
"Sunday"
};
 
    
public
static
void
main(String[] args) {
        
shell.setText(
"SWT"
);
        
shell.setLayout(
new
GridLayout());
 
        
Combo combo =
new
Combo(shell, SWT.BORDER);
        
for
(
int
i =
0
; i < items.length; i++) {
            
combo.add(items[i]);
        
}
        
ComboUtil.addAutoCompleteFeature(combo);
       
        
shell.pack();
        
shell.open();
        
while
(!shell.isDisposed()) {
            
if
(!display.readAndDispatch()) {
                
display.sleep();
            
}
        
}
        
display.dispose();
    
}
}

 

关键的ComboUtil的代码如下:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public
class
ComboUtil {
    
public
static
void
addAutoCompleteFeature(Combo combo) {
        
// Add a key listener
        
combo.addKeyListener(
new
KeyAdapter() {
            
public
void
keyReleased(KeyEvent keyEvent) {
                
Combo cmb = ((Combo) keyEvent.getSource());
                
setClosestMatch(cmb);
            
}
 
            
// Move the highlight back by one character for backspace
            
public
void
keyPressed(KeyEvent keyEvent) {
                
if
(keyEvent.keyCode == SWT.BS) {
                    
Combo cmb = ((Combo) keyEvent.getSource());
                    
Point pt = cmb.getSelection();
                    
cmb.setSelection(
new
Point(Math.max(
0
, pt.x -
1
), pt.y));
                
}
            
}
 
            
private
void
setClosestMatch(Combo combo) {
                
String str = combo.getText();
                
String[] cItems = combo.getItems();
                
// Find Item in Combo Items. If full match returns index
                
int
index = -
1
;
                
for
(
int
i =
0
; i < cItems.length; i++) {
                    
if
(cItems[i].toLowerCase().startsWith(str.toLowerCase())) {
                        
index = i;
                        
break
;
                    
}
                
}
 
                
if
(index != -
1
) {
                    
Point pt = combo.getSelection();
                    
combo.select(index);
                    
combo.setText(cItems[index]);
                    
combo.setSelection(
new
Point(pt.x, cItems[index].length()));
                
}
            
}
        
});
    
}

转载于:https://www.cnblogs.com/zyszeal/p/4461288.html

你可能感兴趣的文章
mac 无法打开xx ,因为无法确认开发者身份
查看>>
简单的排序算法(冒泡、选择、插入)
查看>>
[剑指Offer] 11.二进制中1的个数
查看>>
重置报表输出选择
查看>>
ip代理池抓取qq音乐热歌前300
查看>>
Android面试题集合
查看>>
Android NDK开发
查看>>
Centos中安装和配置vsftp简明教程
查看>>
spring源码学习之AOP(一)
查看>>
AES加密算法动画演示
查看>>
三种方法实现调用Restful接口
查看>>
php第五节(字符串函数和时间、日期函数)
查看>>
magento主页限制某个目录的产品显示数量
查看>>
SpringBoot整合Netty
查看>>
MongoDB数据库的基本操作
查看>>
PAT乙级1014
查看>>
ORACLE wm_concat自定义
查看>>
[Zend PHP5 Cerification] Lectures -- 6. Database and SQL
查看>>
[Drupal] Using the Administrator theme whenever you want.
查看>>
【Hibernate框架】关联映射(一对一关联映射)
查看>>