瀏覽器的Swing地址欄
瀏覽器的Swing地址欄一般帶有輸入網(wǎng)址的記憶功能,輸入首字母,就會出現(xiàn)以它開頭的所有曾使用記錄。在swing中也能很容易的實現(xiàn)這個功能。
對于這個功能,可以分解成幾個步驟:輸入-->響應并彈出提示-->選擇或繼續(xù)輸入。為防止重復的保存,直接用Set保存所有輸入。顯示提示的組件可以用JList外面套上1個JWindow.再加上鼠標響應和輸入響應,基本就完成了。
用戶的所有輸入由addCompletion()方法加入到Set中去,這個動作可以由CompletableJTextField上觸發(fā)Enter快捷鍵響應,或者由其他的自定義動作實現(xiàn),取決于你的需求。用戶無論輸入或者刪除一個字母,后臺都會根據(jù)輸入匹配Set中保存的數(shù)據(jù),然后將所有匹配條目放到 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();
- }
- }
- }
以上是介紹瀏覽器的Swing地址欄,希望對大家有用。
【編輯推薦】