Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

JavaFX 2.0: A Platform for Rich Enterprise Client Apps-2 : Page 2


advertisement

Sample JavaFX App: Rich Client Interface Demo

Following is a JavaFX 2 tutorial that creates a pie chart and bar chart according to the user input into the TextField of estimated cost and actual cost of a hypothetical visual analysis of estimated budget versus actual expense.

/* * Import statements and package declarations */ package org.event.budget; import java.util.Calendar; import java.util.Random; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.*; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.chart.BarChart; import javafx.scene.chart.CategoryAxis; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.PieChart; import javafx.scene.chart.XYChart; import javafx.scene.control.*; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.*; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; import javafx.stage.Stage; /* * Starting application class */ public class TallyReport extends Application { /* * private members declaration */ String bgcolor = "-fx-background-color: DAE6F3"; private Label lblEstTot = new Label(""); private Label lblActTot = new Label(""); private BorderPane border = null; private GridPane grid = null; private String label[] = { "Site", "Decoration", "Publicity", "Misc.", "Refreshments", "Program", "Prizes" }; private TextField tfEstimated[] = new TextField[label.length]; private TextField tfActual[] = new TextField[label.length]; private PieChart estimateChart = new PieChart(); private PieChart actualChart = new PieChart(); private CategoryAxis xAxis = new CategoryAxis(); private NumberAxis yAxis = new NumberAxis(); private BarChart bc = new BarChart(xAxis,yAxis); /* * Main method */ public static void main(String[] args) { Application.launch(args); } /* * JavaFX application starts from the start(...) function * Here stage is initialized and layout is arranged for the application */ @Override public void start(Stage stage) throws Exception { stage.setTitle("Tally Budget"); border = new BorderPane(); border.setStyle(bgcolor); TabPane tabPane = new TabPane(); Tab tab1 = new Tab("Estimated Cost Breakdown"); tab1.setClosable(false); tab1.setContent(estimateChart); Tab tab2 = new Tab("Actual Cost Breakdown"); tab2.setClosable(false); tab2.setContent(actualChart); tabPane.getTabs().addAll(tab1, tab2); border.setRight(tabPane); VBox vbox = new VBox(); vbox.setStyle(bgcolor); vbox.getChildren().addAll(initInputSection(), bc); border.setCenter(vbox); /* * This may be removed to feed random value into the form feedRandomValue(); */ stage.setScene(new Scene(border, 1000, 600)); estimatedPieChart(); actualPieChart(); barChart(); Integer etot = 0; Integer atot = 0; for (int i = 0; i < label.length; i++) { etot += Integer.parseInt(tfEstimated[i].getText()); atot += Integer.parseInt(tfActual[i].getText()); } lblEstTot.setText("$ " + etot.toString()); lblActTot.setText("$ " + atot.toString()); stage.show(); } /* * Input form is created */ public GridPane initInputSection() { grid = new GridPane(); grid.setHgap(2); grid.setVgap(4); grid.setPadding(new Insets(10, 10, 10, 50)); Text title = new Text("Estimated Vs Actual Cost"); title.setFont(Font.font("Tahoma", FontWeight.BOLD, 20)); grid.add(title, 1, 1, 3, 1); Text estimated = new Text("Estimated Cost"); estimated.setFont(Font.font("Tahoma", FontWeight.BOLD, 10)); grid.add(estimated, 2, 2, 2, 1); Text actual = new Text("Actual Cost"); actual.setFont(Font.font("Tahoma", FontWeight.BOLD, 10)); grid.add(actual, 3, 2, 2, 1); Tooltip tip = new Tooltip("Enter numeric value"); tip.setGraphic(new ImageView(new Image(getClass().getResourceAsStream( "fileinfo-32.png")))); for (int i = 0; i < label.length; i++) { grid.add(new Label(label[i]), 1, i + 3); tfEstimated[i] = new TextField("20"); tfEstimated[i].setPromptText("0"); tfEstimated[i].setTooltip(tip); tfActual[i] = new TextField("20"); tfActual[i].setPromptText("0"); tfActual[i].setTooltip(tip); grid.add(tfEstimated[i], 2, i + 3); grid.add(tfActual[i], 3, i + 3); } lblEstTot.setFont(Font.font("Tahoma", FontWeight.BOLD, 12)); lblActTot.setFont(Font.font("Tahoma", FontWeight.BOLD, 12)); grid.add(lblEstTot, 2, 11); grid.add(lblActTot, 3, 11); Button btn = new Button("Calculate and create Chart"); btn.setOnAction(new EventHandler() { public void handle(ActionEvent event) { /* * This may be removed to feed random value into the form on button click feedRandomValue(); */ estimatedPieChart(); actualPieChart(); barChart(); Integer etot = 0; Integer atot = 0; for (int i = 0; i < label.length; i++) { etot += Integer.parseInt(tfEstimated[i].getText()); atot += Integer.parseInt(tfActual[i].getText()); } lblEstTot.setText("$ " + etot.toString()); lblActTot.setText("$ " + atot.toString()); } }); grid.add(btn, 3, 12); return grid; } /* * Pie chart is created here for breakdown of estimated cost */ public void estimatedPieChart() { estimateChart.getData().clear(); ObservableListpieChartData = FXCollections .observableArrayList(); for (int i = 0; i < label.length; i++) { pieChartData.add(new PieChart.Data(label[i], Integer .parseInt(tfEstimated[i].getText()))); } estimateChart.setStyle(bgcolor); estimateChart.setData(pieChartData); estimateChart.setAnimated(true); estimateChart.setTitle("Estimated Cost Breakdown"); } /* * Pie chart is created here for breakdown of actual expense */ public void actualPieChart() { actualChart.getData().clear(); ObservableListpieChartData = FXCollections .observableArrayList(); for (int i = 0; i < label.length; i++) { pieChartData.add(new PieChart.Data(label[i], Integer .parseInt(tfActual[i].getText()))); } actualChart.setStyle(bgcolor); actualChart.setData(pieChartData); actualChart.setAnimated(true); actualChart.setTitle("Actual Cost Breakdown"); } /* * Bar chart is created here for tallying estimated vs. actual expenses */ @SuppressWarnings({ "unchecked", "rawtypes" }) public void barChart() { bc.getData().clear(); bc.setAnimated(true); bc.setTitle("Estimated Vs Actual Cost"); xAxis.setLabel("Estimated Vs Actual Cost"); yAxis.setLabel("Cost"); XYChart.Series series1 = new XYChart.Series(); series1.setName("Estimated Cost"); for (int i = 0; i < label.length; i++) series1.getData().add( new XYChart.Data(label[i], Integer.parseInt(tfEstimated[i] .getText()))); XYChart.Series series2 = new XYChart.Series(); series2.setName("Actual Cost"); for (int i = 0; i < label.length; i++) series2.getData().add( new XYChart.Data(label[i], Integer.parseInt(tfActual[i] .getText()))); bc.getData().addAll(series1, series2); } /* * * This may be removed to feed random value into the form * public void feedRandomValue() { Random r = new Random(Calendar.getInstance().getTimeInMillis()); for (int i = 0; i < label.length; i++) { int n = r.nextInt() % 5000; if (n < 0) n = n * -1; tfEstimated[i].setText(String.valueOf(n)); n = r.nextInt() % 5000; if (n < 0) n = n * -1; tfActual[i].setText(String.valueOf(n)); } } */ }

Conclusion



The most important upgrade in JavaFX 2.0 is obviously its seamless integration of the JavaFX API with the Java API. Though scripting has its use, as a programmer I never liked JavaFX before. Though it will take some time to attain maturity in the market, JavaFX 2.0 is definitely worth considering when building rich enterprise client applications. It features data visualization, media streaming, seamless integration of Web content, hardware accelerated graphics, animations, effects, a rich set of UI controls. and more. The added advantage of JavaFX applications deployed within a browser page or as desktop applications is they can leverage the strengths of the underlying Java platform, including a robust security model, thousands of APIs, a high-performance virtual machine and years of optimization.



Manoj Debnath is a Java programmer with more than seven years of software development experience.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap