TWaver - 專注UI技術

          http://twaver.servasoft.com/
          posts - 171, comments - 191, trackbacks - 0, articles - 2
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理
          讓JTextField添加“自動完成”功能 一文中收到了許多user 的反饋,其中有user提到需要能夠在下拉列表中同時顯示文字和圖片。要實現下拉列表的這種功能,不用太難,給ComboBox設置一個Renderer 即可,ComboBox的Renderer默認是一個JLabel,而JLabel 本身就有設置Icon的功能,所以直接用默認的Renderer,并設置上圖片即可,代碼如下:
           1 cbInput.setRenderer(new DefaultListCellRenderer(){
           2     public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
           3         super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
           4         if(value != null){
           5             this.setText(value.toString());
           6             String url = value.toString().replaceAll(" ", "_") + ".png";
           7             ImageIcon icon = getImageIcon(url, IMAGE_ICON_CACHE);
           8             if(icon != null){
           9                 this.setIcon(icon);
          10             }
          11         }
          12         return this;
          13     }
          14 });

          在上面的代碼中,通過文字去查找對應的圖片,并設置Renderer 的Icon,其中getImageIcon方法如下,緩存了圖片資源,這樣不用每次刷新的時候去每次生成ImageIcon:
           1 public static ImageIcon getImageIcon(String url,Map IMAGE_ICON_CACHE) {
           2         if (url == null) {
           3             return null;
           4         }
           5         if (IMAGE_ICON_CACHE.get(url) == null) {
           6             ImageIcon image = null;
           7             InputStream in =ResourceAgent.class.getResourceAsStream(url);
           8             if (in != null) {
           9                 try {
          10                     byte buffer[] = new byte[in.available()];
          11                     for (int i = 0, n = in.available(); i < n; i++) {
          12                         buffer[i] = (bytein.read();
          13                     }
          14                     Toolkit toolkit = Toolkit.getDefaultToolkit();
          15                     Image img = toolkit.createImage(buffer);
          16                     image = new ImageIcon(img);
          17                     in.close();
          18                 } catch (IOException ex) {
          19                     ex.printStackTrace();
          20                     return null;
          21                 }
          22             }
          23             if (image == null) {
          24                 if (ClassLoader.getSystemResource(url) != null) {
          25                     image = new ImageIcon(ClassLoader.getSystemResource(url));
          26                 } else {
          27                     image = new ImageIcon(url);
          28                 }
          29             }
          30             if (image == null) {
          31                 System.err.println("can't load image '" + url + "'");
          32             } else {
          33                 IMAGE_ICON_CACHE.put(url, image);
          34             }
          35         }
          36         return (ImageIcon) IMAGE_ICON_CACHE.get(url);
          37     }
          38 
          效果如下:

          全部代碼: Source Code

          評論

          # re: 讓JTextField添加“自動完成”功能(續)  回復  更多評論   

          2013-07-25 15:52 by 研究報告
          這篇文章好簡短 不過還不錯

          # re: 讓JTextField添加“自動完成”功能(續)  回復  更多評論   

          2013-07-26 17:36 by 行業報告
          博主好文 講的很不錯 謝謝博主分享

          # re: 讓JTextField添加“自動完成”功能(續)  回復  更多評論   

          2013-12-30 10:25 by 渣漿泵
          要實現下拉列表的這種功能,不用太難,給ComboBox設置一個Renderer 即可

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 绥芬河市| 漳州市| 张家口市| 遂宁市| 固安县| 南城县| 牟定县| 星座| 湘西| 二连浩特市| 鲁甸县| 巴楚县| 称多县| 贡觉县| 桃江县| 武宁县| 土默特左旗| 涟水县| 榆社县| 五指山市| 宁化县| 宣化县| 合阳县| 扬州市| 永宁县| 平潭县| 安塞县| 和平区| 茌平县| 互助| 浦北县| 梨树县| 井冈山市| 大理市| 高雄县| 海林市| 仙游县| 永定县| 海口市| 安化县| 兖州市|