0

set focus on Checkbox doesn't work

asked 2010-07-01 08:35:36 +0800

bpe gravatar image bpe
27 1

I'm not quite sure if this is a bug or if I'm doing something wrong (because it would be very strange, if nobody had encountered this problem earlier :) ), but I can't set the focus on a Checkbox. I tried it the following ways:

-) in Java Code
checkbox.setFocus(true);
checkbox.focus();
-) in ZUL page
<checkbox label="test" focus="true"/>

I also created a ZUL page, only with a Checkbox in it, just to be sure, that the Checkbox doesn't gain the focus and looses it again, because another component requested it, but it didn't work either.

Any hints?

delete flag offensive retag edit

12 Replies

Sort by ยป oldest newest

answered 2010-07-04 03:52:01 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

Hi bpe
ZK only allow focus on below element

if (!jq.nodeName(n, 'button', 'input', 'textarea', 'a', 'select', 'iframe'))
    return false;


but checkbox wrap by a div
You can use ZK client side API
<zk xmlns:w="http://www.zkoss.org/2005/zk/client">
	<checkbox label="test">
		<attribute w:name="bind_">
		    function (dt) {
		           this.$bind_(dt); //call the original method
		           this.$n('real').focus();
		    }
	    </attribute>
	</checkbox>
</zk>

You can refer to Client Computing with ZUML

link publish delete flag offensive edit

answered 2010-07-05 04:05:46 +0800

bpe gravatar image bpe
27 1

Hi as1225/Jimmy, thank you for your response! Unfortunately, it doesn't work. I added your piece of code to one of my checkboxes. But know I'm kinda stuck, because I don't know what to call in my Java code to get this function(dt) processed.

Maybe a further explanation, what my actual situation is: I've got an image, which is visible, and a checkbox, which is invisible. The image got an "onDoubleClick" event listener, which is going to make the image invisible and the checkbox visible and the checkbox got an "onBlur" event listener, which returns both components into the previous state. But because a "checkbox.focus();" doesn't work, I have to click the checkbox once (so it gets the focus) and not till then the "onBlur" can get active. Maybe there is another trick?

Thanks in advance!

link publish delete flag offensive edit

answered 2010-07-05 10:45:42 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

What version of ZK are you using?

link publish delete flag offensive edit

answered 2010-07-06 02:41:13 +0800

bpe gravatar image bpe
27 1

5.0.3

link publish delete flag offensive edit

answered 2010-07-07 21:12:56 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

I created a sample

<zk>
	<zscript deferred="true"><![CDATA[
		cb.setWidgetListener("onShow","this.fire('onBlur');");
	]]></zscript>
	<image id="img" width="100px" height="100px" src="../../../img/1.jpg" >
		<attribute name="onDoubleClick"><![CDATA[
			img.visible = false;
			cb.visible = true;
		]]></attribute>
	</image>
	<checkbox id="cb" visible="false">
		<attribute name="onBlur"><![CDATA[
			alert("onBlur");
		]]></attribute>
	</checkbox>
</zk>

link publish delete flag offensive edit

answered 2010-07-08 07:14:01 +0800

bpe gravatar image bpe
27 1

Thanks for the sample, but it seems that you misunderstood me :(

I'll try to explain it in another way: so we have an user here, who wants to change the setting of the image/checkbox pair. He double-clicks on the image, which will "transform" into the checkbox (image is now invisible, checkbox is visible). He clicks on the checkbox to change the setting. Now the checkbox got the focus. He clicks anywhere else and the onBlur event triggers, "transforming" the checkbox back into the image. Everything is working as intended!

The next day the user double-clicks the image again and the transforming happens. But now he realizes, that he doesn't want to change the setting. So he doesn't click the checkbox. But in this case, because there is no setFocus(), the checkbox doesn't get the focus on its own and therefore the onBlur event (and the transforming back) will never be triggered. Now, to trigger the onBlur, he has to click the checkbox twice, so it will get the focus, but doesn't change the setting.

It is really, really nothing more than a minor issue, but it is kinda annoying. The feature to set the focus on a checkbox would be really nice, because I don't believe there is a workaround to solve this problem.

link publish delete flag offensive edit

answered 2010-07-08 08:23:37 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

Is it correct ?

<zk>
	<image id="img"  src="/img/earth.png" >
		<attribute name="onDoubleClick"><![CDATA[
			img.visible = false;
			cb.visible = true;
		]]></attribute>
	</image>
	<checkbox id="cb" visible="false">
		<attribute name="onBlur"><![CDATA[
			img.visible = true;
			cb.visible = false;
		]]></attribute>
	</checkbox>
	<zscript><![CDATA[
		cb.setWidgetOverride("bind_", "function (desktop) {this.$bind_(desktop);zWatch.listen({onShow: this});}");
		cb.setWidgetOverride("unbind_", "function () {zWatch.unlisten({onShow: this});this.$unbind_();}");
		cb.setWidgetOverride("onShow", "function () {this.$n('real').focus();}");
	]]></zscript>
</zk>

link publish delete flag offensive edit

answered 2010-07-08 10:38:45 +0800

bpe gravatar image bpe
27 1

One word: awesome! It does exactly, what I was looking for. Big thanks! ;)

link publish delete flag offensive edit

answered 2010-07-08 20:43:07 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

I think it is a bug
set focus shall be work
I modify my sample

<zk>
	<image id="img"  src="/img/earth.png" >
		<attribute name="onDoubleClick"><![CDATA[
			img.visible = false;
			cb.visible = true;
                        cb.focus();
		]]></attribute>
	</image>
	<checkbox id="cb" visible="false">
		<attribute name="onBlur"><![CDATA[
			img.visible = true;
			cb.visible = false;
		]]></attribute>
	</checkbox>
	<zscript><![CDATA[
		cb.setWidgetOverride("focus", "function () {this.$n('real').focus();}");
	]]></zscript>
</zk>


I have pose a bug
https://sourceforge.net/tracker/index.php?func=detail&aid=3027158&group_id=152762&atid=785191

we will fix it in ZK 5.0.4

link publish delete flag offensive edit

answered 2010-07-09 03:37:28 +0800

bpe gravatar image bpe
27 1

Nicely done, it worked too.

Thanks again!

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: 2010-07-01 08:35:36 +0800

Seen: 2,129 times

Last updated: Aug 26 '10

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