0

True readonly Combobox

asked 2008-11-27 13:00:36 +0800

aros54 gravatar image aros54
66

Hi all.
As the javadoc states, the value of a readonly Combobox can be changed by dropping down the list and selecting an item.
This behaviour can be very confusing not only because a user can potentially change the value of the readonly fields showing screens with fake data, but also becasue many applications use the same window both to display data (with fields set to readonly) and to modify them.
Datebox has a similar behaviour when readonly: you can click on the calendar button and modify the displayed date. The solution in this case is very simple: hide the calendar button when readonly with the method setButtonVisible(false). With Combobox we have one more problem: even if the drop list button is hided, the list drops down by clicking on the field and the user can still select a different item.
The solution I found is to disable all the comboitems when the parent combobox is readonly. This can be done either defining a new component or "using" a class inherited from Combobox with some method override. I show here the code of the second solution.

This is the java code for the component override:

public class Test extends Combobox {

@Override
public void onChildAdded(Component child) {
super.onChildAdded(child);

// When a Combobox is created the property readonly is evaluated
// before children are added.
// So we need to set the initial status of the child (disabled true/false)
// when each child is added the first time
if (child instanceof Comboitem) {
((Comboitem)child).setDisabled(isReadonly());
}
}

@Override
public void setReadonly(boolean readonly) {
super.setReadonly(readonly);

// as the children drop list is shown on clicking on the field area
// even if the button is not visible, we set the children status
// (disabled if readonly) so that the user can't click on them
for (Iterator iterator = getChildren().iterator(); iterator.hasNext();) {
Component c = (Component) iterator.next();
if (c instanceof Comboitem) {
((Comboitem)c).setDisabled(isReadonly());
}
}
}

}

And a test window:

<zk>
<window border="normal" title="Readonly Combobox test" width="320px" height="80px">
<zscript><![CDATA[
boolean ro = false;
]]></zscript>
<hbox>
<label id="label" value="NOT Readonly"/>
<combobox id="test" use="com.arcademy.test.Test">
<comboitem label="Simple and Rich"/>
<comboitem label="Cool!"/>
<comboitem label="Thumbs Up!"/>
</combobox>
<button label="Toggle" onClick="toggle();"/>
</hbox>
<zscript><![CDATA[
void toggle() {
ro=!ro;
label.value = ro ? "Readonly" : "NOT Readonly";
test.setReadonly(ro);
test.setButtonVisible(!ro);
}
]]></zscript>
</window>
</zk>

I hope this can help somebody. Any comment is appreciated.
Best regards
Adriano

delete flag offensive retag edit

3 Replies

Sort by ยป oldest newest

answered 2008-11-28 11:08:06 +0800

robertlee gravatar image robertlee
561

Thank you, Adriano!

link publish delete flag offensive edit

answered 2008-12-04 14:45:06 +0800

tomyeh gravatar image tomyeh
610 1 3
http://blog.zkoss.org ZK Team

How about posting it How-tos?

link publish delete flag offensive edit

answered 2012-07-19 17:02:48 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 3 8
http://emrpms.blogspot.in...

Great. It helps me too

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: 2008-11-27 13:00:36 +0800

Seen: 637 times

Last updated: Jul 19 '12

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