2013-08-05 27 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

你应该接受你的答案,这是非常有帮助的。 –