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.

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

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");
        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 : " + 
                    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 : " + 

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

                    NodeList textAgeList = ageElement.getChildNodes();
                    System.out.println("Age : " + 

                   pieChartData.add(    new PieChart.Data(

                }//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);
 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 " + 
               NodeList chidnode=doc.getDocumentElement().getChildNodes();
               System.out.println ("Root element of the doc is " + 
               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) {

Xml File
// Comment
   <first>Dot Net</first>
  <first>Java Script</first>

Output Window Screen
raja P Web Developer

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...