2009-07-03 74 views
1

我想創建JComboBox控件與Firefox的URL文本框類似。有誰知道如何自定義JComboBox的文本框。我想補充的ALIGN.HORIZONTAL_RIGHT一些圖標附近JComboBox如何在JComboBox的箭頭圖標附近添加圖標


感謝的箭頭按鈕爲您一絲不苟地解釋。事實上,我將結合DefaultListCellRenderer和圖標添加到組合框像下面的代碼

import java.awt.Dimension; 
import java.awt.Insets; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 

public class Main extends JFrame { 
    public Main() { 
     // Create icon "C" 
     JButton jb = new JButton("C"); 
     jb.setMargin(new Insets(0, 0, 0, 0)); 
     jb.setBounds(245, 2, 18, 18); 

     // Create combo box 
     String[] languages = new String[]{"Java", "C#", "PHP"}; 
     JComboBox jc = new JComboBox(languages); 
     // jc.setEditable(true); 
     jc.add(jb); 

     getContentPane().add(jc); 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(new Dimension(300, 58)); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     final Main main = new Main(); 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       main.setVisible(true); 
      } 
     }); 
    } 
} 

但是當我把jc.setEditable(true);組合編輯器隱藏了我的圖標。我想另一種方式來模擬Firefox真棒吧。你有什麼想法嗎?

回答

2

這裏完成的例子,證明它:

package com.demo.combo.icon; 



import java.awt.BorderLayout; 
import java.awt.Component; 
import java.awt.Dimension; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.util.HashMap; 
import java.util.Map; 

import javax.swing.DefaultListCellRenderer; 
import javax.swing.Icon; 
import javax.swing.ImageIcon; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JList; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 


public class ShowConboWithIcons extends JFrame { 

private static final long serialVersionUID = 1L; 

private static final ImageIcon INFO_ICON = new ImageIcon("info.png"); 
private static final ImageIcon NONE_ICON = new ImageIcon("none.png"); 
public final String NONE_STR = "None"; 
private final String INFO_STR = "Info"; 

private JComboBox comboBox; 
private JPanel topPanel; 

private String[] str_arr = null; 


public ShowConboWithIcons(String[] str_arr) { 
    this.str_arr = str_arr;  
} 


public void createGUI(){ 

    setMinimumSize(new Dimension(100,100)); 
    setTitle("Demo"); 
    setLocation(200, 200); 

    topPanel = new JPanel(); 
    getContentPane().add(topPanel, BorderLayout.CENTER); 

    Map<Object, Icon> icons = new HashMap<Object, Icon>(); 

    icons.put(NONE_STR, NONE_ICON); 
    icons.put(INFO_STR, INFO_ICON); 

    comboBox = new JComboBox(); 
    comboBox.setRenderer(new IconListRenderer(icons)); 
    comboBox.addItem("None"); 

    for(String val : str_arr){ 
     comboBox.addItem(val); 
    } 

    topPanel.add(comboBox); 

    super.addWindowListener(new WindowAdapter() {   
     public void windowClosing(WindowEvent e) {    
      dispose();   
     }   
    }); 
} 

public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException { 

    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 

    String[] str_arr = {"A", "B", "C", "D", "E"}; 

    ShowConboWithIcons T = new ShowConboWithIcons(str_arr); 
    T.createGUI(); 
    T.setVisible(true);   
} 


class IconListRenderer extends DefaultListCellRenderer{ 
    private static final long serialVersionUID = 1L; 
    private Map<Object, Icon> icons = null; 

    public IconListRenderer(Map<Object, Icon> icons){ 
     this.icons = icons; 
    } 

    @Override 
    public Component getListCellRendererComponent(JList list, Object value, int index,boolean isSelected, boolean cellHasFocus) 
    { 
     JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 

     // Get icon to use for the list item value 
     Icon icon = icons.get(value); 

     if(!value.toString().equals(NONE_STR)){ 
      icon = icons.get(INFO_STR); 
     } 

     // Set icon to display for value 
     label.setIcon(icon); 
     return label; 
    } 
} 
} 

前瞻:

combo default mode:

combo select item:

5

要更改組件的渲染方式,您通常使用所謂的Renderer s。

對於組合框,請看如何創建一個custom combobox renderer。只需快速瀏覽一下,但對於您的情況,DefaultListCellRenderer的簡單配置可能已足夠,因爲您可以設置JLabel屬性以將文本定位到圖像。如果沒有,只是從它延伸。

還請記住,您必須設置一個包含圖標的模型,以便組合框渲染器可以獲取它 - 也可能需要執行自定義ComboBoxModel,具體取決於您的數據對象。

+0

嗨aberrant80, 感謝您的快速答覆。我檢出了DefaultListCellRenderer,這將幫助我自定義像Firefox這樣的列表項。 但另一個在JComboBox文本框中添加圖標,我將使用自定義繪畫來繪製應該在控件右側的圖標。 http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html 謝謝, Minh – Minh 2009-07-03 03:45:54