0

How to create a combobox to pick up color items?

asked 2012-04-04 22:22:37 +0800

gyowanny gravatar image gyowanny
283 1 2 6

updated 2012-04-04 22:23:30 +0800

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

delete flag offensive retag edit

4 Replies

Sort by » oldest newest

answered 2012-04-04 22:49:34 +0800

twiegand gravatar image twiegand
1807 3

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

link publish delete flag offensive edit

answered 2012-04-05 02:07:09 +0800

gyowanny gravatar image gyowanny
283 1 2 6

updated 2012-04-05 02:07:46 +0800

Twiegand,

That worked fine!

Here is the result: screenshot

Thank you for the quick answer.

link publish delete flag offensive edit

answered 2012-04-05 02:20:00 +0800

gyowanny gravatar image gyowanny
283 1 2 6

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

link publish delete flag offensive edit

answered 2012-04-05 02:54:02 +0800

gyowanny gravatar image gyowanny
283 1 2 6

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

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2012-04-04 22:22:37 +0800

Seen: 166 times

Last updated: Apr 05 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More