浏览器的Swing地址栏一般带有输入网址的记忆功能,输入首字母,就会出现以它开头的所有曾使用记录。在swing中也能很容易的实现这个功能。
对于这个功能,可以分解成几个步骤:输入-->响应并弹出提示-->选择或继续输入。为防止重复的保存,直接用Set保存所有输入。显示提示的组件可以用JList外面套上1个JWindow.再加上鼠标响应和输入响应,基本就完成了。
用户的所有输入由addCompletion()方法加入到Set中去,这个动作可以由CompletableJTextField上触发Enter快捷键响应,或者由其他的自定义动作实现,取决于你的需求。用户无论输入或者删除一个字母,后台都会根据输入匹配Set中保存的数据,然后将所有匹配条目放到 Jlist中由JWindow显示出来。
如果要看起来更好看,可以在JWindow上setBorder(xx),比如设置一个带阴影层次效果的setBorder(roundedShadowBorder);
如果要更精细一些,可考虑为JList添加上移、下移和回车事件响应,这样就跟浏览器的Swing地址栏完全一样了。
publicclassCompletableJTextFieldextendsJTextFieldimplements
ListSelectionListener{
privatestaticfinallongserialVersionUID=1L;
JListcompletionList;
DefaultListModelcompletionListModel;
JScrollPanelistScroller;
JWindowlistWindow;
Set<String>completions;
publicCompletableJTextField(intcol){
super(col);
getDocument().addDocumentListener(newCompleter());
completionListModel=newDefaultListModel();
completionList=newJList(completionListModel);
completionList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
completionList.addListSelectionListener(this);
listScroller=newJScrollPane(completionList,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
listWindow=newJWindow();
listWindow.getContentPane().add(listScroller);
}
publicvoidaddCompletion(Strings){
completions.add(s);
}
publicvoidremoveCompletion(Strings){
completions.remove(s);
}
publicvoidclearCompletions(){
completions.clear();
listWindow.setVisible(false);
}
publicvoidvalueChanged(ListSelectionEvente){
if(completionList.getModel().getSize()==0){
return;
}
listWindow.setVisible(false);
finalStringcompletionString=(String)completionList
.getSelectedValue();
SwingUtilities.invokeLater(newRunnable(){
publicvoidrun(){
if(null!=completionString){
setText(completionString);
}
}});
}
/**
*@returnthecompletions
*/
publicSet<String>getCompletions(){
returncompletions;
}
/**
*@paramcompletionsthecompletionstoset
*/
publicvoidsetCompletions(Set<String>completions){
this.completions=completions;
}
classCompleterimplementsDocumentListener{
privatePatternpattern;
privatevoidbuildPopup(){
completionListModel.clear();
Iterator<String>it=completions.iterator();
pattern=Pattern.compile(getText()+".+");
while(it.hasNext()){
Stringcompletion=it.next();
Matchermatcher=pattern.matcher(completion);
if(matcher.matches()){
completionListModel.add(completionListModel.getSize(),
completion);
}
}
}
privatevoidshowPopup(){
if(completionListModel.getSize()==0){
listWindow.setVisible(false);
return;
}
Pointlos=getLocationOnScreen();
intpopX=los.x;
intpopY=los.y+getHeight();
listWindow.setLocation(popX,popY);
listWindow.pack();
listWindow.setVisible(true);
}
privatevoidbuildAndShowPopup(){
if(getText().length()<1)
return;
buildPopup();
showPopup();
}
publicvoidinsertUpdate(DocumentEvente){
buildAndShowPopup();
}
publicvoidremoveUpdate(DocumentEvente){
buildAndShowPopup();
}
publicvoidchangedUpdate(DocumentEvente){
buildAndShowPopup();
}
}
}
- 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.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
以上是介绍浏览器的Swing地址栏,希望对大家有用。
【编辑推荐】