Sun. Oct 24th, 2021

Requirement: I have a Print button on my ADF page on click of which it should open the Browser Print Dialog. I want to achieve the solution for this functionality by using JavaScript.

Solution: For solution of the above requirement follow the steps as shown below:

Step 1: Create an Oracle ADF Fusion Web Application. Create a demo.jspx page in the ViewController project.

Step 2: Drag and drop af:panelBox and set text=”About Me” and showDisclosure=”false”

Drag and drop af:outputText and set value=”My name is Susanto Paul. I am an Oracle Technical Specialist”

Drag and drop af:button and set text=”Print” , actionListener=”#{myBean.printActionListener}”
and binding=”#{myBean.printButtonBind}”

Thus, the complete code for the demo.jspx page is shown below.

<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:f="http://java.sun.com/jsf/core"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
    <jsp:directive.page contentType="text/html;charset=UTF-8"/>
    <f:view>
        <af:document title="demo.jspx" id="d1">
            <af:form id="f1">
                <af:panelBox text="About Me" id="pb1" showDisclosure="false">
                    <af:outputText value="My name is Susanto Paul. I am an Oracle Technical Specialist" id="ot1"/>
                    <f:facet name="toolbar">
                        <af:button text="Print" id="b1" actionListener="#{myBean.printActionListener}"
                                   binding="#{myBean.printButtonBind}"/>
                    </f:facet>
                </af:panelBox>
            </af:form>
        </af:document>
    </f:view>
</jsp:root>

And, the complete code for the MyBean.java file is shown below:

package susantotech.com;

import javax.faces.event.ActionEvent;
import javax.faces.context.FacesContext;

import oracle.adf.view.rich.component.rich.nav.RichButton;
import oracle.adf.view.rich.context.AdfFacesContext;

import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import org.apache.myfaces.trinidad.util.Service;

public class MyBean {
    private RichButton printButtonBind;

    public MyBean() {
    }

    public void setPrintButtonBind(RichButton printButtonBind) {
        this.printButtonBind = printButtonBind;
    }

    public RichButton getPrintButtonBind() {
        return printButtonBind;
    }

    public void printActionListener(ActionEvent actionEvent) {
        printButtonBind.setVisible(false);
        AdfFacesContext.getCurrentInstance().addPartialTarget(printButtonBind);
        runJavaScriptCode("window.print();");
    }

    public static void runJavaScriptCode(String javascriptCode) {
        FacesContext facesCtx = FacesContext.getCurrentInstance();
        ExtendedRenderKitService service = Service.getRenderKitService(facesCtx, ExtendedRenderKitService.class);
        service.addScript(facesCtx, javascriptCode);
    }
}

Save all and run the application. Thus, the ran application is shown below:

Click on the Print button and with that it will open the Browser Print Dialog.

Please note before it opens the Browser Print Dialog it will make the Print button in the demo.jspx page invisible so that the Print button does not get printed. Once we refresh the demo.jspx page the Print button will be available again.

Hence, the solution to our requirement.

If you like the post please comment, share, and do join me on Facebook. Please subscribe to my YouTube Channel for video tutorials.

Thanks & Regards,
Susanto Paul

 284 total views,  1 views today

By Susanto Paul

Susanto is an Oracle ACE, a qualified MCA, MBA, and a highly-skilled Senior Oracle Specialist. He is an enthusiastic Blogger and YouTuber who helps learners to solve their complex problems more efficiently. He has 9+ years of experience in multiple technologies like AWS, Oracle ADF, Oracle APEX, Oracle JET, Oracle VBCS, Oracle IDCS, Oracle PL/SQL, Oracle Integration Cloud, Java, JDBC, Servlets, JSP, Spring, Hibernate, HTML5, CSS3, JavaScript, TypeScript, NodesJS, Angular, MySQL, Oracle WebLogic Server, JUnit, JMeter, Selenium Web Driver, etc. He is a certified: Oracle Certified Professional Java SE 6 Programmer, Oracle ADF 11g Certified Implementation Specialist, Oracle Cloud Platform Application Integration 2020 Certified Specialist, Oracle Cloud Infrastructure Foundations 2020 Certified Associate, and Oracle Cloud Infrastructure Developer 2020 Certified Associate

Leave a Reply

Your email address will not be published. Required fields are marked *