-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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
How about posting it How-tos?
Great. It helps me too
Asked: 2008-11-27 13:00:36 +0800
Seen: 637 times
Last updated: Jul 19 '12