Please disable your adblock and script blockers to view this page

Search this blog

Friday 30 November 2018

Switching ADF Skin at run time using trinidad-config.xml

 This post talks about switching ADF skin at runtime and uses a managed bean variable to change skin name at runtime. Sometimes we want to use multiple themes for our application and in that case, we need this.

Created an ADF application, 2 different skins and a page in view controller project


Learn more about creating ADF skin in JDeveloper 

This page has a button to switch between skins and an outputText. Create a variable in the managed bean to hold skin name and button action listener to change skin variable value.

Here goes the managed bean code

  1. package switchadfskin.view.bean;
  2. import javax.faces.application.ViewHandler;
  3. import javax.faces.component.UIViewRoot;
  4. import javax.faces.context.FacesContext;
  5. import javax.faces.event.ActionEvent;
  6. public class SwitchADFSkinBean {
  7. public SwitchADFSkinBean() {
  8. }
  9. //Set default skin name
  10. private String skinName = "RedButton";
  11. public void setSkinName(String skinName) {
  12. this.skinName = skinName;
  13. }
  14. public String getSkinName() {
  15. return skinName;
  16. }
  17. /**Method to change skin at runtime
  18. * @param actionEvent
  19. */
  20. public void switchSkinAction(ActionEvent actionEvent) {
  21. //Change skin name
  22. if (skinName.equalsIgnoreCase("RedButton")) {
  23. this.setSkinName("GreenButton");
  24. } else {
  25. this.setSkinName("RedButton");
  26. }
  27. //Reload page
  28. refreshPage();
  29. }
  30. /**Method to refresh/reload page
  31. */
  32. protected void refreshPage() {
  33. FacesContext fctx = FacesContext.getCurrentInstance();
  34. String page = fctx.getViewRoot().getViewId();
  35. ViewHandler ViewH = fctx.getApplication().getViewHandler();
  36. UIViewRoot UIV = ViewH.createView(fctx, page);
  37. UIV.setViewId(page);
  38. fctx.setViewRoot(UIV);
  39. }
  40. }

The next step is to change the trinidad-config.xml file, set skin-family variable to refer to its value from managed bean so that when the user changes the bean variable then skin (CSS) is updated in the application.

  1. <?xml version="1.0" encoding="windows-1252"?>
  2. <trinidad-config xmlns="http://myfaces.apache.org/trinidad/config">
  3. <skin-family>#{SwitchADFSkinBean.skinName}</skin-family>
  4. </trinidad-config>

Now run and check application

The default skin is "RedButton" so it looks like this


and on click of the button

Sample ADF Application (JDeveloper 12.1.3) - Download

Cheers 🙂 Happy Learning

Saturday 24 November 2018

Read Text File in Java using FileReader Class

 Previously I have posted about reading text file using FileInputStreamBufferedReader and Scanner Class. This post shows how to read text files in java using FileReader class.


This is a sample text file


and this is the java code to read text files using FileReader class

  1. package client;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. public class ReadFileFr {
  6. public ReadFileFr() {
  7. super();
  8. }
  9. public static void main(String[] args) {
  10. // Absolute path of file that you want to read
  11. FileReader fr=null;
  12. try {
  13. fr = new FileReader("D:/sampleFile.txt");
  14. } catch (FileNotFoundException e) {
  15. System.out.println(e);
  16. }
  17. int data;
  18. //Iterate over file content
  19. try {
  20. while ((data = fr.read()) != -1)
  21. System.out.print((char) data);
  22. } catch (IOException e) {
  23. System.out.println(e);
  24. }
  25. }
  26. }

Execute this java code

Output is


Cheers 🙂 Happy Learning