How to change theme for only one zul page

asked 2020-05-13 19:57:22 +0800

pablo123 gravatar image pablo123
35 2

Hi, this is my problem. I need to change the theme but only for one ZUL, not for one user or for the whole app.

I tried te common Themes.setTheme(Executions.getCurrent(), "sapphire"); and Library.setProperty("org.zkoss.theme.preferred", "sapphire"); and it works but it changues the theme for the entire application or for the current user un session in a cookie(at least this is what I understand).

So I tried changuin the theme in the doAfterCompose method and changuin again to the default theme when the user leaves the page. I don't know if that would do the trick because I did not find any solution to make my onClose method do work.

I tried things like:

public void onClose$win(Event event){
    Events.getRealOrigin((ForwardEvent) event).stopPropagation();
@Listen("onClose = #window")
public void restaurarTema() {
    System.out.println("SE HA CERRADO");

Even in the zul page:

<window id="window" closable="true"...
    <attribute name="onClose"><![CDATA[
            Messagebox.show("Window closed", "Warning", Messagebox.OK, Messagebox.EXCLAMATION);

I searched a lot and tried everything but I can't find a solution to my problem. Can anyone help me? I'm not an expert at zk as you may notice. The main question is how to change themes for only one zul page, but if you can help me with the onClose method that would hep too for future improvements. Thank you a lot. Workin with ZK 9.0.0

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted

answered 2020-05-14 19:22:15 +0800

MDuchemin gravatar image MDuchemin
2480 1 6
ZK Team

Hi Pablo.

The rule for applying themes is: 1 - cookie 2 - library property 3 - automatic resolution with registered priority

From your description, it sounds like you should be using mostly the cookie option with Themes.setTheme(Executions.getCurrent(), "themeName");

There is a potential issue with theme resolution by cookie however: Since the theme is resoved early in the Execution lifecycle, you might need to redirect to the same page (i.e. reload the page) after setting the theme. See this fiddle for a sample: https://zkfiddle.org/sample/2rakcte/6-theme-switch

There is another option, using a theme resolver: https://www.zkoss.org/javadoc/latest/zk/org/zkoss/web/theme/ThemeResolver.html

You can implement a theme resolver class which will decide which theme should be served based on individual page requests. However, this is a more complicated scenario.

link publish delete flag offensive edit


And do you have any idea why the onClose methods doesn't work? Im working with SelectorComposer<component> to controll the zul components. Thank you for your response.

pablo123 ( 2020-05-15 18:29:09 +0800 )edit

answered 2020-05-18 15:35:15 +0800

MDuchemin gravatar image MDuchemin
2480 1 6
ZK Team

Hey Pablo,

Just tried out the listener you described: https://zkfiddle.org/sample/q7tvqq/7-onClose

Works on my side, the listener is triggered both from the zul attribute and the composer listener.

If the issue is "the listener is triggered, but the theme has not changed", the the answer is simple: the page hasn't been reloaded and themes are evaluated on page load.

In order to update the theme, you need to actually load a page. You can do that with a redirect, or a link, or anything that causes the browser to navigate to the target page. Changing theme in an event listener alone is not sufficient, because the event listener only send an update to the client, and doesn't reload the page.

link publish delete flag offensive edit


Yes, that was the big problem its pretty bad to reload the page when youre already in. I solved that with an <?init...> in the zul page which calls a class named AuthenticationInit implements Initiator and there I do the logic to check the current theme and chague it if its needed. Thx for yr reply.

pablo123 ( 2020-05-29 19:39:26 +0800 )edit
Your answer
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

1 follower



Asked: 2020-05-13 19:57:22 +0800

Seen: 21 times

Last updated: May 18 '20

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