简体   繁体   中英

Dynamically add Primefaces components to JSF Page

I am trying to add panels to the body of my jsf page and they appear on the page but none of the settings work and the calendars just appear as regular inputText boxes. Am I initializing my components incorrectly or adding them incorrectly? The page I am making allows me to click a button and add premade panels to my page.

private Date startDate;

private Date endDate;

private String Kpi;

private String baselineName;

private UIOutput body;

public void addPanel(ActionEvent event){
    //UIComponent component = FacesContext.getCurrentInstance().getViewRoot();
    if(body != null){
        body.getChildren().add(createCommandThroughputPanel());
        log.debug("Panel Added");
    }
}

public void removePanel(ActionEvent event){
    if(body != null){
        body.getChildren().remove(body.getChildren().size() - 1);
        log.debug("Panel Removed");
    }
}

public UIForm createCommandThroughputPanel(){

    UIForm form = new UIForm();

    //Create Panel
    Panel p = new Panel();
    p.setHeader("Command Throughput Data");
    p.setClosable(true);
    p.setToggleable(true);
    p.setToggleSpeed(500);
    p.setVisible(true);

    log.debug("p.getToggleSpeed() = " + p.getToggleSpeed());

    //Initialize startDate input calendar
    Calendar startCalendar = new Calendar();
    startCalendar.setValue(startDate);
    startCalendar.setShowButtonPanel(true);
    startCalendar.setNavigator(true);
    startCalendar.setPattern("MM/dd/yyyy HH:mm");
    startCalendar.setId("start");

    log.debug("startCalendar.getPattern() = " + startCalendar.getPattern());
    log.debug("startCalendar.getFamily() = " + startCalendar.getFamily());

    //Initialize endDate input Calendar
    Calendar endCalendar = new Calendar();
    endCalendar.setValue(endDate);
    endCalendar.setShowButtonPanel(true);
    endCalendar.setNavigator(true);
    endCalendar.setPattern("MM/dd/yyyy HH:mm");
    endCalendar.setId("end");

    log.debug("startCalendar.getPattern() = " + startCalendar.getPattern());
    log.debug("endCalendar.getFamily() = " + endCalendar.getFamily());

    //Initialize baseline selectOneMenu
   SelectOneMenu baseline = new SelectOneMenu();
   baseline.setId("baseline");
   baseline.setValue(baselineName);
   UISelectItems item = new UISelectItems();

    p.getChildren().add((Calendar)startCalendar);
    p.getChildren().add((Calendar)endCalendar);
    form.getChildren().add(p);

    for(UIComponent cal : p.getChildren()){
        log.debug("cal.getId() = " + cal.getId());
        log.debug("cal.getFamily() = " + cal.getFamily());
    }


    return form;

}

why you dont like just use a xhtml page and you need just to write this:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">

<p:layoutUnit position="north" size="40">
                    <p:commandButton value="About"  onclick="PF(alert('Esprit Olap View')).show()" type="button" icon="ui-icon-power"/>

                 <p:commandButton align="right" value="Connect" type="button" onclick="PF('dlg3').show();" style="text-align:right" />

                </p:layoutUnit>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM