Java Fx Web Component Developement using Xml


In this post you get a good solution to create a web component for your website using Java Fx 2.0.Main benefit of Java Fx 2.0 is Java.Here in this application i choose xml file in order to provide a portable web component .For deploy use Netbeans7.1 by which you can easily develop JNLP file. You can provide your input in the form of xml data..Following is sample program for creating Pie Chart component for your website.As Java FX is rich client side application it can be easily run in all browser.
Sourcecode

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chartsamples;

import java.io.File;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.chart.*;
import javafx.scene.Group;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
/**
 *
 * @author sabari
 */
public class Chartsamples extends Application {
 @Override public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Best Technology");
        stage.setWidth(500);
        stage.setHeight(500); 
        ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList();
        NodeList listOfPersons = getListOfNode();
            int totalPersons = listOfPersons.getLength();
          

            for(int s=0; s<listOfPersons.getLength() ; s++){


                Node firstPersonNode = listOfPersons.item(s);
                if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){


                    Element firstPersonElement = (Element)firstPersonNode;

                    //-------
                    NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
                    Element firstNameElement = (Element)firstNameList.item(0);

                    NodeList textFNList = firstNameElement.getChildNodes();
                    System.out.println("First Name : " + 
                           ((Node)textFNList.item(0)).getNodeValue().trim());
                    String fName=((Node)textFNList.item(0)).getNodeValue().trim().toString();

                    //-------
                    NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
                    Element lastNameElement = (Element)lastNameList.item(0);

                    NodeList textLNList = lastNameElement.getChildNodes();
                    System.out.println("Last Name : " + 
                           ((Node)textLNList.item(0)).getNodeValue().trim());

                    //----
                    NodeList ageList = firstPersonElement.getElementsByTagName("stat");
                    Element ageElement = (Element)ageList.item(0);

                    NodeList textAgeList = ageElement.getChildNodes();
                    System.out.println("Age : " + 
                           ((Node)textAgeList.item(0)).getNodeValue().trim());

                    //------
                   pieChartData.add(    new PieChart.Data(
                           
                           ((Node)textFNList.item(0)).getNodeValue().trim().toString()+
                           ((Node)textLNList.item(0)).getNodeValue().trim().toString(),
                           Double.parseDouble(((Node)textAgeList.item(0)).getNodeValue().trim().toString())));

                }//end of if clause


            }//end of for loop with s var
              pieChartData.add(    new PieChart.Data("Java", 70));
              pieChartData.add(    new PieChart.Data(".Net", 30));
              pieChartData.add(    new PieChart.Data("Php", 20));
              pieChartData.add(    new PieChart.Data("Ajax", 40));
              pieChartData.add(    new PieChart.Data("JavaScript", 20));
              
          
        final PieChart chart = new PieChart(pieChartData);
        chart.setTitle("Best Technology");

        ((Group) scene.getRoot()).getChildren().add(chart);
        stage.setScene(scene);
        stage.show();
    }
 
 public NodeList getListOfNode()
 {   NodeList listOfPersons=null;
  try {

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            System.out.println ("the path is"+ReadAndPrintXMLFile.class.getResource("book.xml").toString());          
            Document doc = docBuilder.parse (ReadAndPrintXMLFile.class.getResource("book.xml").toString()); 
            doc.getDocumentElement ().normalize ();
            System.out.println ("Root element of the doc is " + 
               doc.getDocumentElement().getNodeName());
               NodeList chidnode=doc.getDocumentElement().getChildNodes();
               System.out.println ("Root element of the doc is " + 
               doc.getDocumentElement().getChildNodes());
               listOfPersons = doc.getElementsByTagName("person");
        

        }catch (SAXParseException err) {
        System.out.println ("** Parsing error" + ", line " 
             + err.getLineNumber () + ", uri " + err.getSystemId ());
        System.out.println(" " + err.getMessage ());

        }catch (SAXException e) {
        Exception x = e.getException ();
        ((x == null) ? e : x).printStackTrace ();

        }catch (Throwable t) {
        t.printStackTrace ();
        }
        //System.exit (0);

    return   listOfPersons;

 }
    public static void main(String[] args) {
        launch(args);
    }
}


Xml File
// Comment
eg
<book>
<person>
  <first>Java</first>
  <last>Technology</last>
  <stat>60</stat>
</person>
<person>
   <first>Dot Net</first>
  <last>Technology</last>
  <stat>30</stat>
</person>
<person>
  <first>Php</first>
  <last>Web</last>
  <stat>20</stat>
</person>
<person>
  <first>HTML</first>
 <last>Web</last>
  <stat>20</stat>
</person>
<person>
  <first>Java Script</first>
<last>Web</last>
  <stat>30</stat>
</person>
<person>
  <first>Ajax</first>
 <last>Web</last>
  <stat>40</stat>
</person>
</book> 


Output Window Screen
raja P Web Developer

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...