2013-08-05 23 views
1

我使用DefaultComboBoxModel向JComboBox(字符串文本,圖標圖標)添加特定項目。但有些事情出錯了。當我這兩個項目添加到我的組合模型,它看起來像這樣:動態添加項目到JComboBox(值+圖標= jlabel)

ComboBoxWindow: 

       [icon   ] 

       [icon  value] 

總之,我的組合框的代碼如下所示:

private JComboBox combobox; 
... 
DefaultComboBoxModel model = new DefaultComboBoxModel(); 
combobox = new JComboBox(model); 
... 
/* 
* I use JButton for 'sending' hex value taken from JTextField to 'combobox' 
* with help of 'addToComboBox()' method 
*/ 
public void addToComboBox() { 

    String value = field.getText().toString();  // getin' text from 'left' JTextField 

    Color color = tcc.getColor();     // getin' color from some other JLabel 
    ColorSwatch icon = new ColorSwatch(10, true); // using some custom method to create little square icon 
    icon.setColor(color);  // seting color of created icon 

    combobox.addItem(icon); 
    combobox.addItem(value); 
} 

我使用ListCellRenderer考慮,但我不知道如何通過同時使用'value'和'icon'來'告訴'它應該呈現,例如,JLabel組件。對我來說,有可能通過使用JButton動態地添加這些項目是非常重要的。

enter image description here

+0

動態添加項目到JComboBox(已經可見)== MutableComboBoxModel – mKorbel

+0

更快地幫助發佈[SSCCE](http://sscce.org/),短的,可運行的,可編譯的, [科幻你可以從JOptionPane獲取圖標](http://stackoverflow.com/a/7944388/714968) – mKorbel

+0

好吧,你通過'addItem()'添加了兩個項目,所以你會得到兩條線...... 你的'ColorSwatch'類有'toString()'方法返回顏色的hexa代碼? – Matthieu

回答

1

我做到了&現在的工作就好了:) Basicaly 1.我用一個DefaultComboBoxModel 2.我已經把它添加到我的JComboBox,3.我添加了一個自定義的ListCellRenderer它將所採取的字符串(例如'#FFFFFF')翻譯成圖標&正確的文本,並在最後創建具有該新生兒圖標和文本的JLabel。

/** 
* Main Class 
*/ 
public class ColorChooser { 
    ... 
    public ColorChooser() { 
     ... 
     DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>(); 
     JComboBox combobox = new JComboBox<String>(model); 
     combobox.setEditable(false); 
     cobobox.setRenderer(new ComboRenderer()); 
     ... 
    } 
    ... 
} 

/** 
* Renderer Class 
*/ 
public class ComboRenderer extends JLabel implements ListCellRenderer<Object> { 

    public ComboRenderer() { 

    } 

    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 

     setFont(newFont("Consolas", Font.PLAIN, 14)); 
     setOpaque(true); 

     String hex; 

     if (value != null) { 

     /* 
      * So basically I add to my 'model' in ColorChooser main class only Strings 
      * which I get e.g. from some JTextField. 
      * On base of this String I create icon for future JLabel 
      * and I set String 'value' as text for it. 
      */ 
     hex = value.toString(); 

     Color color = HexToRgb(hex); //Method which translates String to Color 

     ColorSwatch icon = new ColorSwatch(10, true); // ColorSwatch is a method which creates specific square icon (in this case a little square) 
     icon.setColor(color); 

     setText(hex); 
     setIcon(icon); 
     } 
     return this; 
    } 

    /* 
    * My translate method which translates given String to a specific color value 
    * (RGB/RGBA) 
    */ 
    public Color HexToRgb(String colorStr) { 

     Color color = null; 

     // For String hex value '#RRGGBB' 
     if (colorStr.length() == 7) { 

     color = new Color(
      Integer.valueOf(colorStr.substring(1, 3), 16), 
      Integer.valueOf(colorStr.substring(3, 5), 16), 
      Integer.valueOf(colorStr.substring(5, 7), 16)); 

     // For String hex value '#AARRGGBB' 
     } else if (colorStr.length() == 9) { 

     color = new Color(
      Integer.valueOf(colorStr.substring(3, 5), 16), 
      Integer.valueOf(colorStr.substring(5, 7), 16), 
      Integer.valueOf(colorStr.substring(7, 9), 16), 
      Integer.valueOf(colorStr.substring(1, 3), 16)); 

     // For String hex value '0xRRGGBB' 
     } else if (colorStr.length() == 8) { 

     color = new Color(
      Integer.valueOf(colorStr.substring(2, 4), 16), 
      Integer.valueOf(colorStr.substring(4, 6), 16), 
      Integer.valueOf(colorStr.substring(6, 8), 16)); 

     // For String hex value '0xAARRGGBB' 
     } else if (colorStr.length() == 10) { 

     color = new Color(
      Integer.valueOf(colorStr.substring(4, 6), 16), 
      Integer.valueOf(colorStr.substring(6, 8), 16), 
      Integer.valueOf(colorStr.substring(8, 10), 16), 
      Integer.valueOf(colorStr.substring(2, 4), 16)); 

     } else 
     JOptionPane.showMessageDialog(null, "Something wen wrong... :|"); 

     return color; 
    } 
} 

而且,像這樣我可以再補充項目,我的組合框渲染...

try { 

    String hex = jtextfield.getText(); 
    boolean canI = CheckHexValue(hex); //Method for checkin' if 'hex' String fits some specific terms 

    if (canI) { 

     combobox.insertItemAt(hex, 0); 
     combobox.setSelectedIndex(0); 
    } 
} catch (Exception e) { 
    JOptionPane.showMessageDialog(null, e); 
} 

...我們現在的家。希望代碼能夠幫助某人:)

+0

你應該接受你的答案,這是非常有幫助的。 –