You are right. I don't need the setEnabled(false) in the onClick method I have changed my code :
link = new Link("linkRequest") { @Override public void onClick() { setResponsePage(new RequestPage(requestFormModel)); } }; link.add(new Label("linkRequestTxt",String.valueOf(audit.getRequest().getId()))); item.add(link); } else { link = new Link("linkRequest") { @Override public void onClick() { } }; Label label = new Label("linkRequestTxt",""); link.add(label); link.setEnabled(false); item.add(link); } Remark : It should be interesting to be able to create an instance of the Link class without having to override the onclick() method. Otherwise the code becomes very verbose and requires as here that we instantiate two times the class Link Matthias Keller wrote: > > Sure, just create it (including the onClick which might be invalid), but > afterwards set the setEnabled() as needed. > As long as the link is disabled, it can never be clicked thus the > onClick() will never be executed anyway, so it's irrelevant if the > requestFormModel is valid or not... > I dont think you want the setEnabled(false) inside the onClick - do you? > That would deactivate the link after the first click which doesn't make > that much sense? > > Matt > > Charles Moulliard wrote: >> Hi, >> >> I would like to know if there is a better way to code this case : What >> I would like to do is to enable/disable a link depending on a >> condition. If the condition is true, than we create the link otherwise >> we disable it. In both case, a label must be defined for the Link. >> >> I mean, is it possible to avoid to double the creation of the link = >> new Link() .... >> >> Here is my code >> >> 1) HTML >> >> # wicket:id="linkRequestTxt"/> >> >> >> 2) JAVA >> >> // Set link for requestId >> Link link; >> >> if (audit.getRequest() != null) { >> >> final RequestFormModel requestFormModel = new >> RequestFormModel(); >> requestFormModel.setRequestId( (int) >> audit.getRequest().getId() ); >> >> link = new Link("linkRequest") { >> >> @Override >> public void onClick() { >> setResponsePage(new >> RequestPage(requestFormModel)); >> } >> >> }; >> >> link.add(new >> Label("linkRequestTxt",String.valueOf(audit.getRequest().getId()))); >> item.add(link); >> >> } else { >> link = new Link("linkRequest") { >> >> @Override >> public void onClick() { >> this.setEnabled(false); >> } >> >> }; >> Label label = new Label("linkRequestTxt",""); >> link.add(label); >> item.add(link); >> >> >> Charles Moulliard >> Senior Enterprise Architect >> Apache Camel Committer >> >> ***************************** >> blog : http://cmoulliard.blogspot.com >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org >> For additional commands, e-mail: users-h...@wicket.apache.org >> >> > > > -- > matthias.kel...@ergon.ch +41 44 268 83 98 > Ergon Informatik AG, Kleinstrasse 15, CH-8008 Zürich > http://www.ergon.ch > ______________________________________________________________ > e r g o n smart people - smart software > > > > > ----- Charles Moulliard SOA Architect My Blog : http://cmoulliard.blogspot.com/ http://cmoulliard.blogspot.com/ -- View this message in context: http://www.nabble.com/Is-it-the-best-way-to-code-a-Link-depending-on-a-condition-tp25488603p25488926.html Sent from the Wicket - User mailing list archive at Nabble.com. --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org