我做到了&現在的工作就好了:) 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);
}
...我們現在的家。希望代碼能夠幫助某人:)
動態添加項目到JComboBox(已經可見)== MutableComboBoxModel – mKorbel
更快地幫助發佈[SSCCE](http://sscce.org/),短的,可運行的,可編譯的, [科幻你可以從JOptionPane獲取圖標](http://stackoverflow.com/a/7944388/714968) – mKorbel
好吧,你通過'addItem()'添加了兩個項目,所以你會得到兩條線...... 你的'ColorSwatch'類有'toString()'方法返回顏色的hexa代碼? – Matthieu