浏览器的Swing地址栏

开发 后端
本文介绍浏览器的Swing地址栏一般带有输入网址的记忆功能,输入首字母,就会出现以它开头的所有曾使用记录。

浏览器的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地址栏,希望对大家有用。

【编辑推荐】

  1. 在表格中Swing增加列表框
  2. 浅谈Swing控件JList
  3. 概述Swing组件与外部线程
  4. Java Swing做什么好
  5. Swing文件选择器的制作
责任编辑:佚名 来源: 电子工业出版社
相关推荐

2015-12-01 10:43:55

2011-11-04 15:28:49

傲游浏览器

2011-05-20 17:23:41

Chrome 13

2016-10-18 14:22:41

2020-10-21 11:48:22

欺骗漏洞

2010-08-26 17:54:16

微软

2010-08-27 09:47:07

谷歌

2012-08-05 17:13:47

傲游

2021-05-27 20:46:22

浏览器地址栏谷歌

2017-01-03 20:13:02

2013-11-27 15:38:14

IE浏览器故障

2023-02-02 16:35:36

微软Edge浏览器

2023-01-27 11:01:54

谷歌Chrome浏览器

2020-10-26 09:56:40

恶意攻击手机浏览器地址栏欺骗

2024-04-11 08:33:25

2011-02-25 09:03:03

Chrome

2009-08-06 17:34:27

地址栏控件C#记忆功能

2011-06-28 09:23:22

Firefox地址栏

2009-03-30 08:58:52

Firefox浏览器

2011-02-21 14:10:50

Chrome
点赞
收藏

51CTO技术栈公众号