HowTo: ZKoss Multiline Messagebox

With followed code you can create a zkoss multi line messagebox which works with line feeds ‘\n’.
Only take the zul template and the controller to your project and set the template path in the controller.

Additionally you can set an empty line before and after the message for a nicer looking.

Home Home
Home


Have fun with it.

Stephan Gerth

Dipl.rer.pol.
PS: Help to prevent the global warming by writing cool software


multiLineMessageBox.zul


package de.forsthaus.zksample.webui.util;

import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Messagebox;

/**
* Extended messagebox that can show multilined messages. <br>
* Lines can be breaked with the \n . <br>
* <br>
*
* @author Stephan Gerth / Forsthaus Datentechnik
* @email sge(at)forsthaus(dot)de
* @changes 04.07.2009/sge extended for showing the icons <br>
*          05.07.2009/sge added an empty line before and after the message. <br>
*          08.07.2009/sge added for the EventListener
*
*/
public class MultiLineMessageBox extends Messagebox {

private static final long serialVersionUID = 1L;
// path of the messagebox zul-template
private static String _templ = "/WEB-INF/pages/util/multiLineMessageBox.zul";

public MultiLineMessageBox() {
}

public static void doSetTemplate() {
setTemplate(_templ);
}

/**
* Shows a message box and returns what button is pressed. A shortcut to
* show(message, null, OK, INFORMATION). <br>
* <br>
* Simple MessageBox with customizable message and title. <br>
*
* @param message
*            The message to display.
* @param title
*            The title to display.
* @param icon
*            The icon to display. <br>
*            QUESTION = "z-msgbox z-msgbox-question"; <br>
*            EXCLAMATION = "z-msgbox z-msgbox-exclamation"; <br>
*            INFORMATION = "z-msgbox z-msgbox-imformation"; <br>
*            ERROR = "z-msgbox z-msgbox-error"; <br>
* @param buttons
*            MultiLineMessageBox.CANCEL<br>
*            MultiLineMessageBox.YES<br>
*            MultiLineMessageBox.NO<br>
*            MultiLineMessageBox.ABORT<br>
*            MultiLineMessageBox.RETRY<br>
*            MultiLineMessageBox.IGNORE<br>
* @param padding
*            true = Added an empty line before and after the message.<br>
*
*
* @return
* @throws InterruptedException
*/
public static final int show(String message, String title, int buttons, String icon, boolean padding) throws InterruptedException {

String msg = message;

if (padding == true) {
msg = "\n" + message + "\n\n";
}

if (icon.equals("QUESTION")) {
icon = "z-msgbox z-msgbox-question";
} else if (icon.equals("EXCLAMATION")) {
icon = "z-msgbox z-msgbox-exclamation";
} else if (icon.equals("INFORMATION")) {
icon = "z-msgbox z-msgbox-imformation";
} else if (icon.equals("ERROR")) {
icon = "z-msgbox z-msgbox-error";
}

return show(msg, title, buttons, icon, 0, null);
}

/**
* Shows a message box and returns what button is pressed. A shortcut to
* show(message, null, OK, INFORMATION). <br>
* <br>
* Simple MessageBox with customizable message and title. <br>
*
* @param message
*            The message to display.
* @param title
*            The title to display.
* @param icon
*            The icon to display. <br>
*            QUESTION = "z-msgbox z-msgbox-question"; <br>
*            EXCLAMATION = "z-msgbox z-msgbox-exclamation"; <br>
*            INFORMATION = "z-msgbox z-msgbox-imformation"; <br>
*            ERROR = "z-msgbox z-msgbox-error"; <br>
* @param buttons
*            MultiLineMessageBox.CANCEL<br>
*            MultiLineMessageBox.YES<br>
*            MultiLineMessageBox.NO<br>
*            MultiLineMessageBox.ABORT<br>
*            MultiLineMessageBox.RETRY<br>
*            MultiLineMessageBox.IGNORE<br>
* @param padding
*            true = Added an empty line before and after the message.<br>
*
*
* @return
* @throws InterruptedException
*/
public static final int show(String message, String title, int buttons, String icon, boolean padding, EventListener listener)
throws InterruptedException {

String msg = message;

if (padding == true) {
msg = "\n" + message + "\n\n";
}

if (icon.equals("QUESTION")) {
icon = "z-msgbox z-msgbox-question";
} else if (icon.equals("EXCLAMATION")) {
icon = "z-msgbox z-msgbox-exclamation";
} else if (icon.equals("INFORMATION")) {
icon = "z-msgbox z-msgbox-imformation";
} else if (icon.equals("ERROR")) {
icon = "z-msgbox z-msgbox-error";
}

return show(msg, title, buttons, icon, 0, listener);
}
}


and the corresponding zul-template:

multiLineMessageBox.zul


<?xml version="1.0" encoding="UTF-8"?>
<?page title="Multiline Messagebox" language="xul/html"?>

<window border="none" width="300pt" closable="true"
use="org.zkoss.zul.impl.MessageboxDlg">

<style dynamic="true">
.myMultiMessageBox .z-panel-header { background: #FC7A7C -1px;
font-weight:bold; zoom: 1; border: 1px solid; line-height:
15px;} .myMultiMessageBox .z-panel-body { border-style:none
solid solid; border-width:0 1px 1px; overflow:hidden;
padding:0px; }
</style>

<panel title="${arg.title}" border="normal"
sclass="myMultiMessageBox">
<panelchildren>
<hbox>
<div class="${arg.icon}" />
<div sclass="z-messagebox" width="100%">
<label multiline="true" value="${arg.message}"
sclass="word-wrap" width="100%" />
</div>
<div width="10px" />
</hbox>
<separator bar="true" />
<hbox style="margin-left:auto; margin-right:auto">
<button id="btn1" identity="${arg.OK}"
sclass="z-messagebox-btn"
use="org.zkoss.zul.impl.MessageboxDlg$Button"
if="${!empty arg.OK}" />
<button identity="${arg.CANCEL}"
sclass="z-messagebox-btn"
use="org.zkoss.zul.impl.MessageboxDlg$Button"
if="${!empty arg.CANCEL}" />
<button identity="${arg.YES}" sclass="z-messagebox-btn"
use="org.zkoss.zul.impl.MessageboxDlg$Button"
if="${!empty arg.YES}" />
<button identity="${arg.NO}" sclass="z-messagebox-btn"
use="org.zkoss.zul.impl.MessageboxDlg$Button"
if="${!empty arg.NO}" />
<button identity="${arg.RETRY}"
sclass="z-messagebox-btn"
use="org.zkoss.zul.impl.MessageboxDlg$Button"
if="${!empty arg.RETRY}" />
<button identity="${arg.ABORT}"
sclass="z-messagebox-btn"
use="org.zkoss.zul.impl.MessageboxDlg$Button"
if="${!empty arg.ABORT}" />
<button identity="${arg.IGNORE}"
sclass="z-messagebox-btn"
use="org.zkoss.zul.impl.MessageboxDlg$Button"
if="${!empty arg.IGNORE}" />
</hbox>
<separator></separator>
</panelchildren>
</panel>
</window>


sample call:

// Show a error box
String msg = "Hallo \n\nI'm a multi line Message.";
String title = "E r r o r !";

MultiLineMessageBox.doSetTemplate();
MultiLineMessageBox.show(msg, title, MultiLineMessageBox.OK, "ERROR", true);

or

if (MultiLineMessageBox.show(...) == MultiLineMessageBox.OK){
// do someting
}

Samples are hostet in the Zksample2 project on

Leave a Reply

You must be logged in to post a comment.