-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi,
I'm trying to create a combobox that contains colors as items.
I tried to add a ComboboxItemRenderer where I set the background color of each item like this:
String color = (String) data; item.setStyle("background-color: "+color); item.setHeight("20px");
However it´s not working. I did that way since the ComboboxItem doesn´t support child components.
Any clue? Is that possible to do something similiar to my approach or I should generate an icon for each color?
Thank you,
Gyo
Gyo,
Maybe this will help:
<zk> <zscript> import org.zkoss.zk.ui.util.GenericForwardComposer; public class MyController extends GenericForwardComposer { Window main; public void doAfterCompose(Component comp) throws Exception { super.doAfterCompose(comp); Combobox combobox = new Combobox(); String ciColor = "red"; Comboitem comboitem = new Comboitem(); comboitem.setLabel(ciColor); comboitem.setValue(ciColor); comboitem.setStyle("background:" + ciColor + "; color:" + ciColor +";"); combobox.appendChild(comboitem); String ciColor = "green"; Comboitem comboitem = new Comboitem(); comboitem.setLabel(ciColor); comboitem.setValue(ciColor); comboitem.setStyle("background:" + ciColor + "; color:" + ciColor +";"); combobox.appendChild(comboitem); String ciColor = "blue"; Comboitem comboitem = new Comboitem(); comboitem.setLabel(ciColor); comboitem.setValue(ciColor); comboitem.setStyle("background:" + ciColor + "; color:" + ciColor +";"); combobox.appendChild(comboitem); main.appendChild(combobox); } } </zscript> <window id="main" apply="MyController"> </window> </zk>
Regards,
Todd
Twiegand,
I have another question: When I select a color, I don't want the name to be displayed in the combobox field but the selected color. Any clue?
PS: That combobox effort is due to a post-it feature I'm creating using a dynamic Tablelayout: Screenshot
Ok,
I have figured out. Just added an onChange event to the combobox in order to set it's style, like the code below:
comboColors.addEventListener("onChange", new EventListener() { @Override public void onEvent(Event evt) throws Exception { String color = (String) comboColors.getSelectedItem().getValue(); cbCores.setStyle("background: "+color+"; color: "+color+";"); } });
thanks again,
Gyo
Asked: 2012-04-04 22:22:37 +0800
Seen: 166 times
Last updated: Apr 05 '12